与日期和 ID 进行比较?

发布于 2024-08-06 07:48:04 字数 1137 浏览 3 评论 0原文

使用SQL Server 2000,

我想根据Table1.personid获取Table2.TimeIn Table2.TimeOut,如果Table1.Date = Table3.Date,那么它应该采用Table3.TimeIn,Table3.TimeOut。

3 个表

Table1

ID    Date  

001 20090503 
001 20090504 
001 20090506 
002 20090505 
002 20090506 

等等...,

Table2

ID    TimeIn TimeOut

001 08:00:00 18:00:00
002 08:00:00 21:00:00

等等...,

Table3

ID    Date  TimeIn TimeOut

001 20090504 10:00:00 22:00:00
001 20090505 17:00:00 23:00:00
002 20090505 12:00:00 21:00:00 

等等...,

Select Table1.ID, 
       Table1.Date, 
       Table2.TimeIn, 
       Table2.TimeOut 
  from Table1 
Inner Join Table2 on Table1.ID = Table2.ID 

如果 Table1.Date = Table3.Date 那么它应该采取 Table3.TimeIn,Table3.TimeOut 否则 Table2.TimeIn,Table2.Timeout

预期输出

ID Date TimeIn TimeOut

001 20090503 08:00:00 18:00:00
001 20090504 10:00:00 22:00:00 
001 20090506 08:00:00 18:00:00
002 20090505 12:00:00 21:00:00
002 20090506 08:00:00 21:00:00

等等...,

如何编写针对此条件的查询?

Using SQL Server 2000

I want to get Table2.TimeIn Table2.TimeOut according to Table1.personid and also If Table1.Date = Table3.Date then it should take a Table3.TimeIn, Table3.TimeOut.

3 Tables

Table1

ID    Date  

001 20090503 
001 20090504 
001 20090506 
002 20090505 
002 20090506 

So on…,

Table2

ID    TimeIn TimeOut

001 08:00:00 18:00:00
002 08:00:00 21:00:00

So on…,

Table3

ID    Date  TimeIn TimeOut

001 20090504 10:00:00 22:00:00
001 20090505 17:00:00 23:00:00
002 20090505 12:00:00 21:00:00 

So on…,

Select Table1.ID, 
       Table1.Date, 
       Table2.TimeIn, 
       Table2.TimeOut 
  from Table1 
Inner Join Table2 on Table1.ID = Table2.ID 

If Table1.Date = Table3.Date then it should take Table3.TimeIn, Table3.TimeOut else Table2.TimeIn, Table2.Timeout

Expected Output

ID Date TimeIn TimeOut

001 20090503 08:00:00 18:00:00
001 20090504 10:00:00 22:00:00 
001 20090506 08:00:00 18:00:00
002 20090505 12:00:00 21:00:00
002 20090506 08:00:00 21:00:00

So on…,

How to write a query for this condition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2024-08-13 07:48:04

员工时间表回退?:

SELECT Table1.ID
    ,Table1.Date
    ,COALESCE(Table3.TimeIn, Table2.TimeIn) AS TimeIn
    ,COALESCE(Table3.TimeOut, Table2.TimeOut) AS TimeOut
FROM Table1
INNER JOIN Table2 -- Always have an expected schedule for an employee
    ON Table1.ID = Table2.ID
LEFT JOIN Table3 -- May.may not have an actual schedule for an employee
    ON Table3.ID = Table1.ID
    AND Table3.Date = Table1.Date
/*
ORDER BY Table1.ID
    ,Table1.Date
*/

Employee time schedule fallback?:

SELECT Table1.ID
    ,Table1.Date
    ,COALESCE(Table3.TimeIn, Table2.TimeIn) AS TimeIn
    ,COALESCE(Table3.TimeOut, Table2.TimeOut) AS TimeOut
FROM Table1
INNER JOIN Table2 -- Always have an expected schedule for an employee
    ON Table1.ID = Table2.ID
LEFT JOIN Table3 -- May.may not have an actual schedule for an employee
    ON Table3.ID = Table1.ID
    AND Table3.Date = Table1.Date
/*
ORDER BY Table1.ID
    ,Table1.Date
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文