简体   繁体   English

合并多行数据

[英]Merge data on multiple rows in one

I have a table(MySQL) which stores the utilization of users. 我有一个表(MySQL),用于存储用户的利用率。 Now, what I want to do is, get the total utilization per day for each user. 现在,我要做的是获取每个用户每天的总利用率。 I can get data for each user, however, I am finding it really difficult to merge data from multiple rows for a single day. 我可以获取每个用户的数据,但是,发现很难在一天之内合并来自多行的数据。

Right now, the data I get is as : 现在,我得到的数据是:

id       date                        download    upload  
1        2015-10-28 08:05:10         1           5
2        2015-10-28 10:25:15         2           5
3        2015-10-28 11:25:10         3           4
4        2015-10-29 11:25:10         8           5
5        2015-10-29 11:25:10         2           7
6        2015-10-29 11:25:10         1           3
7        2015-10-30 11:25:10         11          10
8        2015-10-30 11:25:10         4           5
9        2015-10-30 11:25:10         5           1
10       2015-10-30 11:25:10         10          1

But what I want it to appear like is : 但是我希望它看起来像是:

    id       date                        download    upload  
    1        2015-10-28                  6           14
    4        2015-10-29                  11          15
    7        2015-10-30                  30          17

You can use the following query: 您可以使用以下查询:

SELECT MIN(id) AS id, DATE(`date`) AS 'date', 
       SUM(download) AS download, SUM(upload) AS upload
FROM mytable
GROUP BY DATE(`date`) 

The query uses DATE function in order to extract the date value from the date field. 查询使用DATE函数以便从date字段中提取日期值。

Demo here 在这里演示

You can try this - 你可以试试这个-

select id, date(`date`) `date`, sum(download) `download`, sum(upload) `upload`
from your_table
group by date(`date`)
order by `date`

It will sum all the downloads & uploads grouping by the date. 它将按日期汇总所有下载和上传分组。

you need to a simple sql query lik this: 您需要一个简单的SQL查询,如下所示:

select max(user_id),op_date,sum(download_count) from test group by op_date;

data: 1 1 09-SEP-16 2 数据:1 1 09-SEP-16 2
2 1 09-SEP-16 1 2 1 09-SEP-16 1
5 1 09-SEP-16 3 5 1 09-SEP-16 3
4 1 10-SEP-16 2 4 1 10-SEP-16 2
3 1 10-SEP-16 4 3 1 10-SEP-16 4

filtered data: 过滤数据:

1 09-SEP-16 6 1 09-SEP-16 6
1 10-SEP-16 6 1 10年9月10日6

Use group by command 按命令使用分组

select id,`date`,sum(download) as 'download',sum(upload) as 'upload' 
from ur_table 
group by date(`date`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM