【问题标题】:SQL query - trying to return records that contains results from previous records and the current recordsSQL 查询 - 尝试返回包含先前记录和当前记录的结果的记录
【发布时间】:2020-05-06 09:08:07
【问题描述】:

所以我的表(表 A)包含显示员工向谁报告的记录。 当一个人更改“报告至”时,它将更新表格并显示旧值和新值。我正在努力编写一个 SQL 查询来返回旧雇主和新雇主。

表A

-----------------------------------------------------------------------  
ID    |Date                           |Employee |Reports To  |Command
-----------------------------------------------------------------------   
1     |2020-05-05 09:32:56.193        |EMP0        |EMP1      OLD
-----------------------------------------------------------------------   
2     |2020-05-05 09:39:56.193        |EMP0        |EMP2      NEW
-----------------------------------------------------------------------   
3     |2020-05-05 11:32:56.193        |EMP0        |EMP2      OLD
-----------------------------------------------------------------------   
4     |2020-05-05 11:38:56.193        |EMP0        |EMP3      NEW
-----------------------------------------------------------------------   
5     |2020-05-05 19:32:56.193        |EMP0        |EMP3      OLD
 -----------------------------------------------------------------------   
6     |2020-05-05 19:35:56.193        |EMP0        |EMP2      NEW
------------------------------------------------------------------------   

我想返回的结果:

------------------------------------------------------------   
ID    |Employee |Currently Reports To |Previously Reported To
-------------------------------------------------------------  
1      EMP0         EMP2               EMP1  
-------------------------------------------------------------  
2      EMP0         EMP3               EMP2      
-------------------------------------------------------------
3      EMP0         EMP2               EMP3      
------------------------------------------------------------- 

我尝试过的 SQL 查询:

SELECT Employee, tb1.[Reports To] FROM Table A tb1 JOIN Table A tb2
ON tb1.Employee =tb2.Employee AND tb1.Reports To <> tb2.Reports To
AND tb2.Command = 'OLD'
WHERE tb1.Command = 'NEW'```

【问题讨论】:

  • 这个查询有什么问题? (除了应该是SELECT tb1.ID, tb1,Employee, tb1.[Reports To] , tb2.[Reports To] 的选择部分)

标签: sql sql-server tsql select


【解决方案1】:

如果“旧”和“新”记录正确交错,您可以使用lead() 或lag():

select
    employee,
    reports_to     as currently_reports_to,
    lag_reports_to as previously_reported_to
from (
    select t.*, lag(reports_to) over(partition by employee order by date, id) lag_reports_to
    from mytable t
) t
where command = 'NEW'

【讨论】:

  • @GMB 。 . .我认为如果您使用as 作为列别名,您的代码会更容易阅读。我也对这个解决方案犹豫不决,因为时间戳完全相同。这意味着结果不稳定。
  • @GordonLinoff:谢谢。我添加了第二个排序标准。
【解决方案2】:

你也可以使用apply:

select row_number() over (order by t.date) as id,
       t.employee, t.reportsto as CurrentlyReportsTo, t1.reportsto as PreviouslyReportsTo
from table t cross apply
     ( select top (1) t1.reportsto
       from tab;e t1
       where t1.employee = t.employee and t1.command = 'OLD' and t1.date <= t.date
       order by t1.date desc
     ) t1
where t.command = 'NEW';

【讨论】:

    【解决方案3】:

    您的行具有完全相同的时间戳。这也允许您使用聚合:

    select employee,
           max(case when command = 'NEW' then reports_to end) as current_reports_to,
           max(case when command = 'OLD' then reports_to end) as previously_reports_to
    from t
    group by employee, timestamp;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 2018-07-28
      • 2019-06-13
      • 2016-09-12
      相关资源
      最近更新 更多