SQL查询,用于获取过去17周内按周分组的数据

时间:2016-10-24 18:50:06

标签: sql sql-server tsql sql-server-2012 pivot

我们正试图在SQL Server 2012中按周分组最近17周。

输入数据

 OrderDate  OrderValue
 ----------------------
  7/17/11       10
  7/24/11       20
  7/31/11       30
   8/7/11       40

SQL尝试过:我不确定它是否正朝着正确的方向发展。

Select  
    om.OrderDate, SUM(MOrderQty * MPrice) as OrderValue 
from 
    OrdMaster om 
inner join 
    OrdDetail od on om.SalesOrder = od.SalesOrder 
where
    om.OrderDate >= DATEADD(WEEK, -17,getdate()) 
group by 
    om.OrderDate

输出数据:

OrderValue   7/17/11     7/24/11     7/31/11     8/7/11  8/14/11     8/21/11     8/28/11     9/4/11  9/11/11    9/18/11  9/25/11     10/2/11     10/9/11     10/16/11    10/23/11    10/30/11    11/6/11    

对此的任何帮助将不胜感激!提前谢谢!

2 个答案:

答案 0 :(得分:1)

首先,我建议如果你在这两个表上没有以下索引,可以考虑添加它们以支持我们准备做的事情以及更多。

--===== If you don't already have them, consider adding these indexes
 CREATE NONCLUSTERED INDEX By_OrderDate
     ON [dbo].OrdMaster([OrderDate])
INCLUDE ([SalesOrder])
;
 CREATE NONCLUSTERED INDEX By_SalesOrder
     ON dbo.OrdDetail([SalesOrder])
INCLUDE ([MOrderQty],[MPrice])
;

为了使这一切“自动神奇”,我们必须使用一些动态SQL。它被称为“预聚合动态CROSSTAB”,比大多数PIVOT运营商更快。它将使您不必每周触摸代码,而且速度很快。我也冒昧地添加了“总计”专栏。细节在代码中。

我还使用“tokenized”动态SQL来简化代码。它不一定是SQL注入证明(必须经常使用QUOTENAME),但由于数据类型被转换,因此没有机会。

而且,是的,这会为您提供您正在寻找的“横向格式”。

--=======================================================================================
--      Builds and executes a high performance, pre-aggregated CROSS TAB that will
--      return the previous 17 weeks without having to adjust the code no matter
--      what today's date is. It also returns a total for the 17 weeks.
--
--      Note that if any give week has no sales, then you'll have bigger things to
--      worry about other than this code won't pick up that missing week. ;-) 
--      We could fix that but it's not worth it because it shouldn't ever happen.
--=======================================================================================
--===== If the pre-aggregate table already exists, drop it to make reruns in SSMS easier.
     IF OBJECT_ID('tempdb..#PreAgg','U') IS NOT NULL
        DROP TABLE #PreAgg
;
--===== Pre-aggregate the data into a working table.
     -- The right indexes will make this very fast and it greatly reduces the amount of
     -- work the CROSSTAB will have to do.
 SELECT  WeekDate       = CAST(DATEADD(dd,DATEDIFF(dd,-1,om.OrderDate)/7*7,-1) AS DATE)
        ,OrderValue     = SUM(od.MOrderQty * od.MPrice)
   INTO #PreAgg
   FROM dbo.OrdMaster om
   JOIN dbo.OrdDetail od ON om.SalesOrder = od.SalesOrder 
  WHERE om.OrderDate    >= DATEADD(WK,-17,DATEADD(dd,DATEDIFF(dd,-1,GETDATE())/7*7,-1))
    AND om.OrderDate    <  DATEADD(dd,DATEDIFF(dd,-1,GETDATE())/7*7,-1)
  GROUP BY DATEDIFF(dd,-1,om.OrderDate)/7*7
;
--===== Declare a place to build the dynamic SQL in.
DECLARE @SQL VARCHAR(8000)
;
--===== Create the dynamic SELECT list of the CROSSTAB from the preggregated table.
 SELECT @SQL  = ISNULL(@SQL+SPACE(8)+',','')
              + REPLACE(REPLACE(
                    '[<<WeekDate>>] = SUM(CASE WHEN WeekDate = "<<WeekDate>>" THEN OrderValue ELSE 0 END)
'               ,'"'           ,'''') --These are the other end of the replaces.
                ,'<<WeekDate>>',CONVERT(CHAR(8),WeekDate,1))
   FROM #PreAgg
  ORDER BY WeekDate
;
--===== Create the static parts of the dynamic CROSSTAB SQL and insert the dynamic part.
 SELECT @SQL = REPLACE('
 SELECT  <<@SQL>>        ,[Total]    = SUM(OrderValue)
   FROM #Preagg
;'      ,'<<@SQL>>',@SQL) --The other end of the replace
;
--===== Display the dynamic SQL for troubleshooting purposes.
     -- This can be commented out for production.
  PRINT @SQL
;
--===== Execute the dynamic SQL
   EXEC (@SQL)
;

答案 1 :(得分:0)

尝试:

Select datepart(week, om.OrderDate)),
SUM(MOrderQty * MPrice) as OrderValue 
from OrdMaster om 
inner join OrdDetail od 
        on om.SalesOrder = od.SalesOrder 
where om.OrderDate >= DATEADD(WEEK, -17,getdate()) 
group by datepart(week, om.OrderDate))

here