| Author |
Topic |
|
baburk
Posting Yak Master
108 Posts |
Posted - 2008-11-14 : 06:48:03
|
| Hi,How to check if both user name are available in a tableSELECT * FROM Users WHERE Users.UserName = 'babu' and Users.UserName = 'pythondave'gives me wrong result.since it checks both 'babu' and 'pythondave' avilable in each row.SELECT * FROM Users WHERE Users.UserName = 'babu' OR Users.UserName = 'pythondave'It satisfy if any one of them present.Help me to solve. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-14 : 07:29:27
|
| select UserName from UsersWHERE Users.UserName = 'babu' OR Users.UserName = 'pythondave'group by UserNamehaving count(distinct UserName)=2MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-14 : 07:32:00
|
quote: Originally posted by madhivanan select UserName from UsersWHERE Users.UserName = 'babu' OR Users.UserName = 'pythondave'group by UserNamehaving count(distinct UserName)=2MadhivananFailing to plan is Planning to fail
grouping by username and still taking count() of it? this will return only 1 always |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-14 : 07:32:49
|
| is it this what you wantSELECT * FROM Users WHERE Users.UserName IN( 'babu','pythondave') |
 |
|
|
baburk
Posting Yak Master
108 Posts |
Posted - 2008-11-14 : 07:36:07
|
| Hi,SELECT UserName FROM Users WHERE Users.UserName = 'babu' OR Users.UserName = 'pythondave'group by UserNamehaving count(distinct UserName) = 1It is not correct |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-14 : 07:36:34
|
or this?SELECT CASE WHEN COUNT(DISTINCT CASE WHEN Users.UserName = 'babu' OR Users.UserName = 'pythondave' THEN Users.UserName ELSE NULL END)=2 THEN 'Present' ELSE 'Not Present' END FROM Users |
 |
|
|
baburk
Posting Yak Master
108 Posts |
Posted - 2008-11-14 : 07:37:39
|
quote: Originally posted by visakh16 is it this what you wantSELECT * FROM Users WHERE Users.UserName IN( 'babu','pythondave')
I want to check both user must available.IN clause gives result if any one is available. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-14 : 07:38:33
|
quote: Originally posted by visakh16
quote: Originally posted by madhivanan select UserName from UsersWHERE Users.UserName = 'babu' OR Users.UserName = 'pythondave'group by UserNamehaving count(distinct UserName)=2MadhivananFailing to plan is Planning to fail
grouping by username and still taking count() of it? this will return only 1 always 
Yesdeclare @test table(Username varchar(100))insert into @test select 'test' union allselect 'babu' union allselect 'pythondave' select Username from( select UserName,count(*) over () as counting from @test where UserName = 'babu' OR UserName = 'pythondave') ast where counting=2 MadhivananFailing to plan is Planning to fail |
 |
|
|
baburk
Posting Yak Master
108 Posts |
Posted - 2008-11-14 : 07:42:58
|
| Hi madhivanan,Thaks for your reply.It worked well.This also workedSELECT @@rowcount, UserName FROM Users WHERE Users.UserName = 'babu' OR Users.UserName = 'pythondave'group by UserNamehaving @@rowcount = 2Bye. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-14 : 07:48:47
|
quote: Originally posted by baburk Hi madhivanan,Thaks for your reply.It worked well.This also workedSELECT @@rowcount, UserName FROM Users WHERE Users.UserName = 'babu' OR Users.UserName = 'pythondave'group by UserNamehaving @@rowcount = 2Bye.
@@ROWCOUNT method wont work properlydeclare @test table(Username varchar(100))insert into @test select 'test' union allselect 'babu' union allselect 'pythondave' SELECT @@rowcount, UserName FROM @TEST WHERE UserName = 'babu' OR UserName = 'pythondave'group by UserNamehaving @@rowcount = 2select Username from( select UserName,count(*) over () as counting from @test where UserName = 'babu' OR UserName = 'pythondave') ast where counting=2 MadhivananFailing to plan is Planning to fail |
 |
|
|
baburk
Posting Yak Master
108 Posts |
Posted - 2008-11-14 : 08:03:42
|
| Hi madhivanan,Since username that I am passing is dynamicso help for this.DECLARE @CombineUserNames VARCHAR(1000)DECLARE @NoOfUsers INTBEGINSELECT @NoOfUsers = count(*) FROM dbo.[Split](',' , @UserNames)SELECT @CombineUserNames = COALESCE(@CombineUserNames + ' OR ', '') + 'UserName = ' + '''' + Item + '''' FROM dbo.[Split](',' ,@UserNames)select Username from( EXEC( 'SELECT count(*) over () as counting, UserName FROM dbo.Users WHERE ' + @CombineUserNames ))ast where counting=@NoOfUsersEND |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-14 : 08:11:35
|
| [code]declare @UserNames varchar(100)set @UserNames='babu,pythondave'select Username from( SELECT count(*) over () as counting, UserName FROM dbo.Users WHERE ','+@UserNames+',' like '%,'+username+',%')as t where counting=(len(@UserNames)-len(replace(@UserNames,',','')))+1[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
baburk
Posting Yak Master
108 Posts |
|
|
|