gpt4 book ai didi

mysql - 使用sql查询从单列设置两个列值

转载 作者:行者123 更新时间:2023-11-30 23:18:56 25 4
gpt4 key购买 nike

我有一张包含以下详细信息的表格

ID    statusID  logTime
100238 1 2011-07-07 03:48:43.000
100238 2 2011-07-07 03:48:46.000
100238 1 2011-07-07 09:07:57.000
100238 2 2011-07-07 16:12:28.000
100238 1 2011-07-08 02:59:57.000
100238 2 2011-07-08 03:00:00.000
100238 1 2011-07-08 09:26:37.000
100238 2 2011-07-08 14:03:05.000

所需的输出应该是这样的

repID   ClockIn                    ClockOut
100238 2011-07-07 03:48:43.000 2011-07-07 03:48:46.000
100238 2011-07-07 09:07:57.000 2011-07-07 16:12:28.000
100238 2011-07-08 02:59:57.000 2011-07-08 03:00:00.000
100238 2011-07-08 09:26:37.000 2011-07-08 14:03:05.000

即.. 如果 statusID 为 1,则 logtime 必须在 ClockIn 列中,如果 statusID 为 2,则 logtime 必须在 ClockOut 列中

我用过这样的查询

SELECT repID,
ClockIn,
ClockOut from
(SELECT a.eventID,
a.repID,
a.logTime,
CASE WHEN a.statusID = 1 THEN a.logTime ELSE NULL END AS ClockIn,
CASE WHEN b.statusID = 2 THEN b.logTime ELSE NULL END AS ClockOut
FROM tbl_ets_reptimelog a
LEFT JOIN tbl_ets_reptimelog b ON a.repID = b.repID)c

结果如下

repID   ClockIn                     ClockOut
100238 2011-07-07 03:48:43.000 NULL
100238 2011-07-07 03:48:43.000 2011-07-07 03:48:46.000
100238 2011-07-07 09:07:57.000 NULL
100238 2011-07-07 09:07:57.000 2011-07-07 16:12:28.000
100238 2011-07-08 02:59:57.000 NULL
100238 2011-07-08 02:59:57.000 2011-07-08 03:00:00.000
100238 2011-07-08 09:26:37.000 NULL
100238 2011-07-08 09:26:37.000 2011-07-08 14:03:05.000

如何删除 ClockOut 列中出现的带有 NULL 值的额外行

请帮忙解决这个问题......

最佳答案

只需在外部查询中添加一个 WHERE 子句

select repID,ClockIn,ClockOut from(select a.eventID, a.repID, a.logTime, 
case when a.statusID = 1 then a.logTime else null end as ClockIn,
case when b.statusID = 2 then b.logTime else null end as ClockOut from tbl_ets_reptimelog a
left join tbl_ets_reptimelog b on a.repID = b.repID)c
WHERE ClockOut IS NOT NULL

关于mysql - 使用sql查询从单列设置两个列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16393829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com