使用CTE的多个嵌套查询

时间:2019-05-22 21:28:18

标签: sql-server

我有以下使用CTE的代码-因为它很长,所以我会简称它。我想添加此代码生成的特定列。例如,此代码将产生两列/变量,[将ED更改为ETU]和[将ED更改为ETU-DepartTime]。我想向此代码添加另一步骤,将这两个变量加在一起。我相信有CTE递归选项;但是,我对这段代码不太熟悉。

https://www.essentialsql.com/recursive-ctes-explained/

Select [Financial Number], EventDisplay1, EventDisplay2, EventDisplay3, RequestedDateTime1, RequestedDateTime2, RequestedDateTime3, CompletedDateTime1, CompletedDateTime2, CompletedDateTime3, [Depart Date & Time1],
sum([Change ED to ETU Ev 1] + [Change ED to ETU - DepartTime Ev 1] +[Change ED to ETU Ev 2] +[Change ED to ETU - DepartTime Ev 2] +[Change ED to ETU Ev 3] +[Change ED to ETU - DepartTime Ev 3]) AS [Change ED to EDU Final],
sum([Admit - Bed Ordered Ev 1] + [Admit - Bed Ordered - DepartTime Ev 1] + [Admit - Bed Ordered Ev 2] +[Admit - Bed Ordered - DepartTime Ev 2] +[Admit - Bed Ordered Ev 3] + [Admit - Bed Ordered - DepartTime Ev 3]) AS [Admit - Bed Ordered Final],
sum([Discharge Ev 1] + [Discharge- DepartTime Ev 1] + [Discharge Ev 2] + [Discharge- DepartTime Ev 2] + [Discharge Ev 3]+ [Change ED to ETU - DepartTime Ev 3] ) AS [Discharge Final]

