日期:2014-05-18  浏览次数:20471 次

急求解决:统计当前月份之前一年收入占比(跨年)
SQL应该怎么写啊
本月占比 = 本月收入 / 上月收入



------解决方案--------------------
SQL code

;with ach as
(
    select convert(varchar(6),date,112) as date,sum(qty) as qty
    from tb
    group by convert(varchar(6),date,112)
)

select a.date,cast(a.qty*100./nullif(b.qty,0) as decimal(6,2)) pe
from ach a join ach b on datediff(mm,b.date+'01',a.date+'01') = 1