如何在输出期间更改值名称

时间:2011-10-07 19:43:23

标签: sql

我正在尝试在执行SQL脚本时更改存储值的名称:

SELECT
    PERSONNUM, 
    PAYCODENAME, 
    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'
                     )

我希望假期成为VAC,病假 - 支付SIC,个人业务 - 支付PER和

3 个答案:

答案 0 :(得分:4)

最简单的方法是替换CASE选项列表。

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
       ....

答案 1 :(得分:0)

使用CASE声明:

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'
    )

答案 2 :(得分:0)

您可以使用永久表,派生表或公用表表达式(取决于RDBMS中的可用性)和映射,并将其连接到in列表。

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'