从日期列中获取最早的日期以在对另一列进行分组时显示
Getting the earliest date from the date column to show when grouping another column
我有以下 table:
item_name
trade_id
日期
椅子
1
01-01-2021
椅子
2
02-01-2021
办公桌
3
02-01-2021
椅子
4
03-01-2021
table
5
03-01-2021
我正在尝试按 item_name
对 table 中的行进行分组,同时还显示 first/earliest trade_id
的 date
(在其他换句话说,我想要的输出是 | chair | 1, 2, 4 | 01-01-2021 |
,而当前输出是 | chair | 1, 2, 4 | 03-01-2021 |
),但运气不好(只找到了使用 ORDER BY
的可能解决方案,遗憾的是没有用).
这可能吗?
将GROUP_CONCAT
与MIN
一起使用:
SELECT item_name, GROUP_CONCAT(trade_id ORDER BY trade_id), MIN(date) AS first_date
FROM yourTable
GROUP BY item_name;
我有以下 table:
item_name | trade_id | 日期 |
---|---|---|
椅子 | 1 | 01-01-2021 |
椅子 | 2 | 02-01-2021 |
办公桌 | 3 | 02-01-2021 |
椅子 | 4 | 03-01-2021 |
table | 5 | 03-01-2021 |
我正在尝试按 item_name
对 table 中的行进行分组,同时还显示 first/earliest trade_id
的 date
(在其他换句话说,我想要的输出是 | chair | 1, 2, 4 | 01-01-2021 |
,而当前输出是 | chair | 1, 2, 4 | 03-01-2021 |
),但运气不好(只找到了使用 ORDER BY
的可能解决方案,遗憾的是没有用).
这可能吗?
将GROUP_CONCAT
与MIN
一起使用:
SELECT item_name, GROUP_CONCAT(trade_id ORDER BY trade_id), MIN(date) AS first_date
FROM yourTable
GROUP BY item_name;