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
 General SQL Server Forums
 New to SQL Server Programming
 Top1 after join

Author  Topic 

aoriju
Posting Yak Master

156 Posts

Posted - 2012-09-20 : 06:18:39



DECLARE @tblA AS TABLE(col1 VARCHAR(20), col2 VARCHAR(20) )
DECLARE @tblB AS TABLE(col1 VARCHAR(20) )

INSERT INTO @tblA SELECT 'A1' ,'A'
INSERT INTO @tblA SELECT 'A2' ,'A'

INSERT INTO @tblA SELECT 'B1' ,'B'
INSERT INTO @tblA SELECT 'B2' ,'B'


INSERT INTO @tblB SELECT 'A'
INSERT INTO @tblB SELECT 'A'
INSERT INTO @tblB SELECT 'A'
INSERT INTO @tblB SELECT 'B'
INSERT INTO @tblB SELECT 'B'


While joining the @tblA col2 with @tblB col1 then i need to get values like

A1
B1
that means after joins 1 record only(Top 1)

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2012-09-20 : 06:37:34
Do you need this?


select distinct a.col2 from @tblA a inner join @tblB b
on a.col2 =b.col1

Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-09-20 : 06:40:02
TOP 1 of ?

maybe like this ?

select MIN(col1)
from @tblA
group by col2



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

Go to Top of Page
   

- Advertisement -