SQL Server老化总计不匹配

时间:2013-01-10 17:57:30

标签: sql sql-server-2008

我有一个老化的查询,它会按12,24和36个月的间隔对购买的商品数量及其成本进行老化。查询运行得很好,但是我的总计不会与用于查找某个特定供应商的总计的单个查询相加。

见下文。

    SELECT s.SupplierNumber as Supplier_Number 
    , s.suppliername as Supplier_Name
    , i.supplierpartnum as Part_No
    , SUBSTRING(e.account_code,1,5)/*+'-'+RIGHT(e.account_code,7)*/ as Account_Code
    , CASE WHEN e.reference_code = ''
        THEN 'NOREF'
        ELSE reference_code
        END as Reference_Code
    , i.Commodity
    , i.ShortDscrptn as Part_Desc
    , i.unitofmeasure as UOM
    , i.unitprice as Unit_Price
, CASE WHEN h.OrderDate >= '2012-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN COUNT(i.Quantity)
        ELSE 0 END as Annual_Qty_12
, CASE WHEN h.OrderDate >= '2011-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN COUNT(i.Quantity)
        ELSE 0 END as Annual_Qty_24 
, CASE WHEN h.OrderDate >= '2010-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN COUNT(i.Quantity)
        ELSE 0 END as Annual_Qty_36 
, CASE WHEN h.OrderDate >= '2012-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN SUM(i.UnitPrice) 
        ELSE 0 END as  Annual_Spend_12          
, CASE WHEN h.OrderDate >= '2011-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN SUM(i.UnitPrice) 
        ELSE 0 END as  Annual_Spend_24  
, CASE WHEN h.OrderDate >= '2010-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN SUM(i.UnitPrice) 
        ELSE 0 END as  Annual_Spend_36  
FROM ekp.dbo.SUPPLIER s   
JOIN ekp.dbo.ORDERHEADER h
    ON s.SupplierID = h.SupplierID
JOIN ekp.dbo.ORDERITEM i
    ON h.OrderID = i.OrderID
JOIN mgnt.dbo.ematch e
    ON h.PONumber = e.po_ctrl_num
    AND i.LineNumber = e.po_sequence_id
WHERE h.OrderDate >= '2010-01-01'
AND h.OrderDate <= '2012-12-31' 
GROUP BY s.SupplierNumber, s.SupplierName, i.SupplierPartNum,  e.account_code
, i.Commodity, i.UnitOfMeasure, i.UnitPrice, i.ShortDscrptn, h.OrderDate
, e.reference_code

我可以继续将结果复制到电子表格中,并将Annual_Spend_12列与特定Supplier相加,但不符合以下条件:

    SELECT SUM(i.unitprice) as Annual_Spend_12 
    FROM ekp.dbo.ORDERITEM i
JOIN ekp.dbo.ORDERHEADER h 
    ON i.OrderID = h.OrderID
JOIN ekp.dbo.Supplier s
    ON h.SupplierID = s.SupplierID
JOIN mgnt.dbo.ematch e
    ON h.PONumber = e.po_ctrl_num
    AND i.LineNumber = e.po_sequence_id
WHERE s.SupplierNumber = '15302'
AND h.OrderDate >= '2012-01-01'
AND h.OrderDate <= '2012-12-31' 

上面的这个查询总是总计很多,远高于顶部查询。我也对整个聚合进行了SUMmed和COUNTed,它也不匹配,实际上,它产生与顶部查询相同的结果:

    , COUNT(CASE WHEN h.OrderDate >= '2012-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN i.Quantity
        ELSE 0 END) as Annual_Qty_12
, COUNT(CASE WHEN h.OrderDate >= '2011-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN i.Quantity
        ELSE 0 END) as Annual_Qty_24 
, COUNT(CASE WHEN h.OrderDate >= '2010-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN i.Quantity
        ELSE 0 END) as Annual_Qty_36 
, SUM(CASE WHEN h.OrderDate >= '2012-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN i.UnitPrice 
        ELSE 0 END) as  Annual_Spend_12         
, SUM(CASE WHEN h.OrderDate >= '2011-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN i.UnitPrice 
        ELSE 0 END) as  Annual_Spend_24 
, SUM(CASE WHEN h.OrderDate >= '2010-01-01'
        AND h.OrderDate <= '2012-12-31'
        THEN i.UnitPrice 
        ELSE 0 END) as  Annual_Spend_36

我真的很害怕为每个聚合编写一个带临时表的存储过程。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我的猜测是测试错误:您在第一个查询中错误地将Excel中的值加起来。以下是在Excel中添加值的三种有效方法:

  • 过滤值并突出显示列。 Excel将在选择单元格中添加值(Excel 2007及更高版本)。
  • 使用“sumif()”或“sum({})”添加给定供应商的总数
  • 使用数据透视表

group by的结果必须排序。它们可能看起来有点分类,但它们可能仍然不是。进行聚合的一种方法是对数据进行排序,然后选择结果。但是,还有其他方法,例如散列聚合。而且,在多线程环境中,您不知道哪个线程将以什么顺序返回值。