【问题标题】:Oracle PL/SQL: Whether multiple records with different conditions exist in a tableOracle PL/SQL:表中是否存在多条不同条件的记录
【发布时间】:2016-03-19 07:14:03
【问题描述】:

这几天我一直在纠结这个问题。

我对 PL/SQL(以及一般的 SQL)还很陌生,我正在尝试确定一个列中是否存在多个条件,而不是单个记录中是否存在多个条件(这是 WHERE 语句检查的内容)。

例如,假设我有一张员工表及其预定的工作班次。假设我也有一张他们的加班报价表,如下所示(对不起所有的时间段;我花了一个小时试图弄清楚如何在没有它们的情况下格式化表格,然后放弃):

表“ScheduledShifts”

EmployeeID  ShiftID   ShiftDate  
1           1         3/20/16   
1           3         3/21/16    
1           1         3/22/16   
2           1         3/20/16  
2           1         3/21/16  
2           2         3/22/16  

表格“加班出价”

EmployeeID  ShiftID   ShiftDate  
1           4          3/21/16  
2           4          3/21/16  

我要做的是根据表格下方列出的规则用以下结果填充第三个表格:

表格“结果”

EmployeeID  ShiftID   ShiftDate   ApprovedYN  
1           4          3/21/16     N  
2           4          3/21/16     Y  

规则: 如果特定员工在特定日期对 4 班进行投标...
并且他们在该投标日期的前一天工作 1 班
并且他们在该投标日期的当天轮班 3
并且他们在投标日期后的第二天工作 1 班

然后在结果表中添加一个条目并用“N”填充“ApprovedYN”

否则,在结果表中添加一个条目并用“Y”填充“ApprovedYN”

AND 和 OR 似乎不起作用,因为它们将所有过滤器应用于每条记录,而不是将每个过滤器单独应用于表。

谢谢!

【问题讨论】:

  • (1) 使用示例数据和所需结果编辑您的问题。 (2) “Ideas on a general way ..” 是邀请某人结束问题(太宽泛?基于意见?)。决定一个特定的问题并描述它。然后有人可以帮助你。
  • 对不起,这是我第一次发帖,我还在努力学习如何格式化。我去清理一下

标签: sql oracle plsql


【解决方案1】:

试试:

select employeeID,ShiftID,ShiftDate,
       CASE WHEN EXISTS( 
                select * 
                from ScheduledShifts ss1
                join ScheduledShifts ss2 on ss1.EmployeeID = ss2.EmployeeID
                join ScheduledShifts ss3 on ss2.EmployeeID = ss3.EmployeeID
                where
                   ss1.EmployeeID = ob.EmployeeID
                   /* AND they are working shift 1 the day BEFORE that bid date */
                   AND ss1.ShiftID = 1 AND ss1.shiftdate = ob.shiftdate - 1
                   /* AND they are working shift 3 the day OF that bid date */
                   AND ss2.ShiftID = 3 AND ss2.shiftdate = ob.shiftdate
                   /* AND they are working shift 1 the day AFTER that bid date */
                   AND ss3.ShiftID = 1 AND ss3.shiftdate = ob.shiftdate + 1
              )
            /* Then add an entry to the results table 
               and populate "ApprovedYN" with an "N" */
            THEN 'N' 
            /* Otherwise, add an entry to the results table 
               and populate "ApprovedYN" with a "Y" */
            ELSE 'Y' 
      END As ApprovedYN
from  OvertimeBids ob;

【讨论】:

    猜你喜欢
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多