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 の両方を 3 列目に組み合わせる必要があります。-Disc (ItemCode) は G-Disc のようにする必要がありますが、0-Disc (ItemCode) は 0G-Disc のようにする必要があります。

次のように選択する必要があります

Edition、ItemCode、Edition+ItemCode... を選択します。

私は以下を使用して 3 番目の列を作成しましたが、これをより良い方法で行うことができるかどうか興味があります...

( 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列) を派生テーブルの結合に引き出し、2 番目の 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 に答える