我试图在案例陈述中包含4个不同的列(Case_Close,Case_Open,Case_Comment,Case_Retrieve) 将所有内容放在单列中,并且不确定如何执行此操作。 我到现在为止的是:
select report_id,
user_id,
action_type,
action_start,
action_end ,
CASE WHEN ACTION_END is null THEN case ACTION_TYPE when 'CLOSE' then ACTION_START end END as Case_Close,
CASE WHEN ACTION_END is null THEN case ACTION_TYPE when 'OPEN' then GETDATE() end END as Case_Open,
CASE WHEN ACTION_END is null THEN case ACTION_TYPE when 'COMMENT' then GETDATE() end END as Case_Comment,
CASE WHEN ACTION_END is null THEN case ACTION_TYPE when 'RETRIEVE' then GETDATE() end END as Case_Retrieve
from Table1
order by report_id
看起来像这样:
report_id user_id action_type action_start action_end Case_Close Case_Open Case_Comment Case_Retrieve
-------------------- ----------- ----------- ----------------------- ----------------------- ----------------------- ----------------------- ----------------------- -----------------------
20122511549-621 621 CLOSE 2012-02-19 10:53:28.000 NULL 2012-02-19 10:53:28.000 NULL NULL NULL
20122511549-621 621 OPEN 2012-02-05 11:05:49.000 2012-02-19 10:53:28.000 NULL NULL NULL NULL
20132121098-621 621 OPEN 2013-02-12 10:09:08.807 2013-02-12 11:16:17.167 NULL NULL NULL NULL
20132121098-621 621 COMMENT 2013-02-12 11:16:17.167 NULL NULL NULL 2013-12-26 14:55:12.017 NULL
201321814390-621 621 OPEN 2013-02-18 14:39:00.157 NULL NULL 2013-12-26 14:55:12.017 NULL NULL
201322010148-707 707 OPEN 2013-02-20 10:01:48.693 2013-10-29 13:22:05.017 NULL NULL NULL NULL
201322010148-707 707 RETRIEVE 2013-10-29 13:22:05.017 NULL NULL NULL NULL 2013-12-26 14:55:12.017
2013222142012-621 621 OPEN 2013-02-22 14:20:12.327 NULL NULL 2013-12-26 14:55:12.017 NULL NULL
2013225102516-707 707 OPEN 2013-02-25 10:25:16.790 2013-02-25 10:47:05.890 NULL NULL NULL NULL
2013225102516-707 707 OPEN 2013-02-04 13:48:38.293 2013-10-28 13:22:09.110 NULL NULL NULL NULL
201324134838-707 707 RETRIEVE 2013-10-28 13:22:09.110 NULL NULL NULL NULL 2013-12-26 14:55:12.017
我需要的是这样:
report_id user_id action_type action_start action_end Column_Action_End
-------------------- ----------- ----------- ----------------------- ----------------------- -----------------------
20122511549-621 621 CLOSE 2012-02-19 10:53:28.000 NULL 2012-02-19 10:53:28.000
20122511549-621 621 OPEN 2012-02-05 11:05:49.000 2012-02-19 10:53:28.000 NULL
20132121098-621 621 OPEN 2013-02-12 10:09:08.807 2013-02-12 11:16:17.167 NULL
20132121098-621 621 COMMENT 2013-02-12 11:16:17.167 NULL 2013-12-26 14:55:12.017
201321814390-621 621 OPEN 2013-02-18 14:39:00.157 NULL 2013-12-26 14:55:12.017
201322010148-707 707 OPEN 2013-02-20 10:01:48.693 2013-10-29 13:22:05.017 NULL
201322010148-707 707 RETRIEVE 2013-10-29 13:22:05.017 NULL 2013-12-26 14:55:12.017
2013222142012-621 621 OPEN 2013-02-22 14:20:12.327 NULL 2013-12-26 14:55:12.017
2013225102516-707 707 OPEN 2013-02-25 10:25:16.790 2013-02-25 10:47:05.890 NULL
2013225102516-707 707 OPEN 2013-02-04 13:48:38.293 2013-10-28 13:22:09.110 NULL
201324134838-707 707 RETRIEVE 2013-10-28 13:22:09.110 NULL 2013-12-26 14:55:12.017
如果any1可以帮助我,我将不胜感激?
答案 0 :(得分:3)
select report_id,
user_id,
action_type,
action_start,
action_end ,
CASE
WHEN ACTION_END is null AND ACTION_TYPE = 'CLOSE' then ACTION_START
WHEN ACTION_END is null AND ACTION_TYPE IN ('OPEN', 'COMMENT', 'RETRIEVE') then GETDATE()
ELSE NULL
END as Column_Action_End
from Table1
order by report_id
答案 1 :(得分:2)
您可以将case语句合并为一个,并在每个WHEN
子句中包含两个条件:
SELECT
report_id,
user_id,
action_type,
action_start,
action_end,
CASE
WHEN ACTION_END is null and ACTION_TYPE = 'CLOSE' then ACTION_START
WHEN ACTION_END is null and ACTION_TYPE = 'OPEN' then GETDATE()
WHEN ACTION_END is null and ACTION_TYPE = 'COMMENT' then GETDATE()
WHEN ACTION_END is null and ACTION_TYPE = 'RETRIEVE' then GETDATE()
END as Column_Action_End
FROM Table1
ORDER BY report_id
答案 2 :(得分:2)
您可以使用嵌套的案例表达式:
Select report_id, user_id, action_type, action_start, action_end ,
Case When ACTION_END is null
Then Case When ACTION_TYPE = 'CLOSE' Then ACTION_START
When ACTION_TYPE IN ('OPEN','COMMENT', 'RETRIEVE') Then GETDATE()
--Else ... (if you need else part for sub case expression)
End
--Else ...(if you need else part for main Case expression)
End As Column_Action_End
From Table1
Order by report_id
答案 3 :(得分:0)
您可以合并以检查action_end是否为空。
如果只有4种操作类型且不是可以为空的列,那么您只需检查“关闭”......
select report_id,
user_id,
action_type,
action_start,
coalesce(action_end,case when ACTION_TYPE = 'CLOSE' then ACTION_START else getdate() end) as action_end
from Table1
否则,你必须检查所有4 ...
select report_id,
user_id,
action_type,
action_start,
coalesce(action_end,case when ACTION_TYPE = 'CLOSE' then ACTION_START when ACTION_TYPE in ('OPEN','COMMENT','RETRIEVE') then getdate() end) as action_end
from Table1