无法正确地将不同的值与组清除

时间:2016-04-06 18:21:34

标签: sql sql-server

我正在尝试运行查询,根据税率,按应税和非应税方式计算销售总额。

SELECT     '$' + CAST (SUM (CASE WHEN Taxes.SalesTax = '0.06' THEN Order_Summary.CompleteSalesTax
            WHEN Taxes.SalesTax = '0.065' THEN Order_Summary.CompleteSalesTax
            WHEN Taxes.SalesTax = '0.07' THEN Order_Summary.CompleteSalesTax 
            WHEN Taxes.SalesTax = '0.075' THEN Order_Summary.CompleteSalesTax ELSE 0.0 END) AS varchar(10))
            AS 'Non-Taxable Sales',
          '$' + CAST (SUM(CASE WHEN Taxes.SalesTax = '0.06' THEN Order_Summary.CompleteSalesNonTax
            WHEN Taxes.SalesTax = '0.065' THEN Order_Summary.CompleteSalesNonTax
            WHEN Taxes.SalesTax = '0.07' THEN Order_Summary.CompleteSalesNonTax 
            WHEN Taxes.SalesTax = '0.075' THEN Order_Summary.CompleteSalesNonTax ELSE 0.0 END) AS varchar(10))
            AS 'Taxable Sales',
           CAST (Taxes.SalesTax * 100 AS VARCHAR ) + '%' AS 'Sales Tax Rate'
FROM ((Order_Label INNER JOIN Order_Summary
     ON Order_Label.OrderID = Order_Summary.SummID) INNER JOIN Taxes
     ON Order_Lablel.ZipCodeShipped = Taxes.StateZipCode)
WHERE (((Order_Label.StateShipped)= 'FLORIDA') 
AND ((Order_Label.O_Date) Between [DATE] And [DATE]))
GROUP BY TAXES.SalesTax
HAVING COUNT (*)  = 1

它返回结果:

        |Non-Taxable Sales|Taxable Sales|Sales Tax Rate|
        |XXXXX.XX         |XXX.XX       |X.XX%         |
        |XXXXX.XX         |XXX.XX       |X.XX%         |
        |XXXXX.XX         |XXXX.XX      |X.XX%         |
        |XXX.XX           |XX.XX        |X.XX%         |

我的问题是,Order_Label表中有一个名为OrderNumber的唯一标识符。 OrderNumber具有重复值,因此,它们聚合了所有重复项,因此我的最终值远大于它们应该的值。我有一个分组,我在查询中实现了DISTINCT,但它仍然返回不正确的值。有什么我可以在我的查询中实现,以返回正确的值并维护我现在拥有的Group。它应该只产生3列,每行4行。

2 个答案:

答案 0 :(得分:0)

另一种方式,试一试,有点乱:

WITH CteData
AS
(
SELECT Taxes.SalesTax
, Order_Summary.CompleteSalesTax
, Order_Summary.CompleteSalesNonTax 
,Dev.StateZipCode
FROM 
(
SELECT DISTINCT OrderID,ZipCodeShipped,StateShipped,O_Date FROM Order_Label
) AS Dev
INNER JOIN Order_Summary ON Dev.OrderID = Order_Summary.SummID
INNER JOIN Taxes ON Dev.ZipCodeShipped = Taxes.StateZipCode
WHERE (((Dev.StateShipped)= 'FLORIDA') 
AND ((Dev.O_Date) Between [DATE] And [DATE]))
)

SELECT DevSalesTax.[Non-Taxable Sales],DevSalesNonTax.[Taxable Sales],DevSalesTaxRate.[Sales Tax Rate]
FROM

(SELECT [Non-Taxable Sales]  = 
            (SUM 
            (CASE WHEN Taxes.SalesTax = '0.06' THEN Order_Summary.CompleteSalesTax
            WHEN Taxes.SalesTax = '0.065' THEN Order_Summary.CompleteSalesTax
            WHEN Taxes.SalesTax = '0.07' THEN Order_Summary.CompleteSalesTax 
            WHEN Taxes.SalesTax = '0.075' THEN Order_Summary.CompleteSalesTax ELSE 0.0 END)), StateZipCode            
            FROM CteData
            GROUP BY SalesTax,StateZipCode
            HAVING COUNT (*)  = 1) AS DevSalesTax 
 INNER JOIN
(SELECT [Taxable Sales] = 
            (SUM
            (CASE WHEN Taxes.SalesTax = '0.06' THEN Order_Summary.CompleteSalesNonTax
            WHEN Taxes.SalesTax = '0.065' THEN Order_Summary.CompleteSalesNonTax
            WHEN Taxes.SalesTax = '0.07' THEN Order_Summary.CompleteSalesNonTax 
            WHEN Taxes.SalesTax = '0.075' THEN Order_Summary.CompleteSalesNonTax ELSE 0.0 END)),StateZipCode             
            FROM CteData
            GROUP BY SalesTax,StateZipCode
            HAVING COUNT (*)  = 1) AS DevSalesNonTax ON DevSalesNonTax.StateZipCode = DevSalesTax.StateZipCode
INNER JOIN 
(SELECT [Sales Tax Rate] = Taxes.SalesTax * 100,StateZipCode           
            FROM CteData
            GROUP BY SalesTax,StateZipCode
            HAVING COUNT (*)  = 1) AS DevSalesTaxRate ON DevSalesTaxRate.StateZipCode = DevSalesNonTax.StateZipCode

答案 1 :(得分:0)

如果订单可以发送到不同的Zipcodes,您可能会遇到麻烦。

SELECT  '$' + CAST (SUM(CASE WHEN Order_Label.SalesTax = '0.06' THEN Order_Summary.CompleteSalesTax
                             WHEN Order_Label.SalesTax = '0.065' THEN Order_Summary.CompleteSalesTax
                             WHEN Order_Label.SalesTax = '0.07' THEN Order_Summary.CompleteSalesTax
                             WHEN Order_Label.SalesTax = '0.075' THEN Order_Summary.CompleteSalesTax
                             ELSE 0.0
                        END) AS VARCHAR(10)) AS 'Non-Taxable Sales',
        '$' + CAST (SUM(CASE WHEN Order_Label.SalesTax = '0.06' THEN Order_Summary.CompleteSalesNonTax
                             WHEN Order_Label.SalesTax = '0.065' THEN Order_Summary.CompleteSalesNonTax
                             WHEN Order_Label.SalesTax = '0.07' THEN Order_Summary.CompleteSalesNonTax
                             WHEN Order_Label.SalesTax = '0.075' THEN Order_Summary.CompleteSalesNonTax
                             ELSE 0.0
                        END) AS VARCHAR(10)) AS 'Taxable Sales',
        CAST (Order_Label.SalesTax * 100 AS VARCHAR) + '%' AS 'Sales Tax Rate'
FROM    Order_Summary
        INNER JOIN (
            SELECT  DISTINCT
                    Order_Label.OrderID,
                    Taxes.SalesTax
            FROM    Order_Label
                    INNER JOIN Taxes ON Order_Label.ZipCodeShipped = Taxes.StateZipCode
            WHERE   Order_Label.StateShipped = 'FLORIDA'
                    AND Order_Label.O_Date BETWEEN [DATE] AND [DATE]
        ) Order_Label ON Order_Label.OrderID = Order_Summary.SummID
GROUP BY Order_Label.SalesTax
HAVING  COUNT(*) = 1