久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在嵌套查詢中插入左連接?

How do I insert left joins in a nested query?(如何在嵌套查詢中插入左連接?)
本文介紹了如何在嵌套查詢中插入左連接?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

首先感謝幫助過這個復雜難查詢的朋友們.

First of all I would like to thank the friends who helped this complex and difficult query.

我有三張桌子

表一

 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

I want the first and last movements of students for each day. Value must be set to null if no movement is available between the specified dates

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;

這里是 SQL Fiddle.

Here is a SQL Fiddle.

如何將 firstGateName 和 LastGateName 添加到此查詢結果中?

推薦答案

您只需將現有查詢加入Gates 表即可獲取這些名稱,即

You can just join your existing query to the Gates table to get those names, i.e.

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

在您的情況下,您可以使用您擁有的總價值加入

In your case you can join using the aggregate value you've

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

這篇關于如何在嵌套查詢中插入左連接?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 欧美精品在线一区 | 久久国产婷婷国产香蕉 | 日韩三级免费观看 | 欧美v在线观看 | 在线观看中文字幕av | 天天人人精品 | 天天综合网7799精品 | 欧美在线一区二区三区 | 国产精品综合视频 | 久久午夜视频 | 亚洲欧美中文日韩在线v日本 | 国产精品一级在线观看 | 亚洲人在线观看视频 | 成人在线观看黄 | 在线播放亚洲 | 精品日韩在线 | 日韩在线视频观看 | 亚洲国产成人精品女人久久久 | 成人午夜av| 亚洲精品99 | 天天操综合网 | 亚州中文字幕 | 欧美成视频在线观看 | 成人欧美一区二区三区色青冈 | 台湾a级理论片在线观看 | 久久99精品国产自在现线小黄鸭 | 女人av | 99精品久久久久久中文字幕 | 中文av电影 | 美国一级片在线观看 | 欧美99| 欧美日韩中文在线 | 日韩爱爱网站 | 欧美一级在线观看 | 日韩欧美一区二区三区免费观看 | 高清成人免费视频 | 91热在线 | 日韩国产精品一区二区三区 | 91av视频在线 | 亚洲精品一区中文字幕乱码 | 国产福利网站 |