2017-03-24 86 views
0

我不確定是否可以使用TSQL實現此方案。我有一個表叫WorkingDays,有該信息SQL - 加入條件列選擇

ID | EmployeeId | Monday | Tuesday | Wednesday | Thursday | Friday 
---------------------------------------------------------------------- 
1 | 1   | 2  | 2  | 3   | 6  | 5 
2 | 2   | 1  | 7  | 5   | 2  | 3 

的日子列商店WorkingSchedule表的IDS,它有這個列:

ID   int Primary Key 
StartTime time 
EndTime  time 

所以我需要什麼ID獲得的開始時間和結束時間一名僱員取決於當前日期。

我需要從查詢得到的是開始和結束時間,具體取決於一天。我要過濾的日期是當前日期(使用getdate()函數)

因此,需要選擇正確的日期列名稱以進行連接。

我該如何實現這種情況?

+1

如果您可以根據上述數據提供樣本輸出 –

+0

這將是非常有用的工作日表中的id是什麼。如果是該表的ID,那麼同一個員工如何能夠包含兩個不同的workingschedule ID,我的意思是兩行相同的emp ID – Rams

+0

每行只有一個員工!每個工作日,每個員工都給了幾個小時。所以問題很明顯。我們也不需要輸出示例,因爲問題與加入的方式有關,而不是確切的結果。據我所知。 :) – IngoB

回答

1

動態SQL版本:

declare @sql nvarchar(max) =' 
select 
    t.EmployeeId 
    , StarTime = max(case when t.rn=1 then '+quotename(datename(weekday,getdate()))+' end) 
    , EndTime = max(case when t.rn=2 then '+quotename(datename(weekday,getdate()))+' end) 
from (
    select * 
    , rn = row_number() over (partition by t.EmployeeId order by t.Id) 
    from t 
    ) t 
group by t.EmployeeId;' 
exec sp_executesql @sql; 

rextester演示:http://rextester.com/WNH34961

回報:

+------------+----------+---------+ 
| EmployeeId | StarTime | EndTime | 
+------------+----------+---------+ 
|   1 |  5 |  3 | 
+------------+----------+---------+ 

根據您想要的輸出,這裏有其他兩種方式不要使用動態sql:

兩者均使用cross apply()來反轉數據,而WorkDay = datename(weekday,getdate())得到當前的WorkDay列。

一行輸出我們添加一些有條件的聚集:

/* one row per employeeId */ 
select 
    t.EmployeeId 
    , x.WorkDay 
    , StarTime = max(case when t.rn=1 then x.Time end) 
    , EndTime = max(case when t.rn=2 then x.Time end) 
from (
    select * 
    , rn = row_number() over (partition by t.EmployeeId order by t.Id) 
    from t 
    ) t 
    cross apply (values 
    ('Monday',Monday),('Tuesday',Tuesday),('Wednesday',Wednesday) 
    ,('Thursday',Thursday),('Friday',Friday) 
) x (WorkDay,Time) 
where WorkDay = datename(weekday,getdate()) 
group by t.EmployeeId, x.WorkDay 

回報:

+------------+---------+----------+---------+ 
| EmployeeId | WorkDay | StarTime | EndTime | 
+------------+---------+----------+---------+ 
|   1 | Friday |  5 |  3 | 
+------------+---------+----------+---------+ 

如果你想在兩行,像你的電流輸出的輸出

/* two rows per employeeId */ 
select 
    t.Id 
    , t.EmployeeId 
    , x.WorkDay 
    , t.StartEnd 
    , x.Time 
from (
    select * 
    , StartEnd = case 
     when row_number() over (partition by t.EmployeeId order by t.Id) = 1 
      then 'StartTime' 
     else 'EndTime' 
     end 
    from t 
    ) t 
    cross apply (values 
    ('Monday',Monday),('Tuesday',Tuesday),('Wednesday',Wednesday) 
    ,('Thursday',Thursday),('Friday',Friday) 
) x (WorkDay,Time) 
where WorkDay = datename(weekday,getdate()); 

回報:

+----+------------+---------+-----------+------+ 
| Id | EmployeeId | WorkDay | StartEnd | Time | 
+----+------------+---------+-----------+------+ 
| 1 |   1 | Friday | StartTime | 5 | 
| 2 |   1 | Friday | EndTime | 3 | 
+----+------------+---------+-----------+------+ 
+0

好吧,我多次閱讀這個問題,仍然沒有線索+1 –

0
select wd.Employee, ws.StartTime, ws.EndTime 
    from WorkingDays wd 
    join WorkingSchedule ws on ws.Id = case datename(weekday, getdate()) 
             when 'Monday' then ws.Monday 
             when 'Tuesday' then ws.Tuesday 
             when 'Wednesday' then ws.Wednesday 
             when 'Thursday' then ws.Thursday 
             when 'Friday' then ws.Friday 
             else 0 
            end 

提示:datename(weekday, getdate())回報你在當前區域星期幾的名字!這可能會更好:

select wd.Employee, ws.StartTime, ws.EndTime 
    from WorkingDays wd 
    join WorkingSchedule ws on ws.Id = case datepart(weekday, getdate()) 
             when 1 then wd.Monday 
             when 2 then wd.Tuesday 
             when 3 then wd.Wednesday 
             when 4 then wd.Thursday 
             when 5 then wd.Friday 
             else  0 
            end 

但你必須檢查哪些天是第一週(0,1),根據您的設置。