如何在嵌套查询中插入左连接?

时间:2018-02-13 15:44:27

标签: sql sql-server tsql

首先,我要感谢帮助这个复杂而困难的查询的朋友们。

我有三张桌子

表1

 StaffId     FirstName       LastName   staffType
---------------------------------------
   1          Adam            Sorme      Student 
   2          Lara            Sandra     Teacher
   3          Jack            Jones      Student

表2

 GateId   GateName  
 ---------------------------------------
   1        frontDoor
   2        superDoor

表3

Id transitionDate     GateId  StaffId 
 ---------------------------------------
1  2018-01-1 08:00:00    1     1
2  2018-01-1 10:00:00    2     1
3  2018-01-1 20:00:00    2     1
4  2018-01-2 07:00:00    1     2
5  2018-01-2 10:00:00    1     3
6  2018-01-9 12:00:00    2     2

我想要每天的学生的第一次和最后一次动作。如果指定日期之间没有可用的移动,则必须将值设置为null

transitionDate> '2018-01-1 00:00:00 000' 
 and transitionDate< '2018-01-03 00:00:00 000'

输出:

  Id     Date    MinTransitionDate    MaxTransitionDate    FirstGateName LastGateName    StaffId    StaffType
  1   2018-01-01  2018-01-1 08:00:00 2018-01-1 20:00:00    frontDoor      superDoor         1         Student
  2   2018-01-01  null                null                  null           null             3         student
  3   2018-01-02  null                null                  null           null             1         student
  4   2018-01-02  2018-01-2 10:00:00  null                 frontDoor       null             3         student

以下查询部分有效。

select s.staffId, d.dte,
       min(t.transitionDate) as first_change,
       max(t.transitionDate) as first_change,
       max(case when seqnum_asc = 1 then gateId end) as first_gateid,
       max(case when seqnum_desc = 1 then gateId end) as last_gateid
from (select s.* from Staff s where stafftype = 'Student') s cross join
     (select distinct cast(transitionDate as date) as dte from Transitions) d left join
     (select t.*,
             row_number() over (partition by StaffId, cast(transitionDate as date) order by transitionDate) as seqnum_asc,
             row_number() over (partition by StaffId, cast(transitionDate as date) order by transitionDate desc) as seqnum_desc
      from Transitions t
     ) t
     on cast(t.transitiondate as date) = d.dte and
        t.staffId = s.staffId and
        1 in (t.seqnum_asc, t.seqnum_desc)
group by s.staffId, d.dte;

Here是一个SQL小提琴。

如何将firstGateName和LastGateName添加到此查询结果中?

1 个答案:

答案 0 :(得分:0)

您可以将现有查询加入Gates表,以获取这些名称,即

<existing query>
inner join Gates g1 on g1.gateId = (required gate id)

在您的情况下,您可以使用您已经

的汇总值加入
select 
    q.*, 
    g1.GateName as first_gate_name,
    g2.GateName as last_gate_name
from 
-- use existing query as a subquery, so we can easily use the first/last_gateid values
(
    select s.staffId, d.dte,
           min(t.transitionDate) as first_change,
           max(t.transitionDate) as last_change,
           max(case when seqnum_asc = 1 then gateId end) as first_gateid,
           max(case when seqnum_desc = 1 then gateId end) as last_gateid   
    from (select s.* from Staff s where stafftype = 'Student') s cross join
         (select distinct cast(transitionDate as date) as dte from Transitions) d left join
         (select t.*,
                 row_number() over (partition by StaffId, cast(transitionDate as date) order by transitionDate) as seqnum_asc,
                 row_number() over (partition by StaffId, cast(transitionDate as date) order by transitionDate desc) as seqnum_desc
          from Transitions t
         ) t
         on cast(t.transitiondate as date) = d.dte and
            t.staffId = s.staffId and
            1 in (t.seqnum_asc, t.seqnum_desc)
    group by s.staffId, d.dte
) q
-- join on the appropriate gate ids      
inner join Gates g1 on g1.gateId = q.first_gateid
inner join Gates g2 on g2.gateId = q.last_gateid