1

第 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。

我需要选择为

选择版本、项目代码、版本+项目代码....

我已经使用以下内容创建了第三列,但很好奇这是否可以以更好的方式完成......

( 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
4

2 回答 2

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
于 2012-11-09T17:16:39.583 回答
1

您的案例语句中有许多多余的子查询,我认为这些子查询不需要存在。如果我理解您的表格和查询正确,您应该能够执行以下操作。请注意,我已将第一个 case 语句(Edition列)提取到派生表连接中,然后从第二个 case 语句中将其引用为 make 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 ...
于 2012-11-09T17:22:44.110 回答