2016-10-04 56 views
0

enter image description here我的SQL查詢沒有得到desier結果

這些是我的出勤表checktype 0是用作銀泰和1爲退房時間, 我想時間,銀泰爲checktype = 0和時間outtime爲checktype = 1 與由銀泰所有EMPID順序單個條目根據日期將爲checktype 0是升序 和降序對checktype 1

我想下面類型的結果

enter image description here

回答

1

這裏有一個方法來選擇每天每個員工的最小銀泰和最大outtime:

select min(case when checktype = 0 then time end) min_intime, 
    max(case when checktype = 1 then time end) max_outtime, 
    empid, 
    date 
from mytable 
group by empid, date 
+0

謝謝很多兄弟。 –

1

我們可以使用OUTER APPLY在這種情況下做出不同組的選擇:

select 
    min(t.time) inTime, 
    max(t1.time) outTime, 
    t.empid, 
    t.date 
from table1 t 
outer apply(select time from table1 t1 where t.empid = t1.empid 
      and t1.checktype = 1) t1 
where t.checktype = 0 
group by t.empid, t.date 
+0

我試着用上面的查詢它給了我結果,但是如果特定的日子沒有intime並且出現時間存在,那麼使用上述查詢邏輯將不會在最終結果中顯示條目。 –