在sql查询中组合2个计算列

时间:2012-11-09 17:04:24

标签: sql-server tsql

第1栏:

( CASE
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'General' THEN 'G'
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'Honors' THEN 'H'
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'WhosWho' THEN 'Y'
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'Catholic' THEN 'J'
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'Conservative' THEN 'B'
           ELSE 'H'
         END )                        AS Edition

第2栏:

(SELECT edition
        FROM   clients.dbo.service
        WHERE  serv_id = hdr.serv_id) AS editiontext,
       ( CASE
           WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
           ELSE ( CASE
                    WHEN si.itmclass = 'Distrib' THEN '-Disc'
                    WHEN si.itmclass = 'PremIR' THEN '0' + '-Disc'
                    WHEN si.itmclass = 'InstResp' THEN '5' + '-Disc'
                    ELSE ''
                  END )
         END )                        AS 'ItemCode'

我需要在第三列中合并Edition和ItemCode。 -Disc(ItemCode)应该像G-Disc一样,但0-Disc(ItemCode)应该变成像0G-Disc。

我需要选择

选择Edition,ItemCode,Edition + ItemCode ....

我已经使用了以下内容来创建第三列,但是如果能够以更好的方式完成这一点我很好奇......

( CASE
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'General' THEN (
           CASE
             WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
             ELSE( CASE
                     WHEN si.itmclass = 'Distrib' THEN 'G-Disc'
                     WHEN si.itmclass = 'PremIR' THEN '0G' + '-Disc'
                     WHEN si.itmclass = 'InstResp' THEN '5G' + '-Disc'
                     ELSE ''
                   END )
           END )
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'Honors' THEN (
           CASE
             WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
             ELSE( CASE
                     WHEN si.itmclass = 'Distrib' THEN 'H-Disc'
                     WHEN si.itmclass = 'PremIR' THEN '0H' + '-Disc'
                     WHEN si.itmclass = 'InstResp' THEN '5H' + '-Disc'
                     ELSE ''
                   END )
           END )
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'WhosWho' THEN (
           CASE
             WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
             ELSE( CASE
                     WHEN si.itmclass = 'Distrib' THEN 'Y-Disc'
                     WHEN si.itmclass = 'PremIR' THEN '0Y' + '-Disc'
                     WHEN si.itmclass = 'InstResp' THEN '5Y' + '-Disc'
                     ELSE ''
                   END )
           END )
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'Catholic' THEN (
           CASE
             WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
             ELSE( CASE
                     WHEN si.itmclass = 'Distrib' THEN 'J-Disc'
                     WHEN si.itmclass = 'PremIR' THEN '0J' + '-Disc'
                     WHEN si.itmclass = 'InstResp' THEN '5J' + '-Disc'
                     ELSE ''
                   END )
           END )
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'Conservative' THEN (
           CASE
             WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
             ELSE( CASE
                     WHEN si.itmclass = 'Distrib' THEN 'B-Disc'
                     WHEN si.itmclass = 'PremIR' THEN '0B' + '-Disc'
                     WHEN si.itmclass = 'InstResp' THEN '5B' + '-Disc'
                     ELSE ''
                   END )
           END )
           ELSE ( CASE
                    WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
                    ELSE( CASE
                            WHEN si.itmclass = 'Distrib' THEN 'H-Disc'
                            WHEN si.itmclass = 'PremIR' THEN '0H' + '-Disc'
                            WHEN si.itmclass = 'InstResp' THEN '5H' + '-Disc'
                            ELSE ''
                          END )
                  END )
         END )                        AS EditionItemCode

2 个答案:

答案 0 :(得分:1)

最好的方法是将查询视为子查询,父查询从该子查询中选择值。所以:

select Edition, ItemCode, ItemCode+Edition as ItemCodeEdition
from
( select (CASE
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'General' THEN 'G'
           WHEN (SELECT edition
                 FROM   clients.dbwo.service
                 WHERE  serv_id = hdr.serv_id) = 'Honors' THEN 'H'
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'WhosWho' THEN 'Y'
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'Catholic' THEN 'J'
           WHEN (SELECT edition
                 FROM   clients.dbo.service
                 WHERE  serv_id = hdr.serv_id) = 'Conservative' THEN 'B'
           ELSE 'H'
         END )                        AS Edition,

(SELECT edition
        FROM   clients.dbo.service
        WHERE  serv_id = hdr.serv_id) AS editiontext,
       ( CASE
           WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
           ELSE ( CASE
                    WHEN si.itmclass = 'Distrib' THEN '-Disc'
                    WHEN si.itmclass = 'PremIR' THEN '0' + '-Disc'
                    WHEN si.itmclass = 'InstResp' THEN '5' + '-Disc'
                    ELSE ''
                  END )
         END )                        AS 'ItemCode') as tempTable

答案 1 :(得分:1)

您的案例陈述中有许多冗余的子查询,我认为不需要在那里。如果我理解您的表和查询是正确的,您应该能够执行以下操作。注意我已将第一个case语句(Edition列)拉出到派生表连接中,然后从第二个case语句引用它以生成EditionItemCode

SELECT s.edition AS editiontext, s.e AS edition,
    CASE
        WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
        WHEN si.itmclass = 'Distrib' THEN '-Disc'
        WHEN si.itmclass = 'PremIR' THEN '0-Disc'
        WHEN si.itmclass = 'InstResp' THEN '5-Disc'
        ELSE ''
    END AS ItemCode,
    CASE
        WHEN hdr.giftcarddiscount = 1 THEN 'GCDisc'
        WHEN si.itmclass = 'Distrib' THEN s.e + '-Disc'
        WHEN si.itmclass = 'PremIR' THEN '0' + s.e + '-Disc'
        WHEN si.itmclass = 'InstResp' THEN '5' + s.e + '-Disc'
        ELSE ''
    END AS EditionItemCode
FROM hdr
LEFT OUTER JOIN
(
    SELECT serv_id, CASE edition
               WHEN 'General' THEN 'G'
               WHEN 'Honors' THEN 'H'
               WHEN 'WhosWho' THEN 'Y'
               WHEN 'Catholic' THEN 'J'
               WHEN 'Conservative' THEN 'B'
               ELSE 'H'
          END AS e, edition
    FROM clients.dbo.service
) s ON s.serv_id = hdr.serv_id
JOIN si ON ...