1

我已经编写了一个查询来对税项进行分组,如果税为空,我不想显示税名。这个怎么做?请检查我的 sql server 语句:

 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,因为它具有空值。

4

4 回答 4

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 回答
5

由于您使用的是 SQL Server,因此您可以使用公用表表达式对其进行一些简化,并在最后删除您不希望使用的行HAVING

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,则原始查询将其同时计为CSTVAT,而具有 CTE 的第一个查询将其计为CST仅。我怀疑它们没有重叠,但我想我会提到它来确定。

于 2013-06-27T05:51:01.830 回答
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 回答
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 回答