[英]SQL Server 2008 - Return a text string in a new column based upon criteria in WHERE statement
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'
where子句仅是过滤器...使用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
我假设您正在寻找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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.