2017-12-13 15 views
-2

こんにちは、IDフィールドでN個の値がXの値に等しい場合、「見つける」方法がありますか?ここで少なくともN個のレコードがxと等しい合計

+0

[how-to-ask](https://stackoverflow.com/help/how-to-ask)を最初にお読みください。 –

+0

あなたの質問が正確に答えるほど詳細でない場合は、広範な「一般的な」回答しか得られません –

+0

申し訳ありませんが、2つの列と多分多くの方法しかないので質問が十分に分かりました。 – BernardL

答えて

1

は、3つの可能な方法があります。

サブクエリ

select t.*, g.n 
from yourtable t 
inner join (
    select id, count(*) as n from yourtable y 
    where y.col = 'N' 
    group by id 
    ) g on t.id = g.id 
where g.n >= 5 

別によって

select * 
from (
     select t.* 
     , count(case when t.col='N' then 1 end) over(partition by t.id) as count_n 
     from yourtable t 
    ) d 
where count_n >= 5 

以上の伝統的なグループは、この

select t.*, oa.n 
from yourtable t 
inner join lateral (
    select count(*) as n from yourtable y 
    where y.id = t.id 
    and y.col = 'N' 
    ) oa on true 
where oa.n >= 5 

SQLFiddle Demoのように "内側の横の参加" を使用することです

関連する問題