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 2000 Forums
 Transact-SQL (2000)
 How to join in the same table?

Author  Topic 

chungpn
Starting Member

8 Posts

Posted - 2009-04-28 : 21:28:29
I have a sql server table contains this data:

EmpCode Month Year Field1 Field2
---------------------------------------------------------------------------------------
QT351 April 2009 45 23
QT351 March 2009 67 43
QT555 March 2009 67 12
QT555 April 2009 54 32
QT351 April 2008 45 23
QT555 March 2007 67 12
--------------------------------------------------------------------------------------
Can you please help me to write a Transact-SQL command to get records from the table where field1 in Arpril but Field2 in March like this result:
EmpCode Month Year Field1 Field2
---------------------------------------------------------------------------------------
QT351 April 2009 45 43
QT555 April 2009 54 12
--------------------------------------------------------------------------------------


khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-04-28 : 21:35:08
use table alias

select a.EmpCode, a.Month, a.Year, a.Field1, m.Field2
from table1 a inner join table1 m
on a.EmpCode = m.EmpCode
and a.Year = m.Year
where a.Month = 'April'
and m.Month = 'March'



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

Go to Top of Page

chungpn
Starting Member

8 Posts

Posted - 2009-05-13 : 21:28:08
quote:
Originally posted by khtan

use table alias

select a.EmpCode, a.Month, a.Year, a.Field1, m.Field2
from table1 a inner join table1 m
on a.EmpCode = m.EmpCode
and a.Year = m.Year
where a.Month = 'April'
and m.Month = 'March'



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




Thank you khtan. This solve my problem.
Go to Top of Page
   

- Advertisement -