开发者

2 results with 2 where in one select statement in sql-server

开发者 https://www.devze.com 2023-02-01 07:36 出处:网络
Let\'s say.. I have two statements select min(Log_In_Time) from tbl where (event_ID=4) select max(Log_Off_Time) from tbl where (event_ID=5)

Let's say.. I have two statements

select min(Log_In_Time) from tbl where (event_ID=4)
select max(Log_Off_Time) from tbl where (event_ID=5)

How can I combine that 2 statement into one select statement which is r开发者_Python百科esulted in 2 column like..

select min(Log_In_Time), max(Log_Off_Time) from tbl where ???????????????????


You can do this with a CASE statement:

Select 
MIN (case when event_ID = 4 then Log_In_Time else null end) as MinTime,
MAX (case when event_ID = 5 then Log_Off_Time else null end) as MaxTime
from tbl 


select min(Log_In_Time) from tbl where (event_ID=4)
union all
select max(Log_Off_Time) from tbl where (event_ID=5)
0

精彩评论

暂无评论...
验证码 换一张
取 消