Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

sql - How to use one data field into another case expression in the same query

I want to use SNumber in the second part of CASE expression but not sure how to use it?

    select    
        PE.EDateTime,
        (case when(PE.EDateTime is not NULL and cast(PE.EDateTime as time) < '12:30') then cast(format(PE.EDateTime,'yyyyMMddhhmm') as varchar(50))+'AM'
             when (PE.ADate is not NULL and cast(PE.ADate as time) < '12:30') then cast(format(PE.ADate,'yyyyMMddhhmm') as varchar(50))+'AM'
             when (PE.EDateTime is not NULL and cast(PE.EDateTime as time) >= '12:30') THEN  cast(format(PE.EDateTime,'yyyyMMddhhmm') as varchar(50))+'PM'
             when  (PE.ADate is not NULL and cast(PE.ADate as time) > '12:30') then cast(format(PE.ADate,'yyyyMMddhhmm') as varchar(50))+'PM'
             else null 
            end) as SNumber,
   
     case 
   when (SNumber like'%AM') then 'AM'
   when SNumber like '%PM') then 'PM'
   else null 
end as [Session],
        Comments   
    from (
        select       
            PE1.RId,
            PE1.ADate,
            PE1.EDateTime
        from (
            select
                RegEId, 
                ADate,
                EDateTime,
                Comments
            from PatEnr
            where PreNumber is not null               
            ) as PE1
        left join Pat PEA 
            on PE1.RegEId = PEA.RegEId   
        left join PBooking PB 
            on PB.RegEId = PE1.RegEId
        ) as PE

I want to use SNumber here below in the same query but I am not able to use this as this all belongs to one query. Is there any way to use the above in below CASE? I want to use something like below.

case 
   when (SNumber like'%AM') then 'AM'
   when (SNumber like '%%PM') then 'PM'
   else null 
end as Session,
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use cross apply to perform your calculation which you can then reuse.

select
    SNumber
    , case when SNumber like '%AM%' then 'AM'
    when SNumber like  '%PM%' then 'PM'
    else null end as [Session]
from MyTable PE
cross apply (
values (
    case when(PE.EDateTime is not NULL and cast(PE.EDateTime as time) < '12:30') then cast(format(PE.EDateTime,'yyyyMMddhhmm') as varchar(50))+'AM'
    when (PE.ADate is not NULL and cast(PE.ADate as time) < '12:30') then cast(format(PE.ADate,'yyyyMMddhhmm') as varchar(50))+'AM'
    when (PE.EDateTime is not NULL and cast(PE.EDateTime as time) >= '12:30') THEN  cast(format(PE.EDateTime,'yyyyMMddhhmm') as varchar(50))+'PM'
    when (PE.ADate is not NULL and cast(PE.ADate as time) > '12:30') then cast(format(PE.ADate,'yyyyMMddhhmm') as varchar(50))+'PM'
    else null 
    end
)) x (SNumber)

Note: contains doesn't work in that context - its a where clause predicate for full-text search, so I've replaced with like.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...