Mysql:合并相同的值

时间:2017-08-08 13:00:11

标签: mysql subquery

我有 table_data 表,该表每天存储数据。

ID      |date         |size
table1   2017-08-07    90
table2   2017-08-07    52
table3   2017-08-07    5
table1   2017-08-01    60
table2   2017-08-01    45
table3   2017-08-01    5
table1   2017-07-26    16
table2   2017-07-26    45
table3   2017-07-26    2.3

我想每周获取每张桌子的最大数据,以检查我的桌子大小增加了多少

我写下面的查询

Select id, date, week-C, week-B, week-A from 
( 
select id, date, file_size as week-C, null as week-B, null as week-A 
from week_data where DATE(date) = DATE(NOW() - INTERVAL 1 DAY) group by id 
UNION ALL 
select id, date, null as week-C, file_size as week-B, null as week-A 
from week_data where DATE(date) = DATE_SUB( CURDATE() , INTERVAL 6 DAY ) group by id 
UNION ALL 
select id, date, null as week-C, null as week-B, file_size as week-A 
from week_data where DATE(date) = DATE_SUB( CURDATE() , INTERVAL 13 DAY ) group by id 
) as t 
order by week-C desc

给出了给定的结果集:

ID      |week-C    |week-B    |week-A
table1   90         NULL       NULL
table2   52         NULL       NULL
table3   5          NULL       NULL
table1   NULL       NULL       16
table2   NULL       NULL       45
table1   NULL       60         NULL
table3   NULL       NULL       2.3
table2   NULL       45         NULL
table3   NULL       5          NULL

但理想情况下我想获得如下的结果表:

ID      |week-C    |week-B    |week-A
table1   90         60         16
table2   52         45         45
table3   5          5          2.3

任何帮助都会让我感到满意

3 个答案:

答案 0 :(得分:1)

透视数据的常用方法是:

select id
    , max(case when DATE(date) = DATE(NOW()) - INTERVAL  1 DAY then file_size end) as week_c
    , max(case when DATE(date) = DATE(NOW()) - INTERVAL  7 DAY then file_size end) as week_b
    , max(case when DATE(date) = DATE(NOW()) - INTERVAL 13 DAY then file_size end) as week_a
from week_data
group by id

但它也适用于加入:

select id
    , c.file_size as week_c
    , b.file_size as week_b
    , a.file_size as week_a
from week_data c
join week_data b using(id)
join week_data a using(id)
where DATE(c.date) = DATE(NOW()) - INTERVAL  1 DAY
  and DATE(b.date) = DATE(NOW()) - INTERVAL  7 DAY
  and DATE(a.date) = DATE(NOW()) - INTERVAL 13 DAY

如果缺少某些日子,则需要LEFT JOIN:

select sub.id
     , c.file_size as week_c
     , b.file_size as week_b
     , a.file_size as week_a
from (
    select distinct id
        , DATE(NOW()) - INTERVAL  1 DAY as day_c
        , DATE(NOW()) - INTERVAL  7 DAY as day_b
        , DATE(NOW()) - INTERVAL 13 DAY as day_a
    from week_data
) sub
left join week_data c on c.id = sub.id and DATE(c.date) = sub.day_c
left join week_data b on b.id = sub.id and DATE(b.date) = sub.day_b
left join week_data a on a.id = sub.id and DATE(a.date) = sub.day_a

http://rextester.com/SVO39700

答案 1 :(得分:0)

select id, date,
MAX(CASE WHEN DATE(date) = DATE(NOW() - INTERVAL 1 DAY) THEN file_size END) `week-C`,
MAX(CASE WHEN DATE(date) = DATE_SUB( CURDATE() , INTERVAL 7 DAY )  THEN file_size END) `week-B`,
MAX(CASE WHEN DATE(date) = DATE_SUB( CURDATE() , INTERVAL 13 DAY ) THEN file_size END) `week-A`
from week_data   group by id 

尝试以上查询。

答案 2 :(得分:0)

不幸的是,MySQL没有PIVOT功能。所以我们需要实施一个变通方法。以下是可能的解决方案之一:

SELECT t1.id, t1.file_size AS weekC, t2.file_size AS weekB, t3.file_size AS weekA FROM week_data t1, week_data t2, week_data t3
WHERE t1.id = t2.id AND t2.id = t3.id
AND t1.`date` = DATE(NOW() - INTERVAL 1 DAY) 
AND t2.`date` = DATE(NOW() - INTERVAL 7 DAY) 
AND t3.`date` = DATE(NOW() - INTERVAL 13 DAY) 

结果:

id  weekC   weekB   weekA
table1  90  60  16
table2  52  45  45
table3  5   5   2

尝试的小提琴: http://sqlfiddle.com/#!9/377c7/11