如何显示每月的最近总计作为运行总计?

时间:2019-04-10 18:14:24

标签: sql sql-server

我正在尝试使SQL Server全年的运行总计。尽管代码成功创建了运行总计,但由于数据表由数百万个观察值组成,因此每个月的每一天都会返回许多观察值,其中许多是在同一天进行的。

USE PIERS

SELECT
    VDATE,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in('LOS ANGELES') 
                then TEUS end) over (order by VDATE rows unbounded preceding) WC_LA,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in('LONG BEACH') 
                then TEUS end) over (order by VDATE rows unbounded preceding) WC_LB,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in('SEATTLE', 'TACOMA') 
                then TEUS end) over (order by VDATE rows unbounded preceding) WC_NWSA,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in('OAKLAND') 
                then TEUS end) over (order by VDATE rows unbounded preceding) WC_Oakland,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in('ABERDEEN WA', 'ANACORTES', 'ANCHORAGE', 'ASTORIA', 'BENICIA', 'COOS BAY', 'DUTCH HBR', 'EL SEGUNDO', 'EVERETT', 'JUNEAU', 'KALAMA', 'KETCHIKAN', 'KODIAK', 'LONGVIEW', 'MARCUS HOOK', 'MONTEREY', 'NEWPORT OR', 'PITTSBURG', 'PORT ANGELES', 'PORT TOWNSEND', 'PORTLAND OR', 'PT HUENEME', 'REDWOOD CY', 'SAN DIEGO', 'SAN FRANCISCO', 'STOCKTON', 'VANCOUVER WA')  
                then TEUS end) over (order by VDATE rows unbounded preceding) WC_Other,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('NEW YORK') 
                then TEUS end) over (order by VDATE rows unbounded preceding) EC_NYNJ,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('SAVANNAH')then TEUS end) over (order by VDATE rows unbounded preceding) EC_Savannah,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('NORFOLK') then TEUS end) over (order by VDATE rows unbounded preceding) EC_Norfolk,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('CHARLESTON') then TEUS end) over (order by VDATE rows unbounded preceding) EC_Charleston,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('BALTIMORE') then TEUS end) over (order by VDATE rows unbounded preceding) EC_Baltimore,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('JACKSONVILLE') then TEUS end) over (order by VDATE rows unbounded preceding) EC_Jacksonville,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('MIAMI') then TEUS end) over (order by VDATE rows unbounded preceding) EC_Miami,
    SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('PT EVERGLADES') then TEUS end) over (order by VDATE rows unbounded preceding) EC_Everglades
,SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('PHILADELPHIA') then TEUS end) over (order by VDATE rows unbounded preceding) EC_Philadelphia
,SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in ('ALBANY', 'BELFAST', 'BOSTON', 'BRIDGEPORT', 'BRUNSWICK', 'CHESTER PA', 'EASTPORT', 'ERIE', 'FALL RVR', 'FERNANDNA BCH', 'FT PIERCE', 'GLOUCESTER CY', 'HOPEWELL', 'MARTINEZ', 'MOREHEAD CY', 'NEW BEDFORD', 'NEW HAVEN', 'NEW LONDON', 'NEWPORT', 'NEWPORT NEWS', 'PAULSBORO', 'PENNSAUKEN', 'PERTH AMBOY', 'PLYMOUTH', 'PORTLAND ME', 'PROVIDENCE', 'PT CANAVERAL', 'RICHMOND VA', 'ROCHESTER', 'SALEM NJ', 'SEARSPORT', 'W PALM BCH', 'WILMINGTON DE', 'WILMINGTON NC') then TEUS end) over (order by VDATE rows unbounded preceding) EC_Other

,SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in('HOUSTON') then TEUS end) over (order by VDATE rows unbounded preceding) GC_Houston
,SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in('NEW ORLEANS') then TEUS end) over (order by VDATE rows unbounded preceding) GC_NO
,SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in('MOBILE') then TEUS end) over (order by VDATE rows unbounded preceding) GC_Mobile
,SUM(case when CONFLAG IN ('1', 'C', 'Y') AND USPORT in('AVONDALE', 'BATON ROUGE', 'BEAUMONT', 'BROWNSVILLE', 'CORPUS CHRSTI', 'DESTREHAN', 'FREEPORT TX', 'GALVESTON', 'GRAMERCY', 'GULFPORT', 'LK CHARLES', 'MANATEE', 'MORGAN CY', 'ORANGE', 'PANAMA CY FL', 'PASCAGOULA', 'PENSACOLA', 'PORT ARTHUR', 'PT LAVACA', 'RICHMOND', 'S LOUISIANA', 'SABINE', 'TAMPA', 'TEXAS CITY') then TEUS end) over (order by VDATE rows unbounded preceding) GC_Other
FROM
dbo.PIERS_IMP_2018

