SQL Server 2008 - 根据 WHERE 语句中的条件在新列中返回文本字符串


select PERSONNUM, PAYCODENAME as StraightTime, PAYCODENAME as OT
from dbo.VP_ALLTOTALS
where OT in ('Overtime', 'Double Overtime') 
return 'OT',
and StraightTime in ('Straight Time Earnings', 'Sunday Premium')
return 'Straight Time'

我假设您正在寻找CASE

SELECT CASE 
         WHEN ot IN ( 'Overtime', 'Double Overtime' ) THEN ot 
         WHEN straighttime IN ( 'Straight Time Earnings', 'Sunday Premium' ) 
            THEN straighttime 
         ELSE NULL 
       END AS ColumnName 
FROM   dbo.vp_alltotals 

演示

或者,如果您只想返回字符串OR/Straight Time

 SELECT CASE 
         WHEN ot IN ( 'Overtime', 'Double Overtime' ) THEN 'OT'
         WHEN straighttime IN ( 'Straight Time Earnings', 'Sunday Premium' ) 
            THEN 'straighttime' 
         ELSE NULL 
       END AS Type
FROM   dbo.vp_alltotals 

演示

其中子句只是过滤器...使用 case 语句将逻辑放入选择行中。

select PERSONNUM, PAYCODENAME as StraightTime, PAYCODENAME as OT
case when OT in ('Overtime', 'Double Overtime')  then 'OT',
when StraightTime in ('Straight Time Earnings', 'Sunday Premium') then 'Straight Time'
else 'not in list?' end as 'returnedcode' --name your new column here
from dbo.VP_ALLTOTALS

相关内容

最新更新