FROM
(

select [Financial Number], EventDisplay1, EventDisplay2, EventDisplay3, RequestedDateTime1, RequestedDateTime2, RequestedDateTime3, CompletedDateTime1, CompletedDateTime2, CompletedDateTime3, [Depart Date & Time1],
IIf ((EventDisplay1 = 'Change ED to ETU' ) and  (CompletedDateTime1 not like '' and RequestedDateTime2 not like '') , datediff(mi, CompletedDateTime1, RequestedDateTime2), 0) AS [Change ED to ETU Ev 1],
IIf ((EventDisplay1 = 'Admit - Bed Ordered') and  (CompletedDateTime1 not like '' and RequestedDateTime2 not like '') , datediff(mi, CompletedDateTime1, RequestedDateTime2), 0) AS [Admit - Bed Ordered Ev 1],
IIf ((EventDisplay1 = 'Discharge') and  (CompletedDateTime1 not like '' and RequestedDateTime2 not like '') , datediff(mi, CompletedDateTime1, RequestedDateTime2), 0) AS [Discharge Ev 1],
IIf ((EventDisplay1 = 'Change ED to ETU') and (RequestedDateTime2 like null) , datediff(mi, CompletedDateTime1, [Depart Date & Time1]), 0) AS [Change ED to ETU - DepartTime Ev 1],
IIf ((EventDisplay1 = 'Admit - Bed Ordered') and (RequestedDateTime2 like null) , datediff(mi, CompletedDateTime1, [Depart Date & Time1]), 0) AS [Admit - Bed Ordered - DepartTime Ev 1],
IIf ((EventDisplay1 = 'Discharge') and  (RequestedDateTime2 like null), datediff(mi, CompletedDateTime1, [Depart Date & Time1]), 0) AS [Discharge- DepartTime Ev 1],

IIf ((EventDisplay2 = 'Change ED to ETU') and  (CompletedDateTime2 not like '' and RequestedDateTime3 not like '') , datediff(mi, CompletedDateTime2, RequestedDateTime3), 0) AS [Change ED to ETU Ev 2],
IIf ((EventDisplay2 = 'Admit - Bed Ordered') and  (CompletedDateTime2 not like '' and RequestedDateTime3 not like '') , datediff(mi, CompletedDateTime2, RequestedDateTime3), 0) AS [Admit - Bed Ordered Ev 2],
IIf ((EventDisplay2 = 'Discharge') and  (CompletedDateTime2 not like '' and RequestedDateTime3 not like '') , datediff(mi, CompletedDateTime2, RequestedDateTime3), 0) AS [Discharge Ev 2],
IIf ((EventDisplay2 = 'Change ED to ETU') and (RequestedDateTime3 like null) , datediff(mi, CompletedDateTime2, [Depart Date & Time1]), 0) AS [Change ED to ETU - DepartTime Ev 2],
IIf ((EventDisplay2 = 'Admit - Bed Ordered') and (RequestedDateTime3 like null) , datediff(mi, CompletedDateTime2, [Depart Date & Time1]), 0) AS [Admit - Bed Ordered - DepartTime Ev 2],
IIf ((EventDisplay2 = 'Discharge') and  (RequestedDateTime3 like null), datediff(mi, CompletedDateTime2, [Depart Date & Time1]), 0) AS [Discharge- DepartTime Ev 2],

IIf ((EventDisplay3 = 'Change ED to ETU') and  (CompletedDateTime3 not like '' and RequestedDateTime4 not like '') , datediff(mi, CompletedDateTime3, RequestedDateTime4), 0) AS [Change ED to ETU Ev 3],
IIf ((EventDisplay3 = 'Admit - Bed Ordered') and  (CompletedDateTime3 not like '' and RequestedDateTime4 not like '') , datediff(mi, CompletedDateTime3, RequestedDateTime4), 0) AS [Admit - Bed Ordered Ev 3],
IIf ((EventDisplay3 = 'Discharge') and  (CompletedDateTime3 not like '' and RequestedDateTime4 not like '') , datediff(mi, CompletedDateTime3, RequestedDateTime4), 0) AS [Discharge Ev 3],
IIf ((EventDisplay3 = 'Change ED to ETU') and (RequestedDateTime4 like null) , datediff(mi, CompletedDateTime3, [Depart Date & Time1]), 0) AS [Change ED to ETU - DepartTime Ev 3],
IIf ((EventDisplay3 = 'Admit - Bed Ordered') and (RequestedDateTime4 like null) , datediff(mi, CompletedDateTime3, [Depart Date & Time1]), 0) AS [Admit - Bed Ordered - DepartTime Ev 3],
IIf ((EventDisplay3 = 'Discharge') and  (RequestedDateTime4 like null), datediff(mi, CompletedDateTime3, [Depart Date & Time1]), 0) AS [Discharge- DepartTime Ev 3]


FROM
(
Select [Financial Number], 
       MAX(CASE DT WHEN 1 THEN [Depart Date & Time] END) AS [Depart Date & Time1],
       MAX(CASE RB WHEN 1 THEN [Event 1 Display] END) AS EventDisplay1,
       MAX(CASE RB WHEN 2 THEN [Event 1 Display] END) AS EventDisplay2,
       MAX(CASE RB WHEN 3 THEN [Event 1 Display] END) AS EventDisplay3,
       MAX(CASE RB WHEN 4 THEN [Event 1 Display] END) AS EventDisplay4,
       MAX(CASE RB WHEN 5 THEN [Event 1 Display] END) AS EventDisplay5,
       MAX(CASE RB WHEN 6 THEN [Event 1 Display] END) AS EventDisplay6,
       MAX(CASE RO WHEN 1 THEN [Event 1 Personnel - Completed] END) AS EventPersonnelCompleted1,
       MAX(CASE RO WHEN 2 THEN [Event 1 Personnel - Completed] END) AS EventPersonnelCompleted2,
       MAX(CASE RO WHEN 3 THEN [Event 1 Personnel - Completed] END) AS EventPersonnelCompleted3,
       MAX(CASE RO WHEN 4 THEN [Event 1 Personnel - Completed] END) AS EventPersonnelCompleted4,
       MAX(CASE RO WHEN 5 THEN [Event 1 Personnel - Completed] END) AS EventPersonnelCompleted5,
       MAX(CASE RO WHEN 6 THEN [Event 1 Personnel - Completed] END) AS EventPersonnelCompleted6,
       MAX(CASE RN WHEN 1 THEN [Requested 1 Date & Time] END) AS RequestedDateTime1,
       MAX(CASE RN WHEN 2 THEN [Requested 1 Date & Time] END) AS RequestedDateTime2,
       MAX(CASE RN WHEN 3 THEN [Requested 1 Date & Time] END) AS RequestedDateTime3,
       MAX(CASE RN WHEN 4 THEN [Requested 1 Date & Time] END) AS RequestedDateTime4,
       MAX(CASE RN WHEN 5 THEN [Requested 1 Date & Time] END) AS RequestedDateTime5,
       MAX(CASE RM WHEN 6 THEN [Requested 1 Date & Time] END) AS RequestedDateTime6,
       MAX(CASE RM WHEN 1 THEN [Completed 1 Date & Time] END) AS CompletedDateTime1,
       MAX(CASE RM WHEN 2 THEN [Completed 1 Date & Time] END) AS CompletedDateTime2,
       MAX(CASE RM WHEN 3 THEN [Completed 1 Date & Time] END) AS CompletedDateTime3,
       MAX(CASE RM WHEN 4 THEN [Completed 1 Date & Time] END) AS CompletedDateTime4,
       MAX(CASE RM WHEN 5 THEN [Completed 1 Date & Time] END) AS CompletedDateTime5,
       MAX(CASE RM WHEN 6 THEN [Completed 1 Date & Time] END) AS CompletedDateTime6
FROM
(

    SELECT [Financial Number], [Depart Date & Time], 
                 (Cast(Year([Arrival Date & Time]) as nvarchar) +'-'+ Cast (Month([Arrival Date & Time]) as nvarchar)) as Yr_Mon,
                 [Event 1 Display],
                 [Event 1 Personnel - Completed],
                 [Requested 1 Date & Time],
                 [Completed 1 Date & Time],
                 ROW_NUMBER() OVER (PARTITION BY [Financial Number] ORDER BY [Event 1 Display]) AS RB,
                 ROW_NUMBER() OVER (PARTITION BY [Financial Number] ORDER BY [Requested 1 Date & Time]) AS RN,
                 ROW_NUMBER() OVER (PARTITION by [Financial Number] ORDER BY [Completed 1 Date & Time]) AS RM,
                 ROW_NUMBER() OVER (PARTITION by [Financial Number] ORDER BY [Event 1 Personnel - Completed]) AS RO,
                 ROW_NUMBER() OVER (PARTITION by [Financial Number] ORDER BY [Arrival Date & Time]) as AD,
                 ROW_NUMBER() OVER (PARTITION by [Financial Number] ORDER BY [Depart Date & Time]) as DT
FROM [ED_Dispo_Events_Using_Event_1 Triage Level 3] AS ED
    )sub group by [Financial Number]  
    )sub 
    )sub group by [Financial Number], EventDisplay1, EventDisplay2, EventDisplay3, RequestedDateTime1, RequestedDateTime2, RequestedDateTime3, CompletedDateTime1, CompletedDateTime2, CompletedDateTime3, [Depart Date & Time1]
         order by [Financial Number]

这段代码产生的结果如下:

 FIN    [Change ED to ETU]   [Change ED to ETU - DepartTime]
  1          45                            0
  2          0                            34
  3          23                            0

所需的输出:

 FIN          Change ED to ETU FINAL
  1                    45
  2                    34
  3                    23

0 个答案:

没有答案