Sql server pivot table or inner join for daterange and sum column -


i have sql problem , not sure how go because i'm not expert in sql.

my outcome is:

**month | 2011 | 2012** 1   8894108.372544  9200915.88732 2   8987154.877656  8188366.52079 3   8135004.323592  9383008.68889201 4   8374239.052416  9660272.13007201 5   9525066.469164  8578163.904876 6   7475397.368328  8606525.720634 7   8992114.52414401    9365367.617496 8   9358753.61943   11048712.924366 9   8853398.95447799    8499947.810022 10  8577323.223498  9225470.14965 11  9265685.67622799    9226157.80527 12  8887908.50775001    8318114.32842 

here sql.

select month( ts.transactiondate) [transactiondate],  isnull(sum(cast([saleprice] float)), 0) "sum([saleprice]) 2011", s.[sum([saleprice]]) 2012]  [tablesales] ts inner join (select month( t.transactiondate)  [transactiondate],  isnull(sum(cast([saleprice] float)), 0) "sum([saleprice]) 2012"   [tablesales]  t [transactiondate] between '2012-01-01' , '2012-12-31' group month( t.transactiondate)) s on s.transactiondate = month( ts.transactiondate) ts.transactiondate between '2011-01-01' , '2011-12-31' group month(ts.transactiondate), s.[sum([saleprice]]) 2012] order [transactiondate] 

the problem have date range "2011" "2015". dont want create inner joins multiple times this. there better way , please can have code examples. in advance.

i know doing single join when there date range of 1 year. "2011 2012". not great when have 2011-2015 thats 4 years means 4 inner joins?

this query pivot data:

; sales(y, m, sale) as(     select year(transactiondate), month(transactiondate), saleprice tablesales     transactiondate >= '20120101' , transactiondate< '20150101' ) select m, [2012], [2013], [2014] sales pivot(     sum(sale)     y in ([2012], [2013], [2014]) ) piv 

you need update clause (start , end years) , add years in select , for


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -