Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get records with more than one value and at least one of them is zero

Create table #Tbl
(
ID int not null,
Keyword nvarchar(max)
)
Insert into #Tbl Values ('0','Cryptography')
Insert into #Tbl Values ('1','Cryptography')
Insert into #Tbl Values ('4','Cryptography')
Insert into #Tbl Values ('0','SQL')
Insert into #Tbl Values ('0','SQL')
Insert into #Tbl Values ('3','Cloud Computing')
Insert into #Tbl Values ('6','Recursion')
Insert into #Tbl Values ('8','Recursion')
Insert into #Tbl Values ('0','Universe')
Insert into #Tbl Values ('0','Universe')
Insert into #Tbl Values ('7','Universe')

I need to get the titles which has more than one ID and at least one of the ID is zero.

So the expected result will be:

Cryptography
Universe

I tried below query but not able to add "at least one id is zero" condition

select Keyword,COUNT(distinct id) from #Tbl
group by Keyword
having COUNT(distinct id)>1

How can I proceed here ? Thanks for your help.

like image 697
skjcyber Avatar asked Dec 27 '22 02:12

skjcyber


1 Answers

Assuming your IDs start from 0, the below should work

select Keyword,COUNT(distinct id) from #Tbl
group by Keyword
having COUNT(distinct id)>1 and MIN(id) = 0
like image 90
iruvar Avatar answered May 15 '23 02:05

iruvar