簡體   English   中英

查詢以查找組合的唯一值

[英]Query to find unique value for the combinations

我有兩個列ID_COL和VALUE_COL的表。

ID_COL    VALUE_OUT
======    =========
15          10
16          10
16          11
17          10
17          11
17          12
18          10
18          12
19          11
20          11
20          12
21          12

該表格根據我們的biz規則填充了一些組合。 現在我想找到如下輸出。

應用程序的輸入為11,12

我們需要在上表中進行搜索,並需要找到ID_COL。 在這種情況下,我需要從ID_COL返回值20(這是11,12個具有完全不同值的精確匹配項)

它不是兩個要匹配的值,有時它也可能是單個值..如果我通過12 ..我需要返回id_col 21

如果我正確地提出了您的問題,那么您想在* Id_COL *中找到滿足* VALUE_OUT *從應用程序傳遞的兩個輸入值的值。

您可以使用以下查詢:

    select ID_COL from table where value_out in (11,12);

希望我正確理解你的問題。

希望能幫助到你

維沙德

select ID_COL 
from table 
group by ID_COL 
having 
  count(0) = 2 and
  count(case when VALUE_OUT in (11,12) then 1 end) = 2

您可以使用一個聚合和一個having子句來做到這一點:

select id_col
from t
group by id_col
having sum(case when value_out = 11 then 1 else 0 end) > 0 and
       sum(case when value_out = 12 then 1 else 0 end) > 0 and
       sum(case when value_out not in (11, 12) then 1 else 0 end) = 0;

前兩個子句在having子句保證每個值是存在的。 第三個子句確保沒有其他值。

編輯:

我意識到,對於您的特殊情況,其中值是整數,您可以執行以下操作:

having min(value_out) = 11 and max(value_out) = 12;

要么

having min(value_out) = 11 and max(value_out) = 12 and count(distinct value_out) = 2;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM