Denomination | Count
----------------------------------------
Cnanaya Catholic | 17
RC Latin Catholic | 28
RC Syrian Catholic | 3
我想交换行和列,如下所示
Cnanaya Catholic | RC Latin Catholic | RC Syrian Catholic
---------------------------------------------------------
17 | 28 | 3
请帮忙解决任何问题。
答案 0 :(得分:1)
您可以使用如下PIVOT:
假设您当前的结果是分组:
SELECT Denomination, count(id) as count
from table
group by Denomination
您的透视查询将如下:
SELECT * FROM (SELECT Denomination, id from table) d
PIVOT (COUNT(id) FOR Denomination IN ([Cnanaya Catholic],[RC Latin Catholic],[RC Latin Catholic],[RC Syrian Catholic])) p
答案 1 :(得分:0)
您可以使用sql server PIVOT
答案 2 :(得分:0)
这是一个row2column问题。 试试这个:
select
sum(case Denomination when 'Cnanaya Catholic' then Count end) CnanayaCatholic,
sum(case Denomination when 'RC Latin Catholic' then Count end) RCLatinCatholic,
sum(case Denomination when 'RC Syrian Catholic' then Count end) RCSyrianCatholic
from YOURTABLE t;