我有一个返回表格的查询,就像
一样 SELECT (CASE WHEN type=1 THEN 'A'
WHEN type=2 THEN 'B'
END) as TYPE,COUNT(*) AS COUNT
from TYPE_TABLE GROUP BY TYPE
(我使用案例来计算每种类型的数量)。
count type
_____ _____
123 A
124 B
120 C
我想让表格像这样
A B C
___ ___ ___
123 124 120
所以实际上我需要对列数进行转置。我如何在HANA中得到它?
答案 0 :(得分:1)
因为只有 3
你可以
SELECT SUM(CASE WHEN TYPE = 1 THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN TYPE = 2 THEN 1 ELSE 0 END) AS B,
SUM(CASE WHEN TYPE = 3 THEN 1 ELSE 0 END) AS C
FROM yourTable
答案 1 :(得分:0)
纯SQL解决方案( SQL Server )
select
[A]
, [B]
, [C]
from
(
select Type,[Count]
from ( SELECT (CASE WHEN type=1 THEN 'A'
WHEN type=2 THEN 'B'
.....
END) as TYPE,COUNT(*) AS COUNT
from TYPE_TABLE GROUP BY TYPE) Table1
) x
pivot
(
SUM(Count)
for Type in([A], [B], [C])
)p
答案 2 :(得分:0)
我知道这是一个较晚的答复,但是您当然可以在“计数”列的计算视图中创建3个限制措施,并过滤类型-A,B,C。
TA-DA,行将转换为列。