Mysql subquery help

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 11:58:49

问题


I have a table which had two types of records - update and end (EventType - Column).

The Update record has the PID and webpage details where as the end record has the volume (upload and download) and PID detail (no web page detail:(). The common field for the both of the record types are PID.

Now from the table, I need to get the SUM(ULVolume+DLVolume) for all of the websites i.e - get sum(ULVo+DLVol) of each (get PID of each (get distinct websites))

At the end, this is what i am looking for from one single query.Need some help here. P.S. The database is huge >10G and query which takes less time wud be preferred.

Expected Output

Website     Sum(ULVolume+DLVolume)
apple.com   112343XXXXX
google.com  121232XXXXX

Update Record:

+-----------------+--------+---------------+----------+----------+
| PID             | Event  | Web           | ULVolume | DLVolume |
+-----------------+--------+---------------+----------+----------+
| 199710687818416 | update | kaspersky.com |          |          |
| 199710687818417 | update | google.com    |          |          |
| 199710687818418 | update | yahoo.com     |          |          |
+-----------------+--------+---------------+----------+----------+

End Record:

+-----------------+-------+------+----------+----------+
| PID             | Event | Web  | ULVolume | DLVolume |
+-----------------+-------+------+----------+----------+
| 199710687818416 | end   |      |     5187 |   309683 |
+-----------------+-------+------+----------+----------+

回答1:


Try this:

select
    u.website,
    sum(e.ULVolume + e. DLVolume) as volume
from mytable e
left join mytable u on u.PID = e.PID and u.event ='update'
where e.Event = 'end'
group by 1;


来源:https://stackoverflow.com/questions/6712847/mysql-subquery-help

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!