我有两个表:pet类型
Id | 类型B |
---|---|
1 | 狗 |
2 | cat |
3 | 蛇 |
使用pivot
如下
SELECT *
FROM
(
select Ownerid,TypeB from Ownerstable o
join pettype p
on p.Id=o.Pettype
) d
PIVOT
(
count(TypeB)
FOR TypeB IN ([cat],[dog],[snake])
) p