SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help On Query


Help On Query

Author
Message
vignesh.ms
vignesh.ms
SSC Eights!
SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)

Group: General Forum Members
Points: 842 Visits: 516
create table Sample
(Name Varchar(100),
Role Varchar(10))

insert into Sample values ('Vignesh' , 'Admin')
insert into Sample values ('Vignesh' , 'User')

insert into Sample values ('Bala' , 'Admin')
insert into Sample values ('Bala' , 'User')

insert into Sample values ('Suresh' , 'Admin')
insert into Sample values ('Arun' , 'User')


1. In sample table there were 4 names Vignesh, Bala, arun & suresh
2. There are 2 kinds of role (admin & user)
3. Vignesh & bala have both the roles , arun & suresh have any one of the role

I need to find who are having both roles ..

Kindly help..
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63610 Visits: 13298
This should do the trick:


SELECT Name
FROM SAMPLE
GROUP BY Name
HAVING COUNT(*) > 1;




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Dennis Post
Dennis Post
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1009 Visits: 547
Koen's query is much easier, but here's my alternate query anyway.

WITH cteRoles
AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY Name ORDER BY NAME) RowNr,
Name,
Role
FROM Sample
)
SELECT Name
FROM cteRoles
WHERE RowNr = 2





For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions.

"Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort
Dennis Post
Dennis Post
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1009 Visits: 547
Double post after error.



For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions.

"Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort
HanShi
HanShi
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8740 Visits: 3718
If there are double rows (like the same NAME is entered twice both with the ROLE 'User') the solution of Koen will give false results. The code below will just display the results where a NAME is entered only once as 'Admin' and once as 'User'.
select name
from Sample
group by name
having sum(case when ROLE = 'Admin' then 1 else 0 end) = 1
and sum(case when ROLE = 'User' then 1 else 0 end) = 1



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63610 Visits: 13298
HanShi (9/10/2013)
If there are double rows (like the same NAME is entered twice both with the ROLE 'User') the solution of Koen will give false results. The code below will just display the results where a NAME is entered only once as 'Admin' and once as 'User'.
select name
from Sample
group by name
having sum(case when ROLE = 'Admin' then 1 else 0 end) = 1
and sum(case when ROLE = 'User' then 1 else 0 end) = 1



Pffff, crap in crap out :-D

I believe this to be a more elegant solution:


SELECT Name
FROM [SAMPLE]
GROUP BY Name
HAVING COUNT(DISTINCT [Role]) > 1;




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
HanShi
HanShi
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8740 Visits: 3718
Koen Verbeeck (9/10/2013)

Pffff, crap in crap out :-D


I totally agree!! Cool
But with such a limited sample you better prepare for the worst. Or at least provide different solutions. The OP can pick the one that fits the best. And he's the only one who can decide which one that is.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
twin.devil
twin.devil
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5432 Visits: 2692
+1 to koen ... :-)
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42180 Visits: 20012
drop table #Sample
create table #Sample(Name Varchar(100),Role Varchar(10))
insert into #Sample values ('Vignesh' , 'Admin')
insert into #Sample values ('Vignesh' , 'User')
insert into #Sample values ('Bala' , 'Admin')
insert into #Sample values ('Bala' , 'User')
insert into #Sample values ('Suresh' , 'Admin')
insert into #Sample values ('Arun' , 'User')

insert into #Sample values ('Suresh' , 'Admin')
insert into #Sample values ('Arun' , 'User')

-- user has two or more rows in the table - may be same role
SELECT Name
FROM #SAMPLE
GROUP BY Name
HAVING COUNT(*) > 1;

-- user has two or more rows in the table - may be same role
WITH cteRoles
AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY Name ORDER BY NAME) RowNr,
Name,
Role
FROM #Sample
)
SELECT Name
FROM cteRoles
WHERE RowNr = 2

-- user has any two or more roles
SELECT s.Name
FROM #Sample s
WHERE EXISTS (
SELECT 1
FROM #Sample i
WHERE i.Name = s.Name
AND i.[Role] <> s.[Role]
)

-- user has two or more roles, including both Admin and User
SELECT Name
FROM (
SELECT Name, [Role]
FROM #Sample s
WHERE [Role] IN ('Admin','User')
GROUP BY Name, [Role]
) d
GROUP BY Name
HAVING COUNT(*) > 1

-- user has two or more roles, including both Admin and User
SELECT s.Name
FROM #Sample s
WHERE EXISTS (
SELECT 1
FROM #Sample i
WHERE i.Name = s.Name
AND i.[Role] = 'User'
)
AND s.[Role] = 'Admin'



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
vignesh.ms
vignesh.ms
SSC Eights!
SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)SSC Eights! (842 reputation)

Group: General Forum Members
Points: 842 Visits: 516
Thanks everybody...

It helps me a lot .
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search