我有事件状态表,该表的结构已经定义好,我想将多行值变成一行。
当前表数据:
事件:
EVENT EVENTNAME EVENTACTION STATUS TIMESTAMP
"Event1" "FootBall" "FootBall0" "started" "1554980085"
"Event1" "TableTennis" "TT0" "started" "1554980086"
"Event1" "FootBall" "FootBall0" "success" "1554980087"
"Event1" "Chess" "CHESS0" "success" "1554980095"
"Event1" "TableTennis" "TT0" "running" "1554980189"
"Event1" "FootBall" "FootBall1" "started" "1554980082"
我需要选择查询以显示数据,如下所示:
EVENT EVENTNAME EVENTACTION STATUS STARTTIME RUNNINGTIME ENDTIME
"Event1" "FootBall" "FootBall0" "success" 1554980085 NULL 1554980087
"Event1" "TableTennis" "TT0" "running" 1554980086 1554980189 NULL
"Event1" "Chess" "CHESS0" "success" 1554980098 NULL 1554980100
我必须显示EVENT
,EVENTNAME
,EVENTACTION
和最新的STATUS
以及started
,running
和{{1 }},如果时间不存在,则应设置success
。
我使用了带大小写的SELECT查询,但是时间戳没有放在相关行中。
NULL
任何线索。
答案 0 :(得分:0)
您需要按事件进行汇总,然后采用MAX
表达式中的CASE
:
SELECT
AE.EVENT,
AE.EVENTNAME,
AE.EVENTACTION,
MAX(CASE WHEN AE.STATUS = 'started' THEN AE.timestamp END) AS starttime,
MAX(CASE WHEN AE.STATUS = 'running' THEN AE.timestamp END) AS runningtime,
MAX(CASE WHEN AE.STATUS IN ('success', 'failed') THEN AE.timestamp END) AS endtime
FROM agentEvents AE
GROUP BY
AE.EVENT,
AE.EVENTNAME,
AE.EVENTACTION
ORDER BY
MAX(CASE WHEN AE.STATUS = 'started' THEN AE.timestamp END);
请注意,仅凭timestamp
列进行排序是没有意义的,因为聚合后它不再存在。因此,我改为在开始时间之前订购。选择STATUS
列也没有任何意义,因此我已将其删除。