SQL - 选择仅匹配特定(多个)唯一条件的值

时间:2017-01-12 20:58:32

标签: sql sql-server

我有以下(示例)表:

+-------------+------------+----------+---------+
| alarminc_no | cust_name  | event_id | site_no |
+-------------+------------+----------+---------+
|       99999 | Jonh Smith | UYALR    |    9090 |
|       99999 | Jonh Smith | UYBR     |    9090 |
|       99999 | Jonh Smith | PAF      |         |
|       77777 | Allan      | UYBIC    |    9090 |
|       77777 | Allan      | UYBR     |    9090 |
|       77777 | Allan      | PAF      |    9090 |
|       88888 | Susan      | UYGR     |    9090 |
|       88888 | Susan      | UYLAR    |    9090 |
|       88888 | Susan      | PAF      |    9090 |
+-------------+------------+----------+---------+

我需要与以下event_id匹配的alarminc_no:

' PAF'' PFA'' USABA'' USALR'' USBUR'&# 39; USDUR'' USFIR'' USPAN'' USTAM&#39 ;, ' UYALR'' UYAVI'' UYBUR'' UYDIR'' UYDUR'' UYE73& #39;,' UYFIR'' UYMED&#39 ;, ' UYPAN'' UYTAM

还有很多其他的event_ids,我只需要包含这些event_id的alarminc_no数字,而不是其他的。 每个Alarminc_no都有多个event_ID,我需要每个包含至少一个这些值的内容,如果包含另一个也无关紧要,规则是:

ouptut中的每个AlarmInc_no都应该包含在event_id强制PAF,PFA和列表中的一些事件中(事件也是强制性的,至少其中一个必须存在),还可以有其他事件。

来自这个论坛的用户帮助我使用了这段代码,但是部分工作,它给了我很多我需要的alarminc_no,但有些遗失了,我不知道为什么。

SELECT *
FROM MYTABLE 
JOIN (
  SELECT DISTINCT alarminc_no, system_no
  FROM (
    SELECT alarminc_no,system_no,
       CASE WHEN event_id in
         ('PAF','PFA','USABA','USALR','USBUR','USDUR','USFIR','USPAN','USTAM',
          'UYALR','UYAVI','UYBUR','UYDIR','UYDUR','UYE73','UYFIR','UYMED',
          'UYPAN','UYTAM') THEN 0 ELSE 1 END AS BAD,
       CASE WHEN event_id in ('PAF','PFA') THEN 1 ELSE 0 END AS GOOD
    FROM MYTABLE
    where event_date between '01/12/2016' and '31/12/2018'
  ) X
  GROUP BY alarminc_no, system_no
  HAVING SUM(BAD) = 0 AND SUM(GOOD) > 0 AND COUNT(*) > 1
) X ON MYTABLE.alarmic_no = X.alarmic_no AND MYTABLE.system_no = x.system_no

这是输出的屏幕截图:

https://i.stack.imgur.com/Hp3ax.png

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

根据您的描述,脚本应排除'GOOD'部分。 HAVING语句仅对于在('PAF','PFA')中具有event_id记录的alarminc_no以及在较大组中具有event_id in的第二记录才成立。如果不需要('PAF','PFA'),您可以尝试以下修改后的脚本:

SELECT *
FROM MYTABLE 
JOIN (
  SELECT DISTINCT alarminc_no, system_no
  FROM (
    SELECT alarminc_no,system_no,
       CASE WHEN event_id in
         ('PAF','PFA','USABA','USALR','USBUR','USDUR','USFIR','USPAN','USTAM',
          'UYALR','UYAVI','UYBUR','UYDIR','UYDUR','UYE73','UYFIR','UYMED',
          'UYPAN','UYTAM') THEN 0 ELSE 1 END AS BAD--,
       --CASE WHEN event_id in ('PAF','PFA') THEN 1 ELSE 0 END AS GOOD
    FROM MYTABLE
    where event_date between '01/12/2016' and '31/12/2018'
  ) X
  GROUP BY alarminc_no, system_no
  HAVING SUM(BAD) = 0 /*AND SUM(GOOD) > 0*/ AND COUNT(*) > 1
) X ON MYTABLE.alarmic_no = X.alarmic_no AND MYTABLE.system_no = x.system_no

答案 1 :(得分:0)

获取记录:

SELECT *
FROM @your_table t
WHERE t.event_id IN ('PAF','PFA','USABA','USALR','USBUR','USDUR','USFIR','USPAN','USTAM','UYALR','UYAVI','UYBUR','UYDIR','UYDUR','UYE73','UYFIR','UYMED','UYPAN','UYTAM')
    AND NOT EXISTS (
        SELECT 1
        FROM @your_table tt
        WHERE t.alarmic_no = tt.alarmic_no
            AND tt.event_id NOT IN ('PAF','PFA','USABA','USALR','USBUR','USDUR','USFIR','USPAN','USTAM','UYALR','UYAVI','UYBUR','UYDIR','UYDUR','UYE73','UYFIR','UYMED','UYPAN','UYTAM')
        ) 

只获取alarminc_nos:

SELECT DISTINCT t.alarminsc_no
FROM @your_table t
WHERE t.event_id IN ('PAF','PFA','USABA','USALR','USBUR','USDUR','USFIR','USPAN','USTAM','UYALR','UYAVI','UYBUR','UYDIR','UYDUR','UYE73','UYFIR','UYMED','UYPAN','UYTAM')
    AND NOT EXISTS (
        SELECT 1
        FROM @your_table tt
        WHERE t.alarmic_no = tt.alarmic_no
            AND tt.event_id NOT IN ('PAF','PFA','USABA','USALR','USBUR','USDUR','USFIR','USPAN','USTAM','UYALR','UYAVI','UYBUR','UYDIR','UYDUR','UYE73','UYFIR','UYMED','UYPAN','UYTAM')
        )