SQL Server 交叉表查询——case
今天下午忙于将access数据库中的交叉表转到SQL server数据库中,在SQL server中,不支持transfrom方法,查找相关资料,找到使用case方法进行转换。
代码如下所示:
表landundertake结构如下所示:
表appraiser结构如下所示:
access代码:
1 TRANSFORM First(Landundertake.valuerId) AS valuerId之First2 SELECT Appraiser.quarterId, Landundertake.landCode3 FROM Landundertake INNER JOIN Appraiser ON (Landundertake .valuerId = Appraiser.valuerID) AND (Landundertake .quarterId = Appraiser.quarterId)4 GROUP BY Appraiser.quarterId, Landundertake .landCode5 PIVOT Landundertake .valuerGrade;
复制代码
SQL server代码:
1 select dbo.appraiser.quarterID,dbo.landUndertake.landcode,case dbo.landUndertake.appraiserGrade when 'appraiserID1' then dbo.landundertake.appraiserID else null END as appraiserID,case dbo.landUndertake.appraiserGrade when 'appraiserID2' then dbo.landundertake.appraiserID else null END as appraiserID12 from dbo.LandUndertake inner join3 dbo.Appraiser ON dbo.LandUndertake.quarterID=dbo.appraiser.quarterID and dbo.landundertake.appraiserID=DBO.Appraiser.appraiserID4 Group by dbo.appraiser.quarterID,dbo.landUndertake.landcode,dbo.landundertake.appraiserGrade,dbo.landundertake.appraiserID