简体   繁体   English

如何在同一查询中将一个数据字段用于另一个 case 表达式

[英]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?我想在CASE表达式的第二部分使用SNumber但不知道如何使用它?

    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.我想在下面的同一查询中使用SNumber ,但我无法使用它,因为这都属于一个查询。 Is there any way to use the above in below CASE ?有没有办法在下面的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,

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 .注意: contains在该上下文中不起作用 - 它是全文搜索的where子句谓词,因此我已替换为like

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 SQL 查询中使用另一个 CASE 表达式中的列值 - Use a CASE expression Column value in another CASE expression in SQL Query 如何在同一查询的另一个字段中使用计算字段 - How to use calculated field in another field of the same query 如何对以下查询使用 case 表达式 - How to use case expression for the below query 查询在一个字段中相同但在另一个字段中不同的记录? - Query for records that are the same in one field, but different in another? 如何使用CASE表达式匹配Google Data Studio中的多个行(SQL / Big Query数据集) - How to use a CASE expression to match multiples rows in Google Data Studio (SQL/Big Query data set) 如何在 SQL 中使用 CASE 语句根据另一列更改一列中的空字段 - How to use CASE statement in SQL to change an empty field in one column based on another column 如何使用 SQL 查询中一个字段的值来获取 Oracle Apex 上另一个字段的值列表? - How to use value of one field in the SQL query for list of values of another field on Oracle Apex? 一个表中的单独数据,同一表中另一个表中的数据 - Separate data in one field, that is referenced in another field within same table 如何在查询中使用CASE表达式? - How can I use CASE expression in my query? 如何从查询中的一个字段获取信息以在另一个查询中使用? - How can I get the information from one field in a query to use in another query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM