创建的串联列变量的更改顺序

时间:2019-05-30 16:28:31

标签: sql-server

这是我的问题-有没有一种方法可以编辑我的代码,以使查询1的RequestedDateTime1时间与查询1的对应EventDisplay1时间保持一致?

下面是我对这个问题的看法。

我有以下代码:

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

产生的结果如下所示(出于空间原因,此缩写):

[Financial Number] [Depart Date & Time] .. EventDisplay1 RequestedDateTime1
      1              2018-01-01 10:19:11     EP Exam     2018-01-01 11:19 
      1              2018-01-01 10:19:11     Discharge   2018-01-01 12:20
      2              2018-01-01 13:49:11     EP Exam     2018-01-01 12:20

然后我将查询添加到1产生的数据查询中:

Select [Financial Number], 
   MIN(CASE DT WHEN 1 THEN [Depart Date & Time] END) AS [Depart Date & 
   Time1],
   MIN(CASE RB WHEN 1 THEN [Event 1 Display] END) AS EventDisplay1,
   MIN(CASE RB WHEN 2 THEN [Event 1 Display] END) AS EventDisplay2,
   MIN(CASE RB WHEN 3 THEN [Event 1 Display] END) AS EventDisplay3,
   MIN(CASE RO WHEN 1 THEN [Event 1 Personnel - Completed] END) AS 
   EventPersonnelCompleted1,
   MIN(CASE RO WHEN 2 THEN [Event 1 Personnel - Completed] END) AS 
   EventPersonnelCompleted2,
   MIN(CASE RO WHEN 3 THEN [Event 1 Personnel - Completed] END) AS 
   EventPersonnelCompleted3,
   MIN(CASE RN WHEN 1 THEN [Requested 1 Date & Time] END) AS RequestedDateTime1,
   MIN(CASE RN WHEN 2 THEN [Requested 1 Date & Time] END) AS RequestedDateTime2,
   MIN(CASE RN WHEN 3 THEN [Requested 1 Date & Time] END) AS RequestedDateTime3,
   MIN(CASE RM WHEN 1 THEN [Completed 1 Date & Time] END) AS CompletedDateTime1,
   MIN(CASE RM WHEN 2 THEN [Completed 1 Date & Time] END) AS CompletedDateTime2,
   MIN(CASE RM WHEN 3 THEN [Completed 1 Date & Time] END) AS CompletedDateTime3,

  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] 

此查询产生的结果将每一行连接起来,因此每一行都是一个唯一的财务编号;但是,这样做会使RequestedDateTime1变量与EventDisplay1变量的对齐混乱。例如,使用上述示例数据,与DISCHARGE对齐的2018-01-01 12:20的RequestedDateTime1现在与EP EXAM对齐了。我相信这是因为代码按字母顺序对新创建的EventDisplay1,EventDisplay2,EventDisplay3变量进行排序。

示例结果不正确,因为Discharge RequestedDateTime1与EP RequestedDateTime1对齐:

 [Financial Number]   [Depart Date & Time]  EventDisplay1    EventDisplay2 RequestedDateTime1 RequestedDateTime2
        1             2018-01-01 10:19:11    Discharge         EP Exam 2018-01-01 11:19  2018-01-01 12:20

长话短说,有没有一种方法可以编辑我的代码,以使查询1的RequestedDateTime1时间与查询1的对应EventDisplay1时间保持一致?

所需结果:

   [Financial Number]   [Depart Date & Time]  EventDisplay1    EventDisplay2 RequestedDateTime1 RequestedDateTime2
        1             2018-01-01 10:19:11     EP Exam        Discharge     2018-01-01 11:19  2018-01-01 12:20

2 个答案:

答案 0 :(得分:0)

您对行号感到困惑。您要做的是有一列排序-单个ROW_NUMBER语句将为该行提供一个数字。然后,您想在此行= 1时提取所有值,在此行= 2时提取所有值,以此类推...这样,您的数据就有意义了。 rowNumber1中的所有值将显示为EventDisplay1,RequestedDateTime1等。

更新的查询:

Select [Financial Number], 
   MIN(CASE DT WHEN 1 THEN [Depart Date & Time] END) AS [Depart Date & 
   Time1],
   MIN(CASE RN WHEN 1 THEN [Event 1 Display] END) AS EventDisplay1,
   MIN(CASE RN WHEN 2 THEN [Event 1 Display] END) AS EventDisplay2,
   MIN(CASE RN WHEN 3 THEN [Event 1 Display] END) AS EventDisplay3,
   MIN(CASE RN WHEN 1 THEN [Event 1 Personnel - Completed] END) AS 
   EventPersonnelCompleted1,
   MIN(CASE RN WHEN 2 THEN [Event 1 Personnel - Completed] END) AS 
   EventPersonnelCompleted2,
   MIN(CASE RN WHEN 3 THEN [Event 1 Personnel - Completed] END) AS 
   EventPersonnelCompleted3,
   MIN(CASE RN WHEN 1 THEN [Requested 1 Date & Time] END) AS RequestedDateTime1,
   MIN(CASE RN WHEN 2 THEN [Requested 1 Date & Time] END) AS RequestedDateTime2,
   MIN(CASE RN WHEN 3 THEN [Requested 1 Date & Time] END) AS RequestedDateTime3,
   MIN(CASE RN WHEN 1 THEN [Completed 1 Date & Time] END) AS CompletedDateTime1,
   MIN(CASE RN WHEN 2 THEN [Completed 1 Date & Time] END) AS CompletedDateTime2,
   MIN(CASE RN WHEN 3 THEN [Completed 1 Date & Time] END) AS CompletedDateTime3,

  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 RN --choose whichever column you want to determine ordering here...
  FROM [ED_Dispo_Events_Using_Event_1 Triage Level 3] AS ED
   )sub group by [Financial Number] 

答案 1 :(得分:0)

问题是您没有考虑NULL,而您正在使用MIN,而不是max。 如果希望能够为null提供有效值,则将值强制转换为字符串:

替换看起来像这样的每一行

MIN(CASE RB WHEN 1 THEN [Event 1 Display] END) AS EventDisplay1,

带有类似这样的内容

COALESCE(CAST(MAX(CASE WHERE RB = 1 THEN [Event 1 Display] ELSE NULL END) AS VARCHAR(20)),
         '    -- NO VALUE --  ') AS EventDisplay1,