【问题标题】:Merging rows in MySQL with the same item number but different dates在 MySQL 中合并具有相同项目编号但不同日期的行
【发布时间】:2018-10-11 08:32:06
【问题描述】:

大家好,我想向你们寻求帮助。 看到我有一张桌子Table1 有列

Item_id, 
date, 
count

我需要的是一个 MySQL 查询,它将: 查询条件 *所有具有相同id的项目应合并为一行 *将日期从最早到最新排成一行。

谢谢你,祝你有美好的一天。

表1

Item_id||Date      |Count
1       |01-20-2017|    12
1       |02-02-2017|    5
1       |03-01-2017|    10
2       |03-01-2017|    45
3       |01-26-2017|    15
1       |04-09-2017|    12
2       |04-25-2017|    5
3       |02-15-2017|    45

表2

Item_id    Date1    Count1     Date2    Count2     Date3    Count3     Date4    Count4
1      01-20-2017      12    02-02-2017     5   03-01-2017    10   04-09-2017   
2      03-01-2017      45    04-25-2017     5   
3      01-26-2017      15    02-15-2017     45  

【问题讨论】:

  • 要在 MySQL 中实现这一目标,您需要使用动态 SQL,这可能远高于您(和我)的工资等级。在表示层(PHP?)中生成此输出会容易得多。顺便问一下,为什么需要这个输出?
  • 如果您可以将示例数据和预期输出作为文本添加到问题中,我会给您答案。
  • 您可能需要SELECT Item_id, GROUP_CONCAT(Date) FROM Table1 GROUP BY Item_id 之类的内容。但是你仍然需要在你的应用程序中做一些事情。像 Tim(?) 一样,如果您只是制作一个没有任何额外 COUNT() 等函数的简单列表,我也看不到这个输出的意图。您最终可能会得到一堆空列(例如现在的 Item_id 1 除外)。
  • 现在我想想,预期的输出没有意义。哈哈。实际上这个问题只是转达给了我。要点是计算每笔交易之间的天数差异。
  • MySQL pivot table的可能重复

标签: mysql


【解决方案1】:
set @sql = 
(
select  concat
            ('Select item_id,',
            group_Concat(maxs)
            ,' from '
            , '(select t.*,if(item_id <> @p ,@col:=1,@col:=@col+1) col,
                @p:= item_id
             from t cross join (select @col:=0,@p:=0) c
             order by item_id,date) c
             group by item_id;'
            ) 
from
(
select 
         concat
         (
         'max(case when col  = ', col, ' then date else null end) date',col,
         ','
         'max(case when col  = ', col, ' then count else null end) count',col
         ) as maxs

from
(
select @col:=@col + 1 col
from t
cross join (select @col:=0) c
where item_id = 
(
select item_id from
(
select item_id, count(*) col from t group by item_id order by count(*) desc limit 1
) s
)
) x
) y

);

这里日期最多的 item_id 用于构建所需数量的聚合语句并包装在 select..group by 中

@sql 然后可以在准备好的语句中使用

prepare sqlstmt from @sql;
execute sqlstmt;
deallocate prepare sqlstmt;

给出这个结果

+---------+------------+--------+------------+--------+------------+--------+------------+--------+
| item_id | date1      | count1 | date2      | count2 | date3      | count3 | date4      | count4 |
+---------+------------+--------+------------+--------+------------+--------+------------+--------+
|       1 | 2017-01-20 |     12 | 2017-02-02 |      5 | 2017-03-01 |     10 | 2017-04-09 |     12 |
|       2 | 2017-03-01 |     45 | 2017-04-25 |      5 | NULL       |   NULL | NULL       |   NULL |
|       3 | 2017-01-26 |     15 | 2017-02-15 |     45 | NULL       |   NULL | NULL       |   NULL |
+---------+------------+--------+------------+--------+------------+--------+------------+--------+
3 rows in set (0.00 sec)

【讨论】:

    猜你喜欢
    • 2013-07-31
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多