sql server - Comparing Quarterly Sales from Q4 to Q1 -
sql server - Comparing Quarterly Sales from Q4 to Q1 -
say have table named sales looks this:
year quarter sales 2012 4 5000 2013 1 6111 2013 2 7222
and i'm trying compare sales increment quarter quarter, want end this:
q1 q2 sales difference 4 1 1111 1 2 1111
i'm having problem coming way compare q4 of previous year q1 of next year.
i've set sqlfiddle similar table here, along solution works quarters within same year.
how about:
select a.year, a.quarter q1, isnull(b.quarter, c.quarter) q2, isnull(b.sales, c.sales) - a.sales [sales increase] sales left bring together sales b on a.quarter = b.quarter - 1 , a.year = b.year left bring together sales c on a.quarter = 4 , c.quarter = 1 , a.year = c.year - 1
sql fiddle (though changing 2014 q5 q1...)
sql-server sql-server-2008
Comments
Post a Comment