问题描述
大家好,
我有这样的记录。
ID类型
1 0
1 1
1 2
2 1
2 2
现在我想要一个查询,让我这样出来。
ID Type1 TYPE2 TYPE3
1是真的
2否定是真的
i尝试了一些事情,但是没有应用,因为我的表有多个连接和一些条件以及所以我不能把整个代码。上面只是我想要做的基本事情。
Hi all ,
i have records like this.
ID TYPE
1 0
1 1
1 2
2 1
2 2
now i would like to have a query which gives me out put like this .
ID Type1 TYPE2 TYPE3
1 TRUE TRUE TRUE
2 FALSE TRUE TRUE
i tried some group by kind of things but not applied as my table have multiple joins and some where conditions as well so i can not put the whole code. above is just the basic thing which i want to do.
推荐答案
SELECT id, [0], [1], [2]
FROM (SELECT id, [type] FROM mytable) AS src
PIVOT (COUNT([type]) FOR [type] IN ([0],[1],[2])) AS pvt
您可以选择重命名列或替换0 / 1为TRUE / FALSE。
You can optionally rename the columns or replace 0/1 with TRUE/FALSE.
这篇关于如何在sql中将多行合并为一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!