TransWikia.com

Отсеять значения столбца, которые содержат одно из нескольких значений другого столбца

Stack Overflow на русском Asked by Sergey Kuznetsov on December 19, 2021

Примерная структура данных таблицы:

|   object_id   | |   risk_id |
|    111111     | |    23     |
|    111111     | |    45     |
|    111111     | |    543    |
|    222222     | |    45     |
|    222222     | |    543    |
|    333333     | |    45     |
|    333333     | |    543    |
|    333333     | |    123    |

Необходимо оставить уникальные object_id, которые не содержат в столбце risk_id значение 23.

Пример результата:

|   object_id   |
|    222222     |
|    333333     |

2 Answers

select object_id
  from table
 group by object_id
having sum(risk_id = 23) = 0

Группируем по obect_id в having суммируем записи с указанным risk_id.

Answered by Mike on December 19, 2021

SELECT
    object_id
FROM
    table_name
WHERE
    object_id NOT IN (
        SELECT
            object_id
        FROM
            table_name
        WHERE
            risk_id = 23
    )
GROUP BY
    object_id

Answered by Alexandr on December 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP