YTD和MTD在同一个查询中

时间:2014-08-27 07:29:38

标签: sql-server-2008

我有

create table 
    tbl(CustKey int, dt varchar(6), category varchar(20), amtbc int)
    insert into tbl values(2475,'201402','P',100) 
    insert into tbl values(2475,'201403','P',130) 
    insert into tbl values(2475,'201311','R',2056) 
    insert into tbl values(2475,'201312','R',978)
    insert into tbl values(2475,'201406','P',100) 
    insert into #tbl values(2475,'201406','R',410)   
    insert into tbl values(2475,'201401','P',200)     
    insert into #tbl values(2475,'201407','R',179)
    insert into tbl values(2475,'201404','P',100) 
    insert into #tbl values(2475,'201405','P',380) 
    insert into tbl values(2475,'201407','P',100)
    insert into #tbl values(2475,'201401','R',1377)
    insert into tbl values(9628,'201404','P',500) 
    insert into #tbl values(9628,'201405','P',500) 
    insert into tbl values(9628,'201402','P',500) 
    insert into #tbl values(9628,'201406','P',500)
    insert into tbl values(9628,'201401','P',500) 
    insert into #tbl values(9628,'201407','P',500) 
    insert into tbl values(9628,'201403','P',500)
  

从tbl中选择*:

CustKey dt  category    amtbc
2475  201402    P        100
2475  201403    P        130
2475  201311    R        2056
2475  201312    R        978
2475  201406    P        100
2475  201406    R        410
2475  201401    P        200
2475  201407    R        179
2475  201404    P        100
2475  201405    P        380
2475  201407    P        100
2475  201401    R        1377
9628  201404    P        500
9628  201405    P        500
9628  201402    P        500
9628  201406    P        500
9628  201401    P        500
9628  201407    P        500
9628  201403    P        500

然后我有这个选择,每个custkey计算SUM amtbc。每个类别,每dt(每月)

  

选择custkey,dt,sum(在' R'中的类别时的情况)然后amtbc else   0结束)作为ostatnisuma,

     

总和(在(' P')中的类别然后amtbc else 0结束时的情况)as   来自tbl group by custkey的custsuma,由CustKey命令,dt
  这给了我这个:

custkey dt  ostatnisuma residencesuma
2475    201311  2056         0
2475    201312  978          0
2475    201401  1377        200
2475    201402  0           100
2475    201403  0           130
2475    201404  0           100
2475    201405  0           380
2475    201406  410         100
2475    201407  179         100
9628    201401  0           500
9628    201402  0           500
9628    201403  0           500
9628    201404  0           500
9628    201405  0           500
9628    201406  0           500
9628    201407  0           500

所以现在我知道每个月的金额。但是想知道每个月的P类金额(确定),但是对于R类我想知道全年的金额,并将该年份的金额输入该年的每一行。所以决赛桌看起来像:

custkey dt  ostatnisuma residencesuma
2475  201311    2056       0
2475  201312    978        0
2475  201401    1377    1110
2475  201402    0       1110
2475  201403    0       1110
2475  201404    0       1110
2475  201405    0       1110
2475  201406    410     1110
2475  201407    179     1110
9628  201401    0       3500
9628  201402    0       3500
9628  201403    0       3500
9628  201404    0       3500
9628  201405    0       3500
9628  201406    0       3500
9628  201407    0       3500

请注意,实际上,类别的数量类似于100+和大约500,000个值...非常感谢您提前为您提供帮助

2 个答案:

答案 0 :(得分:1)

你想要OVER子句:

SUM(...) OVER (PARTITION BY custkey, dt/100)

答案 1 :(得分:0)

您可以对按客户键和年份分组的查询尝试left join

declare @tbl table (CustKey int, dt varchar(6), category varchar(20), amtbc int);
insert into @tbl values(2475,'201402','P',100) 
insert into @tbl values(2475,'201403','P',130) 
insert into @tbl values(2475,'201311','R',2056) 
insert into @tbl values(2475,'201312','R',978)
insert into @tbl values(2475,'201406','P',100) 
insert into @tbl values(2475,'201406','R',410)   
insert into @tbl values(2475,'201401','P',200)     
insert into @tbl values(2475,'201407','R',179)
insert into @tbl values(2475,'201404','P',100) 
insert into @tbl values(2475,'201405','P',380) 
insert into @tbl values(2475,'201407','P',100)
insert into @tbl values(2475,'201401','R',1377)
insert into @tbl values(9628,'201404','P',500) 
insert into @tbl values(9628,'201405','P',500) 
insert into @tbl values(9628,'201402','P',500) 
insert into @tbl values(9628,'201406','P',500)
insert into @tbl values(9628,'201401','P',500) 
insert into @tbl values(9628,'201407','P',500) 
insert into @tbl values(9628,'201403','P',500)

--select * from @tbl

select 
    t.CustKey, 
    t.dt, 
    sum (case when t.category in ('R') then t.amtbc else 0 end) as ostatnisuma,
    --sum (case when t.category in ('P') then t.amtbc else 0 end) as residencesuma,
    coalesce(avg(x.residencesuma),0) as residencesuma
from @tbl t 

-- sub-query totals custkey, year
left join(
    select 
        custkey, 
        left(dt,4) as year,
        sum (case when category in ('P') then amtbc else 0 end) as residencesuma
    from @tbl 
    where category in ('P') 
    group by custkey, left(dt,4)
) x on x.CustKey = t.CustKey and x.year = left(t.dt,4)

group by t.CustKey, t.dt 
order by t.CustKey, t.dt