SQl 行变列的实例
有3个表 Car,User,User_Car 要得到一个用户对应有几种车。
SQl:
select Uname as 姓名,
MAX(case Name when '宝马' then Smoney end) '宝马',
MAX(case Name when '奔驰' then Smoney end) '奔驰',
MAX(case Name when '大众' then Smoney end) '大众'
from
(select u.Uname,c.Name,uc.Smoney
from dbo.Car c ,dbo.Users u,dbo.User_Car uc
where c.ID = uc.CID and u.ID=uc.[UID]) aa
group by aa.Uname