SQL Server 交叉表查询 case

前端之家收集整理的这篇文章主要介绍了SQL Server 交叉表查询 case前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码如下所示:

表landundertake结构如下所示:

表appraiser结构如下所示:

access代码

代码如下:
TRANSFORM First(Landundertake.valuerId) AS valuerId之First
SELECT Appraiser.quarterId,Landundertake.landCode
FROM Landundertake INNER JOIN Appraiser ON (Landundertake .valuerId = Appraiser.valuerID) AND (Landundertake .quarterId = Appraiser.quarterId)
GROUP BY Appraiser.quarterId,Landundertake .landCode
PIVOT Landundertake .valuerGrade;

sql server代码
代码如下:
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 appraiserID1
from dbo.LandUndertake inner join
dbo.Appraiser ON dbo.LandUndertake.quarterID=dbo.appraiser.quarterID and dbo.landundertake.appraiserID=DBO.Appraiser.appraiserID
Group by dbo.appraiser.quarterID,dbo.landundertake.appraiserGrade,dbo.landundertake.appraiserID

交叉表查询结果如下所示:

原文链接:https://www.f2er.com/mssql/63879.html

猜你在找的MsSQL相关文章