我能得到我的表顯示像這樣使用數據透視表動態集團通過 - 鮮明的數據透視表
month | Hugo | Marco |
january 2017 | 5 | |
february 2017 | 3 | |
january 2017 | | 4 |
february 2017 | | 7 |
我怎麼能組個月的一年,以顯示這個樣子?任何幫助將不勝感激。謝謝。
month | Hugo | Marco |
january 2017 | 5 | 4 |
february 2017 | 3 | 7 |
SET @query ='SELECT * FROM(SELECT
petstoreemployee.employeefirstname as employeefirstname
,sum(petID.breed) as breeds
,Format(date, ''MMMM-yyyy'') as Month
FROM
petID, petstoreemployee
WHERE
petID.petstoreemployeeID=petstoreemployee.petstoreemployeeID and
petID.ProjectedPrjID=1
and
(petID.date >= ''2017-01-01 00:00:00:000'' AND petID.date <=
''2017-12-31 00:00:00:000'')
group by petstoreemployee.employeefirstname, Format(date,''yyyy'')
)
as d
PIVOT(
avg(breeds)
for employeefirstname
IN (' + @pet + ')
) as p'
exec sp_executesql @query
添加GROUP BY月和使用SUM爲列雨果和Marco –
選擇月,MAX([雨果])[雨果],MAX([馬可])[馬科] FROM#表11 組按月 – Chanukya
我確實按月分組? **格式(日期,''yyyy'')** @EstebanP。 – vanillacoke9191