0

我在使用 SQL Server 查询时遇到了一些麻烦。您可以在我的结果中看到,在第 1 列,有一条记录“第 1 级活动”,但实际上有 3 条记录“第 1 级活动,第 1 级活动第二,第 1 级活动第三”。使用我的命令MIN(activityName),它只显示 1。

我想知道我们有没有其他方法可以用 3 条记录显示这样的数据?

SELECT 
   strandName as [Strand Name], 
   scopeName as [Scope Name], 
   FoundationLevel as [Foundation Level], 
   Level1 as [Level 1]
FROM 
    (select 
         st.strandName, s.scopeName, l.levelNo, activityName 
     from 
          strands as st 
     inner join 
          scopes as s on st.id = s.strandID 
     left join 
          activities as a on s.id = a.scopeID 
     left join 
          levels as l on a.levelID = l.id where unitID = 2) up 
PIVOT 
     (min(activityName) FOR levelNo IN (FoundationLevel, Level1)) AS pvt 
ORDER BY 
    scopeName

非常感谢。

4

1 回答 1

0
WITH T
     AS (select strandName as [Strand Name] , scopeName as [Scope Name] , FoundationLevel as [Foundation Level] , Level1 as [Level 1]

FROM (select st.strandName, s.scopeName, l.levelNo, activityName from strands as st 

inner join scopes as s on st.id = s.strandID 

left join activities as a on s.id = a.scopeID 

left join levels as l on a.levelID = l.id where unitID = 2) 

SELECT *
FROM   T PIVOT (min(activityName)
FOR levelNo IN (Level 1 activity, Level 1 activity second, Level 1 activity third) ) AS pvt
ORDER  BY OrderID  
于 2013-10-31T06:39:12.210 回答