MySQL查询总数逐行

时间:2014-03-10 07:54:17

标签: mysql

我想按行总结一下。有什么快捷的方法吗?

当前查询:

select t1.name as domain, cast(sum(t2.http_out)/1024/1024/1024 as decimal(8,2)) as HTTP_out_GB, cast(sum(t2.ftp_out)/1024/1024/1024 as decimal(8,2)) as FTP_out_GB, cast(sum(t2.smtp_out)/1024/1024/1024 as decimal(8,2)) as SMTP_out_GB, cast(sum(t2.pop3_imap_out)/1024/1024/1024 as decimal(8,2)) as POP_IMAP_out_GB 

from domains t1 join DomainsTraffic t2 on t1.id = t2.dom_id 

where t2.date BETWEEN '2014-02-01 00:00:00' AND '2014-02-28 23:59:59' 

group by domain;

示例:

+--------------------------------+-------------+------------+-------------+-----------------+
| domain                         | HTTP_out_GB | FTP_out_GB | SMTP_out_GB | POP_IMAP_out_GB |
+--------------------------------+-------------+------------+-------------+-----------------+
| abc.com                        |        0.00 |       0.00 |        0.08 |            0.39 |
| def.com                        |        0.00 |       0.00 |        1.84 |            2.45 |
| fgh.com                        |        0.00 |       0.00 |        0.00 |            0.00 |

需要什么:

+--------------------------------+-------------+------------+-------------+-----------------+-----------------+
| domain                         | HTTP_out_GB | FTP_out_GB | SMTP_out_GB | POP_IMAP_out_GB | TOTAL           |
+--------------------------------+-------------+------------+-------------+-----------------+-----------------+
| abc.com                        |        0.00 |       0.00 |        0.08 |            0.39 | 0.47             
| def.com                        |        0.00 |       0.00 |        1.84 |            2.45 | 4.29             
| fgh.com                        |        0.00 |       0.00 |        0.00 |            0.00 | 0.00             

1 个答案:

答案 0 :(得分:0)

select domain, HTTP_out_GB, FTP_out_GB, SMTP_out_GB, POP_IMAP_out_GB,
(HTTP_out_GBFTP_out_GB + SMTP_out_GB + POP_IMAP_out_GB) as total from(
select t1.name as domain, cast(sum(t2.http_out)/1024/1024/1024 as decimal(8,2)) as HTTP_out_GB, cast(sum(t2.ftp_out)/1024/1024/1024 as decimal(8,2)) as FTP_out_GB, cast(sum(t2.smtp_out)/1024/1024/1024 as decimal(8,2)) as SMTP_out_GB, cast(sum(t2.pop3_imap_out)/1024/1024/1024 as decimal(8,2)) as POP_IMAP_out_GB 
from domains t1 join DomainsTraffic t2 on t1.id = t2.dom_id 
where t2.date BETWEEN '2014-02-01 00:00:00' AND '2014-02-28 23:59:59' 
group by domain) as A