2016-11-18 109 views
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 
+0

什麼是你所得到的錯誤?另外,你提到你正在嘗試加入另一個表,但是你發佈的腳本沒有任何連接。 –

+0

'從....從CTE組中選擇....到#someTemp ....' – scsimon

回答

0

使用數據透視表您的生活將會更容易 - 你不需要cte。

而且你的問題 - 有幾種類型的臨時表:

  1. 全局臨時表 - 與##前綴(您可以訪問其他會話此表)
  2. 會話臨時表 - 與#前綴(只能從會話訪問此表)
  3. 變量臨時 - 必須聲明並以@作爲前綴
  4. tempdb上的表 - 公用表,但管理任務後重新啓動(重新啓動服務)將變得模糊ED

其他:

  • 可以使用視圖或TVF代替臨時表(它是另一種方式)
  • 可以利用CTE的potencial(一般人僅用於查看)
  • 類型1,2,4可以創建爲標準表(使用create statement或script select into)。 類型3你必須聲明爲變量。

    會議臨時表的例子:

    create table #TmpTable (id int, col1 varchar(5), col2 varchar(5)) 
    
        insert into #TmpTable (id, col1, col2) select 1, 'KSDFA', 'ASDAS' -- from tbl