我在mysql表中有以下格式的数据
id order_id state time_stamp
1 1 created 1:15 6/2/2015
2 2 created 1:17 6/2/2015
3 1 ack 1:18 6/2/2015
4 1 shipped 1:20 6/2/2015
5 1 delivered 1:25 6/2/2015
6 2 ack 1:25 6/2/2015
7 2 shipped 1:26 6/2/2015
8 2 delivered 1:29 6/2/2015
我想查询此数据库以获得以下格式的结果 -
order_id created_ts ack_ts shipped_ts delivered_ts
1 1:15 6/2/2015 1:18 6/2/2015 1:20 6/2/2015 1:25 6/2/2015
2 1:17 6/2/2015 1:25 6/2/2015 1:26 6/2/2015 1:29 6/2/2015
这可以写为select SQL Query吗?请帮助您进行示例查询。 提前致谢。
答案 0 :(得分:0)
也许是这样的:
SELECT
order_id,
MAX(CASE WHEN state='created' THEN time_stamp ELSE NULL END) AS created_ts,
MAX(CASE WHEN state='ack' THEN time_stamp ELSE NULL END) AS ack_ts,
MAX(CASE WHEN state='shipped' THEN time_stamp ELSE NULL END) AS shipped_ts,
MAX(CASE WHEN state='delivered' THEN time_stamp ELSE NULL END) AS delivered_ts
FROM
Table1
GROUP BY
order_id