Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Prob with Select Query

Author  Topic 

dineshrajan_it
Posting Yak Master

217 Posts

Posted - 2009-05-12 : 02:05:14
Hi All,

I have a Login table which has Loginid,username,pwd,lastmodifiedby as int

so i need to get a query to know who modified which username


SELECT USERNAME,[ModifiedBy]=(SELECT L1.USERNAME FROM LOGIN L1 WHERE L1.LOGINID=L.LOGINID)
FROM
LOGIN L

This is query. i have used subquery. may be we can use joins. so best solution is to use self-joins for this. why do i have to use same table twice to get desired results.
Any other way out

thanks in advance

Iam a slow walker but i never walk back

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-12 : 02:24:51
[code]
SELECT L.USERNAME, MODIFIEDBY = M.USERNAME
FROM LOGIN L
INNER JOIN LOGIN M ON L.LASTMODIFIEDBY = M.LOGINID
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

dineshrajan_it
Posting Yak Master

217 Posts

Posted - 2009-05-12 : 02:46:19
Thanks for Response,

I know we can use self-joins. i thought of is it possible in CTE.

Iam a slow walker but i never walk back
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-12 : 02:47:43
the data you need are from 2 separate row. CTE or not, you still have to read 2 rows to get the required data


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

dineshrajan_it
Posting Yak Master

217 Posts

Posted - 2009-05-12 : 02:51:01
quote:
Originally posted by khtan

the data you need are from 2 separate row. CTE or not, you still have to read 2 rows to get the required data


KH
[spoiler]Time is always against us[/spoiler]





Thanks

Iam a slow walker but i never walk back
Go to Top of Page
   

- Advertisement -