0
有沒有辦法將此查詢的結果插入到SSMS中的臨時表中?我嘗試了很多方法,但迄今爲止失敗了。如何將數據插入臨時表
還是有另一種方式,所有我尋找它來查詢這個結果,並加入另一個表,我似乎無法在查詢本身做。
USE CommDB
;With CTE AS (SELECT s.attendanceNumber,
MAX(CASE WHEN s.rnk = 1 THEN s.ExamExaminationCode END) as examCode1,
MAX(CASE WHEN s.rnk = 2 THEN s.ExamExaminationCode END) as examCode2,
MAX(CASE WHEN s.rnk = 3 THEN s.ExamExaminationCode END) as examCode3,
MAX(CASE WHEN s.rnk = 4 THEN s.ExamExaminationCode END) as examCode4,
MAX(CASE WHEN s.rnk = 5 THEN s.ExamExaminationCode END) as examCode5,
MAX(CASE WHEN s.rnk = 6 THEN s.ExamExaminationCode END) as examCode6,
MAX(CASE WHEN s.rnk = 7 THEN s.ExamExaminationCode END) as examCode7,
MAX(CASE WHEN s.rnk = 8 THEN s.ExamExaminationCode END) as examCode8,
MAX(CASE WHEN s.rnk = 9 THEN s.ExamExaminationCode END) as examCode9,
MAX(CASE WHEN s.rnk = 10 THEN s.ExamExaminationCode END) as examCode10,
MAX(CASE WHEN s.rnk = 11 THEN s.ExamExaminationCode END) as examCode11,
MAX(CASE WHEN s.rnk = 12 THEN s.ExamExaminationCode END) as examCode12,
MAX(CASE WHEN s.rnk = 13 THEN s.ExamExaminationCode END) as examCode13,
MAX(CASE WHEN s.rnk = 14 THEN s.ExamExaminationCode END) as examCode14
FROM (
SELECT [AttendanceNumber]
,[ExaminationDate]
,RadiologyID
,[ExamExaminationCode]
,ROW_NUMBER() OVER(PARTITION BY [AttendanceNumber]
ORDER BY [ExamExaminationCode]) as rnk --Ordered ASC so examcodes dont move
FROM [CommDB].[dbo].[tblRadiologyData] rd
where rd.ExaminationDate >= '01 october 2015'
and rd.AttendanceSiteCode IN('CNM','RNM')) s
GROUP BY s.attendanceNumber)
Select c.examCode1,
c.examCode2,
c.examCode3,
c.examCode4,
c.examCode5,
c.examCode6,
c.examCode7,
c.examCode8,
c.examCode9,
c.examCode10,
c.examCode11,
c.examCode12,
c.examCode13,
c.examCode14,
c.AttendanceNumber,
COUNT(c.AttendanceNumber) as [No of occurances]
-- (Select lu.HRGCode from CommDB.dbo.tblRadiologyNucMedLookup lu
-- Where ISNULL(c.examCode1,'') = ISNULL(lu.Exam01,'')
-- )
from CTE c
GROUP by c.examCode1,
c.examCode2,
C.examCode3,
C.examCode4,
C.examCode5,
C.examCode6,
C.examCode7,
C.examCode8,
C.examCode9,
C.examCode10,
C.examCode11,
C.examCode12,
C.examCode13,
C.examCode14,
C.AttendanceNumber
ORDER BY C.examCode1
什麼是你所得到的錯誤?另外,你提到你正在嘗試加入另一個表,但是你發佈的腳本沒有任何連接。 –
'從....從CTE組中選擇....到#someTemp ....' – scsimon