服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Sql Server - SQL Server 交叉表查询 case

SQL Server 交叉表查询 case

2019-12-12 16:17MSSQL教程网 Sql Server

今天下午忙于将access数据库中的交叉表转到SQL server数据库中,在SQL server中,不支持transfrom方法,查找相关资料,找到使用case方法进行转换

代码如下所示:

表landundertake结构如下所示:

SQL Server 交叉表查询 case

表appraiser结构如下所示:

SQL Server 交叉表查询 case

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.landcode,dbo.landundertake.appraiserGrade,dbo.landundertake.appraiserID


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

 

SQL Server 交叉表查询 case

延伸 · 阅读

精彩推荐