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
Old Hand
Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)

Group: General Forum Members
Points: 386 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
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28415 Visits: 13268
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
SSC-Addicted
SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)

Group: General Forum Members
Points: 407 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
SSC-Addicted
SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)

Group: General Forum Members
Points: 407 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
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4526 Visits: 3672
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
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28415 Visits: 13268
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
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4526 Visits: 3672
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
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2469 Visits: 2675
+1 to koen ... :-)
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17070 Visits: 19557
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
Old Hand
Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)Old Hand (386 reputation)

Group: General Forum Members
Points: 386 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