1

税項目をグループ化するクエリを 1 つ作成しました。税が null の場合、税名を表示したくありません。これを行う方法?私のSQLサーバーステートメントを確認してください:

 SELECT     
   'CENTRAL EXCISE DUTY'    AS 'TaxName',
   SUM(TaxAmount)       AS 'Tax'
 FROM
   PMT_InvoiceTaxAttribute
 WHERE 
   InvoiceID = 100 
   AND TaxAttributeName NOT LIKE '%IBR%' 
   AND TaxAttributeName NOT LIKE '%CST%' 
   AND TaxAttributeName NOT LIKE '%VAT%'
 UNION
 SELECT
   'CST'            AS 'TaxName',
   SUM(TaxAmount)       AS 'Tax'
 FROM
   PMT_InvoiceTaxAttribute
 WHERE 
   InvoiceID = 100   
   AND TaxAttributeName LIKE '%CST%'
 UNION
 SELECT
   'VAT'            AS 'TaxName',
   SUM(TaxAmount)       AS 'Tax'
 FROM
   PMT_InvoiceTaxAttribute
 WHERE 
   InvoiceID = 100   
   AND TaxAttributeName LIKE '%VAT%'

このクエリは、次のような出力を提供します。

      TaxName                     Tax
   -------------              -----------
  CENTRAL EXCISE DUTY             15000
        CST                       NULL
        VAT                       NULL

ここでは、CST と VAT は null 値であるため、表示したくありません。

4

4 に答える 4

5

HAVINGSQL Server を使用しているため、一般的なテーブル式を使用して多少単純化し、最後に使用したくない行を削除するだけです。

WITH cte AS (
  SELECT 
    CASE WHEN TaxAttributeName LIKE '%CST%' THEN 'CST' 
         WHEN TaxAttributeName LIKE '%VAT%' THEN 'VAT'
         WHEN TaxAttributeName LIKE '%IBR%' THEN NULL
         ELSE 'CENTRAL EXCISE DUTY' END TaxName,
    TaxAmount Tax
  FROM PMT_InvoiceTaxAttribute
  WHERE InvoiceID = 100
)
SELECT TaxName, SUM(Tax) Tax FROM cte
GROUP BY TaxName
HAVING TaxName IS NOT NULL AND SUM(Tax) IS NOT NULL

でテストする SQLfiddle

編集:完全を期すために、既存のクエリを修正する最小限の侵入方法は、興味のない行を削除する別の選択でラップすることです。

SELECT * FROM (
    ...your query here...
) a_dummy_name_is_required_here
WHERE Tax IS NOT NULL;

元のクエリに対する最初のクエリのわずかな違いに注意してください。行が に設定されている場合、元のクエリはそれをとCSTVATの両方としてカウントしますが、CTE を含む最初のクエリはそれをのみとしてカウントします。それらは重複していないと思われますが、念のために言及したいと思いました。CSTVATCST

于 2013-06-27T05:51:01.830 に答える
5

これを試して:

SELECT * FROM (
SELECT     
   'CENTRAL EXCISE DUTY'    AS 'TaxName',
   SUM(TaxAmount)       AS 'Tax'
 FROM
   PMT_InvoiceTaxAttribute
 WHERE 
   InvoiceID = 100 
   AND TaxAttributeName NOT LIKE '%IBR%' 
   AND TaxAttributeName NOT LIKE '%CST%' 
   AND TaxAttributeName NOT LIKE '%VAT%'
 UNION
 SELECT
   'CST'            AS 'TaxName',
   SUM(TaxAmount)       AS 'Tax'
 FROM
   PMT_InvoiceTaxAttribute
 WHERE 
   InvoiceID = 100   
   AND TaxAttributeName LIKE '%CST%'
 UNION
 SELECT
   'VAT'            AS 'TaxName',
   SUM(TaxAmount)       AS 'Tax'
 FROM
   PMT_InvoiceTaxAttribute
 WHERE 
   InvoiceID = 100   
   AND TaxAttributeName LIKE '%VAT%') as SomeName
WHERE TAX is NOT NULL
于 2013-06-27T05:34:57.857 に答える
4

UNIONS を回避するための代替手段。

select
    Case 
        When TaxAttributeName LIKE '%CST%' Then 'CST'
        When TaxAttributeName LIKE '%VAT%' Then 'VAT'
        Else 'CENTRAL EXCISE DUTY'
    End 'TaxName',
    SUM(TaxAmount) as 'Tax'
From 
    PMT_InvoiceTaxAttribute
Where
    InvoiceID = 100
    AND TaxAttributeName not like '%IBR%'
Group By
    Case 
        When TaxAttributeName LIKE '%CST%' Then 'CST'
        When TaxAttributeName LIKE '%VAT%' Then 'VAT'
        Else 'CENTRAL EXCISE DUTY'
    End

JoachimのCTEの方が少し好きですが。多分...

with TaxCTE as (
    select
        InvoiceID,
        Case When TaxAttributeName LIKE '%CST%' Then 'CST'
             When TaxAttributeName LIKE '%VAT%' Then 'VAT'
             Else 'CENTRAL EXCISE DUTY'
        End 'TaxName',
        TaxAmount
    From PMT_InvoiceTaxAttribute
    where TaxAttributeName not like '%IBR%')
select TaxName, SUM(TaxAmount) Tax 
from TaxCTE 
where InvoiceId = 100
group by TaxName
于 2013-06-27T05:54:55.163 に答える
4

これを試してみてください -

SELECT *
FROM
(
     SELECT
           TaxName = 'CENTRAL EXCISE DUTY'
         , Tax = SUM(TaxAmount)
     FROM PMT_InvoiceTaxAttribute
     WHERE InvoiceID = 100
          AND TaxAttributeName NOT LIKE '%IBR%'
          AND TaxAttributeName NOT LIKE '%CST%'
          AND TaxAttributeName NOT LIKE '%VAT%'

     UNION ALL

     SELECT 
            TaxName = 
                    CASE WHEN TaxAttributeName LIKE '%CST%'
                              THEN 'CST' 
                              ELSE 'VAT' 
                    END
          , Tax = SUM(TaxAmount)
     FROM PMT_InvoiceTaxAttribute
     WHERE InvoiceID = 100
          AND ( 
                 TaxAttributeName LIKE '%CST%' 
               OR 
                 TaxAttributeName LIKE '%VAT%'  
          )
) t
WHERE Tax IS NOT NULL

またはこれを試してください -

;WITH cte AS 
(
     SELECT TaxAttributeName, TaxAmount 
     FROM PMT_InvoiceTaxAttribute
     WHERE InvoiceID = 100
)
SELECT *
FROM
(
     SELECT 
            TaxName = 'CENTRAL EXCISE DUTY'
          , Tax = SUM(TaxAmount)
     FROM cte
     WHERE NOT EXISTS(
         SELECT 1
         FROM (VALUES ('IBR'), ('CST'), ('VAT')) c(filter)  
         WHERE TaxAttributeName LIKE '%' + filter + '%'
     )

     UNION ALL

     SELECT 
            TaxName = 
                    CASE WHEN TaxAttributeName LIKE '%CST%'
                              THEN 'CST' 
                              ELSE 'VAT' 
                    END
          , Tax = SUM(TaxAmount)
     FROM cte
     WHERE ( 
                 TaxAttributeName LIKE '%CST%' 
               OR 
                 TaxAttributeName LIKE '%VAT%'  
          )
) t
WHERE Tax IS NOT NULL
于 2013-06-27T05:37:49.320 に答える