在时间范围内的值的总和不起作用

时间:2015-02-26 08:34:33

标签: sql sql-server sql-server-2012

我有一个返回一些列的数据集。我正在编写一个查询,该查询将显示另一列买方的每一行的日期时间范围内的两列TotalQty和TotalValue的总和。这意味着总行数将是不同买家的数量。但不知何故,我的查询不是在日期时间范围内对行进行求和。它显示了每个日期的所有值。我怎么解决这个问题? 我试过这个 -

 Create PROCEDURE [dbo].[spRPTMonthWiseProductionSummaryFORATL]
@MonthName  Varchar(50) 
-- Exec spRPTMonthWiseProductionSummaryFORATL 'Sep,2014'
AS BEGIN
Declare @FromDate datetime, 
        @ToDate datetime,
        @FromDate2 datetime,
        @FromDate3 datetime,
        @FromDate4 datetime,
        @FromDate5 datetime,
        @FromDate6 datetime
Select @FromDate = Convert(Datetime,@MonthName), 
       @ToDate = DateAdd(d,-1,DateAdd(m,1,Convert(Datetime,@MonthName)))
Select @FromDate2 = DATEADD(M,1,@FromDate)
Select @FromDate3 = DATEADD(M,1,@FromDate2)
Select @FromDate4 = DATEADD(M,1,@FromDate3)
Select @FromDate5 = DATEADD(M,1,@FromDate4)
Select @FromDate6 = DATEADD(M,1,@FromDate5)

Select B.Buyer,
**CASE WHEN UseDate>=@FromDate And UseDate< @FromDate2  
     THEN SUM(TotalQty) END AS m1Qty,
CASE WHEN UseDate>=@FromDate And UseDate< @FromDate2  
     THEN SUM(TotalValue) END AS m1Value,  
CASE WHEN UseDate>=@FromDate2 And UseDate< @FromDate3  
     THEN SUM(TotalQty) END AS m2Qty,
CASE WHEN UseDate>=@FromDate2 And UseDate< @FromDate3  
     THEN SUM(TotalValue) END AS m2Value,  
CASE WHEN UseDate>=@FromDate3 And UseDate< @FromDate4  
     THEN SUM(TotalQty) END AS m3Qty,
CASE WHEN UseDate>=@FromDate3 And UseDate< @FromDate4  
     THEN SUM(TotalValue) END AS m3Value,  
CASE WHEN UseDate>=@FromDate4 And UseDate< @FromDate5  
     THEN SUM(TotalQty) END AS m4Qty,
CASE WHEN UseDate>=@FromDate4 And UseDate< @FromDate5  
     THEN SUM(TotalValue) END AS m4Value,  
CASE WHEN UseDate>=@FromDate5 And UseDate< @FromDate6  
     THEN SUM(TotalQty) END AS m5Qty,
CASE WHEN UseDate>=@FromDate5 And UseDate< @FromDate6  
     THEN SUM(TotalValue) END AS m5Value**  

