如果某列的所有值都不同于某个字符串,则选择具有相同ID的行

时间:2018-07-10 21:30:55

标签: sql oracle

我正在尝试编写一个sql查询,但是我是该领域的新手,我需要帮助。我有三个表,并使用以下查询完成了2个内部联接:

select a1.SYS_ID, a1.U_JOURNEY_STATUS, b1.SYS_ID
     , b1.U_TICKET_JOURNEY_SYSID
     , b1.U_STATUS, c1.SYS_ID, c1.U_TICKET_JOURNEY_SYSID, c1.U_STATUS
  from NOCCIA_TICKET_JOURNEY_CONTROL a1 INNER JOIN NOCCIA_AUTOMATION_CONTROL b1
    on a1.SYS_ID=b1.U_TICKET_JOURNEY_SYSID 
 INNER JOIN NOCCIA_MANUAL_EX_CONTROL c1
    on b1.U_TICKET_JOURNEY_SYSID=c1.U_TICKET_JOURNEY_SYSID
 where ( ( a1.U_JOURNEY_STATUS='In Automation'
        OR a1.U_JOURNEY_STATUS='Alarms Cleared' ) ) 

结果如下:

enter image description here

SYS_ID是第一张表的主键,而U_TICKET_JOURNEY_SYS_ID和U_TICKET_JOURNEY_SYS_ID1是第二张表和第三张表的外键。但是,我真正想要的是仅选择所有行U_STATUS和U_STATUS_1与该SYS_ID的字符串s不同的SYS_ID。

3 个答案:

答案 0 :(得分:0)

您可以执行SELECT ... MINUS SELECT ... MINUS SELECT ... 第2个,第3个SELECT的结果集将从第一个结果集中删除。

答案 1 :(得分:0)

如果我已经理解了问题和评论,那么您只需添加一个条件:

select a1.SYS_ID, a1.U_JOURNEY_STATUS, b1.SYS_ID
     , b1.U_TICKET_JOURNEY_SYSID
     , b1.U_STATUS, c1.SYS_ID, c1.U_TICKET_JOURNEY_SYSID, c1.U_STATUS
  from NOCCIA_TICKET_JOURNEY_CONTROL a1 INNER JOIN NOCCIA_AUTOMATION_CONTROL b1
    on a1.SYS_ID=b1.U_TICKET_JOURNEY_SYSID 
 INNER JOIN NOCCIA_MANUAL_EX_CONTROL c1
    on b1.U_TICKET_JOURNEY_SYSID=c1.U_TICKET_JOURNEY_SYSID
 where ( a1.U_JOURNEY_STATUS='In Automation'
      OR a1.U_JOURNEY_STATUS='Alarms Cleared' )
   AND 'In Automation' NOT IN ( b1.U_STATUS, c1.U_STATUS );

答案 2 :(得分:0)

使用count()的解析版本。表data是您的查询。对于count(),由于sys_id的使用,partition扫描具有相同Case when的行。 with data(a_sys_id, a_status, b_sys_id, b_status, c_sys_id, c_status) as ( select 110, 'Auto', 4, 'Closed', 4, 'Closed' from dual union all select 110, 'Auto', 101, 'Auto', 4, 'Closed' from dual union all select 115, 'Auto', 17, 'Closed', 21, 'Open' from dual union all select 115, 'Auto', 21, 'Open', 21, 'Open' from dual union all select 169, 'Auto', 32, 'Closed', 33, 'Closed' from dual union all select 169, 'Auto', 33, 'Open', 33, 'Open' from dual union all select 169, 'Auto', 44, 'Open', 33, 'Auto' from dual union all select 169, 'Auto', 47, 'Open', 33, 'Closed' from dual) select a_sys_id, a_status, b_sys_id, b_status, c_sys_id, c_status from ( select a_sys_id, a_status, b_sys_id, b_status, c_sys_id, c_status, count(case when a_status = b_status or a_status = c_status then 1 end) over (partition by a_sys_id) as cnt from data) where cnt = 0 子句检查状态是否相同。这样,对于每一行,我们将扫描整个组。外部查询会删除至少一个条目包含相同状态的组。

我在这里模拟了样本数据,其中只有sys_id 115满足条件:

  A_SYS_ID A_STATUS   B_SYS_ID B_STATUS   C_SYS_ID C_STATUS
---------- -------- ---------- -------- ---------- --------
       115 Auto             17 Closed           21 Open
       115 Auto             21 Open             21 Open

Count() documentation(请看分析示例),demo和结果:

 int mHour;
 int mMin;
 StringBuilder stringBuilder = new StringBuilder();

 TimePickerDialog timePickerDialog = new TimePickerDialog(context, new  
 TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            mHour = hourOfDay;
            mMin = minute;

            String am_pm ;

            Calendar datetime = Calendar.getInstance();
            datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            datetime.set(Calendar.MINUTE, minute);

            am_pm = getTime(mHour,mMin);

            stringBuilder.append(" ");
            stringBuilder.append(am_pm);
            editTextDate.setText(stringBuilder);

        }

    }, mHour, mMin, false);

    timePickerDialog.show();



 public String getTime(int hr, int min) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY,hr);
    cal.set(Calendar.MINUTE,min);
    Format formatter;
    formatter = new SimpleDateFormat("h:mm a");
    return formatter.format(cal.getTime());
}