How to change a value name during output
I am trying to change the name of a stored value when I execute my SQL script:
SELECT
PERSONNUM,
PAYCODENAME,
CAST(WFCTIMEINSECONDS AS FLOAT)/3600 AS Total_Hours
FROM
VP_ALLTOTA开发者_StackOverflowLS
WHERE
Applydate >= '09/25/2011' AND
Applydate <= '10/01/2011' AND
PAYCODENAME IN ('Vacation'
,'Sick Leave - Paid'
,'Personal Business - Paid'
,'Comp Time - Paid'
)
I want the Vacation to be VAC, Sick Leave - Paid to be SIC, Personal Business - Paid to be PER and
The easiest way is a list of CASE
options for substitutions.
SELECT PERSONNUM
PAYCODENAME CASE WHEN 'Vacation' THEN 'VAC'
WHEN 'Sick Leave - Paid' THEN 'SIC'
WHEN 'Personal Business - Paid' THEN 'PER'
ELSE PAYCODENAME END AS PAYCODENAME
....
Use a CASE statement:
SELECT
PERSONNUM,
CASE PAYCODENAME
WHEN 'VACATION' THEN 'Vac'
WHEN 'Sick Leave - Paid' THEN 'SIC'
WHEN 'Comp Time - Paid' THEN 'PER'
ELSE PAYCODENAME
END,
cast(WFCTIMEINSECONDS as float)/3600 as Total_Hours
FROM
VP_ALLTOTALS
WHERE
Applydate >= '09/25/2011' AND
Applydate <= '10/01/2011' AND
PAYCODENAME in ('Vacation'
,Sick Leave - Paid'
,'Personal Business - Paid'
,'Comp Time - Paid'
)
You can use a permanent table, derived table, or common table expression (subject to availability in your RDBMS) with the mappings and join onto that in place of the in
list.
WITH PAYCODES(PAYCODENAME, LABEL)
AS (SELECT 'Vacation',
'VAC'
UNION ALL
SELECT 'Sick Leave - Paid',
'SIC'
UNION ALL
SELECT 'Personal Business - Paid',
'PER'
UNION ALL
SELECT 'Comp Time - Paid',
'')
SELECT PERSONNUM,
LABEL AS PAYCODENAME,
CAST(WFCTIMEINSECONDS AS FLOAT) / 3600 AS Total_Hours
FROM VP_ALLTOTALS
JOIN PAYCODES
ON PAYCODES.PAYCODENAME = VP_ALLTOTALS.PAYCODENAME
WHERE Applydate >= '09/25/2011'
AND Applydate <= '10/01/2011'
精彩评论