在SQL中查找多列的差异和协方差

时间:2015-01-24 02:27:00

标签: sql sql-server

我在52周的时间内在原点和目的地之间有货运数据。我正试图 a)找出每周1 - 52(完成)的出发地和目的地之间的货物数量 b)找出每周发货数量的差异(见代码)
c)找出每周发货号码中的自相关(需要很多帮助)

关于如何做的更好的想法b)更好,怎么做c)?

SELECT [Origin], [Destination],

    (SQUARE(SUM(CASE WHEN [DeliveryWeek] = 1 THEN 1 ELSE 0 END))+
    SQUARE(SUM(CASE WHEN [DeliveryWeek] = 2 THEN 1 ELSE 0 END))+
    SQUARE(SUM(CASE WHEN [DeliveryWeek] = 3 THEN 1 ELSE 0 END))
    -
    3*
    SQUARE((SUM(CASE WHEN [DeliveryWeek] = 1 THEN 1 ELSE 0 END)+
    SUM(CASE WHEN [DeliveryWeek] = 2 THEN 1 ELSE 0 END)+
    SUM(CASE WHEN [DeliveryWeek] = 3 THEN 1 ELSE 0 END))/CONVERT(Decimal(10),3)))/2 as VARIANCE

FROM DATA

非常感谢!

2 个答案:

答案 0 :(得分:0)

您是否可以使用步骤A的结果来计算步骤B?喜欢的东西;

select
    Origin,
    Destination
    var(ShipmentsPerWeek)
from
    a_data
group by
    Origin,
    Destination

答案 1 :(得分:0)

我使用SQLFiddle汇总了一些数据集,但我做了三件事。第一个是每周返回Origin,Destination和一列的pivot。第二个是所有DeliveryWeeks的Origin,Destination和差异。最后一个是所有Origin和Destinations的DeliveryWeek和方差。

SQLFiddle for SQL 2008R2

--Pivot over Weeks
select Origin
, Destination
 , [1], [2], [3], [4], [5], [6], [7], [8], [9], [10]
  , [11], [12], [13], [14], [15], [16], [17], [18], [19] 
from (select Origin, Destination
,DeliveryWeek
from dbo.Shipments ) src
pivot (count(DeliveryWeek) for DeliveryWeek in (
  [1], [2], [3], [4], [5], [6], [7], [8], [9], [10]
  , [11], [12], [13], [14], [15], [16], [17], [18], [19] ) 
       )pvt

--Variance by Origin and Destination
select Origin
, Destination
, VAR(DeliveryCount) DeliveryVariance
from (
select Origin
, Destination
, DeliveryWeek
, COUNT(*) DeliveryCount
from dbo.Shipments
 group by Origin
, Destination
, DeliveryWeek
  ) x
 group by Origin
, Destination

--Variance by DeliveryWeek
select DeliveryWeek
, VAR(DeliveryCount) DeliveryVariance
from (
select Origin
, Destination
, DeliveryWeek
, COUNT(*) DeliveryCount
from dbo.Shipments
 group by Origin
, Destination
, DeliveryWeek
  ) x
 group by DeliveryWeek

希望这说明了如何使用VAR()和PIVOT()函数来帮助你筛选出你想要的东西。