我有一张如下所示的表格:
name field value
---- ----- -----
a p 1
a q 2
b p 5
c q 7
d p 4
e p 3
e q 2
. . .
. . .
. . .
预期产出:
name p q . . . . .
---- --- --- --- --- --- --- ---
a 1 2
b 5
c 7
d 4
e 3 2
. . .
. . .
. . .
如上所示的列中的字段元素...
答案 0 :(得分:1)
我认为你可以使用这样的查询:
select name,
sum(case when field = "p" then value else 0 end) p,
sum(case when field = "q" then value else 0 end) q,
...
from yourTable
group by name;