| Author |
Topic |
|
lilb321
Starting Member
2 Posts |
Posted - 2011-03-23 : 10:33:46
|
| Here is an example of my current table.BuildingID, BuildingName, PeriodEndingDate, ElectricityUse1, Oak Grove, 01/31/11, 2790151, Oak Grove, 02/28/11, 2640022, Ferndale, 11/30/10, 1552345, Winston, 2/28/11, 3459801, Oak Grove, 12/31/10, 299980I would like to run a query that returns the following table for a specific BuildingID. In this example the BuildingID = 1MonthNumber, MonthName, 2010_Elec_Usage, 2011_Elec_Usage1, Jan, 230498, 2790152, Feb, 235678, 2640023, Mar, 240567, 288976I figured out how to get the month columns, but that was about it.SELECT MonthNumber, MonthNameFROM (SELECT * ,DATEPART(mm,[Period Ending Date]) AS MonthNumber ,DATENAME(mm,[Period Ending Date]) AS MonthName FROM Utilities) AS tGROUP BY MonthNumber, MonthNameORDER BY MonthNumberI'm still a beginner with sql, so any help is appreciated. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-23 : 10:39:29
|
[code]select *from ( select MonthNumber = datepart(month, PeriodEndingDate), MonthName = datename(month, PeriodEndingDate), UsageYear = datepart(year, PeriodEndingDate), ElectricityUse from tbl ) d pivot ( sum(ElectricityUse) for UsageYear in ([2010], [2011]) ) p[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lilb321
Starting Member
2 Posts |
Posted - 2011-03-23 : 16:46:58
|
| Thanks! It worked great. |
 |
|
|
|
|
|