Click here to Skip to main content
15,890,381 members
Home / Discussions / Database
   

Database

 
Questionwhere clause in sql [modified] Pin
AndyInUK14-Dec-09 0:22
AndyInUK14-Dec-09 0:22 
AnswerRe: where clause in sql Pin
David Skelly14-Dec-09 0:34
David Skelly14-Dec-09 0:34 
GeneralRe: where clause in sql Pin
AndyInUK14-Dec-09 0:39
AndyInUK14-Dec-09 0:39 
AnswerRe: where clause in sql Pin
Luc Pattyn14-Dec-09 1:29
sitebuilderLuc Pattyn14-Dec-09 1:29 
GeneralRe: where clause in sql Pin
AndyInUK14-Dec-09 1:31
AndyInUK14-Dec-09 1:31 
GeneralRe: where clause in sql Pin
Eddy Vluggen14-Dec-09 1:40
professionalEddy Vluggen14-Dec-09 1:40 
GeneralRe: where clause in sql Pin
Luc Pattyn14-Dec-09 1:59
sitebuilderLuc Pattyn14-Dec-09 1:59 
GeneralRe: where clause in sql Pin
Niladri_Biswas14-Dec-09 18:12
Niladri_Biswas14-Dec-09 18:12 
Take this example

declare @t table(username varchar(50),userid varchar(100))
insert into @t 
	select 'username1','a' union all select 'username1','b' union all
	select 'username1','c' union all select 'username1','d' union all
	select 'username2','a' union all select 'username3','b' union all
	select 'username3','c' union all select 'username3','d' union all
	select 'username4','a' union all select 'username4','b' union all
	select 'username4','c' union all select 'username5','d' union all
	select 'username6','a' union all select 'username7','b' union all
	select 'username8','c' union all select 'username8','d' 


username	userid
username1	a
username1	b
username1	c
username1	d
username2	a
username3	b
username3	c
username3	d
username4	a
username4	b
username4	c
username5	d
username6	a
username7	b
username8	c
username8	d


I want to find the users who has userid only 'a'. So in this case the desired output will be username2 and username6

Query 1:

select username from  @t
except
select username from @t
where  userid in('b','c','d')


Query 2:

select t1.username from  @t t1
left join (select username from @t where  userid in('b','c','d')) x
on x.username = t1.username
where x.username is null


Query 3:

select username from (
select username, 

	stuff((select ',' + CAST(t2.userid as varchar(max)) from @t t2
	where t2.username = t1.username for xml path('')),1,1,'') as userids
from @t t1
group by username )X
where X.userids = 'a'


In all the 3 cases the output is

username
username2
username6


Hope this helps
Smile | :)

Niladri Biswas

QuestionDSn Creation through SQLConfigDataSource() funciton Pin
Durga_Devi14-Dec-09 0:19
Durga_Devi14-Dec-09 0:19 
QuestionHow to identify the ID of row which has been modified? Pin
Member 470414312-Dec-09 13:26
Member 470414312-Dec-09 13:26 
AnswerRe: How to identify the ID of row which has been modified? Pin
dan!sh 12-Dec-09 18:49
professional dan!sh 12-Dec-09 18:49 
AnswerRe: How to identify the ID of row which has been modified? Pin
Mycroft Holmes13-Dec-09 13:44
professionalMycroft Holmes13-Dec-09 13:44 
AnswerRe: How to identify the ID of row which has been modified? Pin
Member 450194017-Dec-09 8:11
Member 450194017-Dec-09 8:11 
QuestionInvalid Object Name : 'sysdatabases' Pin
VikashGohil11-Dec-09 20:11
VikashGohil11-Dec-09 20:11 
AnswerRe: Invalid Object Name : 'sysdatabases' Pin
Mycroft Holmes11-Dec-09 23:17
professionalMycroft Holmes11-Dec-09 23:17 
GeneralRe: Invalid Object Name : 'sysdatabases' Pin
VikashGohil13-Dec-09 18:47
VikashGohil13-Dec-09 18:47 
GeneralRe: Invalid Object Name : 'sysdatabases' Pin
Mycroft Holmes13-Dec-09 19:04
professionalMycroft Holmes13-Dec-09 19:04 
AnswerRe: Invalid Object Name : 'sysdatabases' Pin
Rob Philpott13-Dec-09 23:14
Rob Philpott13-Dec-09 23:14 
QuestionDeny users to change data through backend Pin
v1i9n6o7d11-Dec-09 19:06
v1i9n6o7d11-Dec-09 19:06 
AnswerRe: Deny users to change data through backend Pin
The Man from U.N.C.L.E.11-Dec-09 23:21
The Man from U.N.C.L.E.11-Dec-09 23:21 
GeneralRe: Deny users to change data through backend Pin
v1i9n6o7d12-Dec-09 1:07
v1i9n6o7d12-Dec-09 1:07 
GeneralRe: Deny users to change data through backend Pin
Eddy Vluggen12-Dec-09 4:34
professionalEddy Vluggen12-Dec-09 4:34 
AnswerRe: Deny users to change data through backend Pin
Mycroft Holmes11-Dec-09 23:22
professionalMycroft Holmes11-Dec-09 23:22 
QuestionLocking and Transaction in SQL Server Pin
sandeepranjan11-Dec-09 15:12
sandeepranjan11-Dec-09 15:12 
AnswerRe: Locking and Transaction in SQL Server Pin
The Man from U.N.C.L.E.11-Dec-09 23:15
The Man from U.N.C.L.E.11-Dec-09 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.