当前输出看起来像这样。

VDATE   WC_LA   WC_LB   WC_NWSA WC_Oakland  WC_Other    EC_NYNJ EC_Savannah EC_Norfolk  EC_Charleston   EC_Baltimore    EC_Jacksonville EC_Miami    EC_Everglades   EC_Philadelphia EC_Other    GC_Houston  GC_NO   GC_Mobile   GC_Other
2018-01-27  325733.70   312893.15   90954.71    61868.57    9187.17 246965.45   142550.78   82106.38    67751.38    38852.59    18528.73    28260.92    26714.52    20625.43    42341.01    80724.51    7704.58 7376.37 14115.45
2018-01-27  325733.70   312893.15   90954.71    61868.57    9187.17 246965.63   142550.78   82106.38    67751.38    38852.59    18528.73    28260.92    26714.52    20625.43    42341.01    80724.51    7704.58 7376.37 14115.45
2018-01-27  325733.70   312893.15   90954.71    61868.57    9187.17 246965.81   142550.78   82106.38    67751.38    38852.59    18528.73    28260.92    26714.52    20625.43    42341.01    80724.51    7704.58 7376.37 14115.45
2018-01-27  325733.70   312895.15   90954.71    61868.57    9187.17 246965.81   142550.78   82106.38    67751.38    38852.59    18528.73    28260.92    26714.52    20625.43    42341.01    80724.51    7704.58 7376.37 14115.45
2018-01-28  325733.70   312895.15   90954.71    61868.57    9187.17 246965.99   142550.78   82106.38    67751.38    38852.59    18528.73    28260.92    26714.52    20625.43    42341.01    80724.51    7704.58 7376.37 14115.45
...

如您所见,我得到了由数百万个条目组成的输出,每个条目都比前一个条目增加了很小的数量。取而代之的是,我只想按月显示累计总数。例如,“一月”将是一月底的总数,而“二月”将是二月底的总数,以此类推。看起来像这样。

VDATE   WC_LA   WC_LB   WC_NWSA WC_Oakland  WC_Other    EC_NYNJ EC_Savannah EC_Norfolk  EC_Charleston   EC_Baltimore    EC_Jacksonville EC_Miami    EC_Everglades   EC_Philadelphia EC_Other    GC_Houston  GC_NO   GC_Mobile   GC_Other
2018-01  325733.70   312895.15   90954.71    61868.57    9187.17 246965.99   142550.78   82106.38    67751.38    38852.59    18528.73    28260.92    26714.52    20625.43    42341.01    80724.51    7704.58 7376.37 14115.45
2018-02 325733.70   312895.15   90974.71    61870.57    9187.17 247038.92   142576.78   82118.88    67751.38    38859.59    18528.73    28260.92    26714.52    20625.43    42341.01    80724.51    7704.58 7376.37 14115.45
...

您能帮忙吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

您的问题是,在尝试对月份进行累计之前,您没有将各个值分组为每月总计,这就是为什么在源数据的每一行都看到窗口总计的原因。

您可以使用CTE对总计进行分组,然后从此处应用运行总计:

declare @t table(d date,v int);
insert into @t values
 ('20190101',1)
,('20190102',3)
,('20190106',8)
,('20190108',4)
,('20190116',7)
,('20190102',9)
,('20190204',2)
,('20190207',9)
,('20190216',4)
,('20190220',6);

with m as
(
    select dateadd(month,datediff(month,0,d),0) as m -- Date arithmetic to return the start of the month
            ,sum(v) as MonthlyTotal
    from @t
    group by dateadd(month,datediff(month,0,d),0)
)
select m
        ,sum(MonthlyTotal) over (order by m) as CumulativeTotal
from m
order by m;

输出

+-------------------------+-----------------+
|            m            | CumulativeTotal |
+-------------------------+-----------------+
| 2019-01-01 00:00:00.000 |              32 |
| 2019-02-01 00:00:00.000 |              53 |
+-------------------------+-----------------+