from 
(
Select Buyer,
       sum(Unit1Qty)Unit1_Qty ,
       sum(Unit2Qty)Unit2_Qty,      
       sum(Unit3Qty)Unit3_Qty, 
       sum(Unit1Qty*FOB) as Unit1_Value,
       sum(Unit2Qty*FOB) as Unit2_Value,
       sum(Unit3Qty*FOB) as Unit3_Value,
       sum(Unit1Qty*CM) as Unit1_CM,
       sum(Unit2Qty*CM) as Unit2_CM,
       sum(Unit3Qty*CM) as Unit3_CM,
       sum(Unit1Qty) +sum(Unit2Qty)+ sum(Unit3Qty) as TotalQty, 
      sum(Unit1Qty*FOB)+sum(Unit2Qty*FOB) +sum(Unit3Qty*FOB) as TotalValue,
      sum(Unit1Qty*CM) +sum(Unit2Qty*CM) +sum(Unit3Qty*CM) as TotalCM,
      A.UseDate
from 
(
Select C.Name as Buyer,
Unit1Qty = Case When PPC.factoryname ='Unit-1' 
                then OM.OrderQty else 0 end,
Unit2Qty = Case When PPC.factoryname ='Unit-2' 
                then OM.OrderQty else 0 end,
Unit3Qty = Case When PPC.factoryname ='Unit-3' 
                then OM.OrderQty else 0 end,
FOB = 
(Select Rate = 
        Case When Sum(ISNULL(OrderQty,0)) <> 0 
        Then Sum(ISNULL(rate,0)*ISNULL(OrderQty,0))/Sum(ISNULL(OrderQty,0))    
        Else 0 
   End From ExportOrder 
 Where OrderRefID = OM.OrderRefID) ,
--FR.Rate  as FOB,
isnull(CM,0) as CM,LA.UseDate
from AmanProduction.dbo.OrderMaster OM
INNER JOIN AmanProduction.dbo.LineAllocation LA 
ON LA.OrderRef=OM.OrderRefID
INNER JOIN FileRef FR 
ON FR.FileRefID= OM.FileRefID
INNER JOIN SystemManager.dbo.ProductionProcessCostCenter PPC 
ON PPC.CostCenter = LA.Line
INNER JOIN SystemManager.dbo.ProductionProcess PP 
ON PP.ProcessID = PPC.ProcessId
INNER JOIN SystemManager.dbo.Contacts C 
ON C.Code = Om.BuyerCode
LEFT JOIN(Select OC.OrderRefID,Rate as CM 
from AmanProduction.dbo.OrderMasterCostBreakdown OC
INNER JOIN BOMItemGroups BG 
ON BG.BOMItemGroupId = OC.BOMItemGroupId
Where BOMItemGroupDesc ='CM') CM 
ON OM.OrderRefID=CM.OrderRefID 
Where PP.ProcessName ='Sewing' 
and LA.UseDate >=  @FromDate  ) A
Where Unit1Qty>0 or Unit2Qty>0  or Unit3Qty>0 Group by UseDate,Buyer
)B 
Group by year(UseDate),month(UseDate),UseDate,Buyer

End

我收到这样的数据 enter image description here

对于买方s.OLIVER,m1qty,m1value和m2qty,m2value的值太多了。但是m1qty,m1value和m2qty的1个值应该是1的值,“s.Oliver”的m2值是在条件条件下指定的日期范围的总和。

2 个答案:

答案 0 :(得分:1)

您的程序相当大,需要一些时间才能理解完整的逻辑。但是,有一件事似乎可能是问题,它是你的CASE表达式。它们可能应该在聚合函数中。我的意思是,而不是像这样写它们:

CASE WHEN UseDate>=@FromDate And UseDate< @FromDate2 THEN SUM(TotalQty) END

你应该(可能)这样写:

SUM(CASE WHEN UseDate>=@FromDate And UseDate< @FromDate2 THEN TotalQty END)

然后您还应该从GROUP BY中移除UseDate(但可能同时保留year(UseDate)month(UseDate) - 再说一次,如果不深入了解逻辑,很难说。)

答案 1 :(得分:0)

如果我粗略地简化了您的查询,我会在group by中看到一些奇怪的内容。

 SELECT B.Buyer
   FROM 
  (
   SELECT Buyer
     FROM 
    (
     SELECT C.Name as Buyer
     FROM ....
    )
  ) B
GROUP BY year(UseDate),month(UseDate),UseDate,Buyer

如果您想要每个买家一行,为什么不将买家列为最高级别?

如果您将group by更改为

,则每位买家可获得一行
GROUP BY Buyer

将日期分组也会为每位买家提供几行,即每个分组日期一行。您的查询似乎想要在不同的列上传播不同日期的结果,因此无需对这些列进行分组。

实际上,您可能希望在之前的一个内部查询中group by Buyer