我如何在SQL中的一个查询中带以下报告?有没有一种方法可以将两个日期的数据合并到一个查询中?

时间:2018-07-30 09:35:36

标签: sql sql-server sql-server-2008

当我在(@Date From'2018-06-01')和(@Date To ='2018-06-01')上运行查询时

DateFrom	Opening Qty	In Quantity	Out Quantity	Closing Qty	 For Billing
2018-06-01 	1556	       802.	             0.            	2358.        1556

当我更改时(@Date From'2018-06-02')和(@Date To ='2018-06-02') 同样,当我更改更多日期时,昨天的截止日期变成了我所输入的日期的开始日期。

Date	   Opening Qty	In Quantity	Out Quantity	Closing Qty	For Billing
2018-06-02	2358	   1443	            1095	        2706	2358

IF OBJECT_ID('TEMPDB..#Temp')IS NOT NULL 
DROP TABLE #Temp

Declare @DateFrom DateTime
Set @DateFrom='2018-06-01'
Declare @DateTo DateTime
Set @DateTo='2018-06-01'

Select @DateFrom Date,
 case when cast(ILE.Quantity as Numeric(19,6))>0 and ILE.[Posting Date] between @DateFrom and @DateTo 
 then cast(ILE.Quantity as Numeric(19,6)) else 0 end AS [In Quantity],
case when cast(ILE.Quantity as Numeric(19,6))<0 and ILE.[Posting Date] between @DateFrom and @DateTo 
then -cast(ILE.Quantity as Numeric(19,6)) else 0 end AS [Out Quantity],
(select SUM(ILE1.[Quantity]) from [Snowman Logistics Limited$Item Ledger Entry] as ILE1 where ILE1.[Entry No_]=ILE.[Entry No_] and ILE1.[Item No_]=ILE.[Item No_]
and ILE1.[Document No_]=ILE.[Document No_] and ILE1.[Posting Date]<@DateFrom) as [Opening Qty],
(select SUM(ILE1.[Quantity]) from [Snowman Logistics Limited$Item Ledger Entry] as ILE1 where ILE1.[Entry No_]=ILE.[Entry No_] and ILE1.[Item No_]=ILE.[Item No_]
and ILE1.[Document No_]=ILE.[Document No_] and ILE.[Posting Date]<=@DateTo) as [Closing Qty],ILE.[Posting Date][Posting Date],
ILE.[Item No_] Product,
   ROW_NUMBER() OVER (Partition BY [Item No_] Order by ILE.[Posting Date]) Row_Num
into #Temp
 from [Snowman Logistics Limited$Item Ledger Entry]ILE where ILE.[Posting Date]<=@DateTo and  ILE.[Primary Customer No_]in('MMBP000094 ') 
 
  Select
  Date
  ,Sum([Opening Qty])[Opening Qty],
   Sum([In Quantity])[In Quantity],
   Sum([Out Quantity])[Out Quantity],
   Sum([Closing Qty])[Closing Qty],Sum([Opening Qty]+[In Quantity])[For Billing] 
   from #Temp Group by Date
 

I want result like this 
Date		     Opening	  In	    Out  Closing
01-06-18		1556	  802	     0	    2358
02-06-18		2358	  1443     1095	    2706
03-06-18		2706	   0	     0      2706
04-06-18		2706	   0	    416	    2790
05-06-18		2790	  792       291	    2791

有可能吗?

1 个答案:

答案 0 :(得分:0)

您只需要更改代码即可从电子表格中提取日期,而不是强制输入参数值。

SELECT 
  ILE.[Posting Date] DATE -- <--Change in code
  ,CASE 
    WHEN cast(ILE.Quantity AS NUMERIC(19, 6)) > 0
      AND ILE.[Posting Date] BETWEEN @DateFrom
        AND @DateTo
      THEN cast(ILE.Quantity AS NUMERIC(19, 6))
    ELSE 0
    END AS [In Quantity]
  ,CASE 
    WHEN cast(ILE.Quantity AS NUMERIC(19, 6)) < 0
      AND ILE.[Posting Date] BETWEEN @DateFrom
        AND @DateTo
      THEN - cast(ILE.Quantity AS NUMERIC(19, 6))
    ELSE 0
    END AS [Out Quantity]
  ,(
    SELECT SUM(ILE1.[Quantity])
    FROM [Snowman Logistics Limited$Item Ledger Entry] AS ILE1
    WHERE ILE1.[Entry No_] = ILE.[Entry No_]
      AND ILE1.[Item No_] = ILE.[Item No_]
      AND ILE1.[Document No_] = ILE.[Document No_]
      AND ILE1.[Posting Date] < @DateFrom
    ) AS [Opening Qty]
  ,(
    SELECT SUM(ILE1.[Quantity])
    FROM [Snowman Logistics Limited$Item Ledger Entry] AS ILE1
    WHERE ILE1.[Entry No_] = ILE.[Entry No_]
      AND ILE1.[Item No_] = ILE.[Item No_]
      AND ILE1.[Document No_] = ILE.[Document No_]
      AND ILE.[Posting Date] <= @DateTo
    ) AS [Closing Qty]
  ,ILE.[Posting Date] [Posting Date]
  ,ILE.[Item No_] Product
  ,ROW_NUMBER() OVER (
    PARTITION BY [Item No_] ORDER BY ILE.[Posting Date]
    ) Row_Num
INTO #Temp
FROM [Snowman Logistics Limited$Item Ledger Entry] ILE
WHERE ILE.[Posting Date] <= @DateTo
  AND ILE.[Primary Customer No_] IN ('MMBP000094 ')

SELECT 
   DATE
  ,Sum([Opening Qty]) [Opening Qty]
  ,Sum([In Quantity]) [In Quantity]
  ,Sum([Out Quantity]) [Out Quantity]
  ,Sum([Closing Qty]) [Closing Qty]
  ,Sum([Opening Qty] + [In Quantity]) [For Billing]
FROM #Temp
GROUP BY DATE
ORDER BY DATE -- <--Change in code