使用LIKE运算符分组并忽略NULL值

时间:2013-06-27 05:31:19

标签: sql sql-server sql-server-2008-r2 group-by sql-like

我写了一个查询来对税项进行分组,如果税是空的,我不想显示税名。这该怎么做?请检查我的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%'

此Query提供如下输出:

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

在这里,我不想显示CST和增值税,因为它具有空值。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

An SQLfiddle to test with

编辑:为了完整起见,修复现有查询的最小侵入方法是将其包装在另一个删除您不感兴趣的行的选择中;

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

请注意第一个查询与原始查询的细微差别,如果某行设置为CSTVAT,则原始查询会将其计为CSTVAT,而使用CTE的第一个查询仅将其计为CST。我怀疑他们没有重叠,但我想我一定会提到它。

答案 2 :(得分: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

答案 3 :(得分: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