我试图在SQL Server Compact Edition 3.5中使用pivot,但由于Compact版本不支持pivot,我该怎么做?
表:Donate
| Name | Date | Amount |
| James | 27/7/2015 20:19 | 500 |
| Owen | 27/7/2015 20:20 | 400 |
| James | 27/7/2015 20:21 | 100 |
因此,通过以下查询,我可以在没有问题的情况下进行转动
select *
from
(select Name, datename(month, Date) as mname, Amount
from Donate) as test
pivot(
sum(Amount)
for mname in ([July])
) as pvt
获得完美的
| Name | July |
| James | 600 |
| Owen | 400 |
但是,当我使用代码是SQL Server紧凑版时,它给我一个错误,没有枢轴功能。所以我研究了一个解决方案,发现在SQL Server CE中不存在pivot。
所以任何人都知道如何在SQL Server Compact Edition中执行此操作?提前致谢
答案 0 :(得分:3)
这有用吗?
Select name,
sum(case datename(month, Date) when 'January' then Amount else 0 end) as Jan,
sum(case datename(month, Date) when 'February' then Amount else 0 end) as Feb,
...
...
From Donate
group by name