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.
| Author |
Topic |
|
hspatil31
Posting Yak Master
182 Posts |
Posted - 2011-12-13 : 01:08:35
|
| Dear All,I am having following output. In this I have joined two table. In that my first all 3 columns value is same but 4th one is different.Now in this situation I want first record only.Can anybody plz tell me how to do this ?Query:SELECT * FROM XYZ X INNER JOIN ABC AON X.Column1 = a.Column1Column1 Column2 column3 column4 A AAA AAAA WW A AAA AAAA QQThanks and Regard'sHarish Patil |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-13 : 01:38:56
|
On what basis you tell you want first record from second table? there's no concept of first and last in sql table so you've to specify first,last in terms of order of another column by means of ORDER BY clause.Anyways, one thing you can do is use either of below and see if its what you wantSELECT X.Column1, X.Column2, X.column3 ,MAX(Column4) AS Column4FROM XYZ X INNER JOIN ABC AON X.Column1 = a.Column1GROUP BY X.Column1, X.Column2, X.column3 orSELECT X.Column1, X.Column2, X.column3 ,MIN(Column4) AS Column4FROM XYZ X INNER JOIN ABC AON X.Column1 = a.Column1GROUP BY X.Column1, X.Column2, X.column3 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sunnyleoneo
Starting Member
19 Posts |
Posted - 2011-12-13 : 02:09:46
|
| Hi,you can use ROW_NUMBER(). I have used sample data from my side. It will be good if you can provide structure and data for both the table.CREATE TABLE #xyz (Column1 varchar(20), Column2 varchar(20), column3 varchar(20))INSERT INTO #xyz (Column1, Column2, column3)SELECT 'A', 'AAA', 'AAAA' UNION ALLSELECT 'A', 'AAA', 'AAAA'CREATE TABLE #abc (Column1 varchar(20), Column2 varchar(20))INSERT INTO #abc (Column1, Column2)SELECT 'A', 'WW'UNION ALLSELECT 'A', 'QQ'SELECT * FROM (SELECT ROW_NUMBER() OVER( PARTITION BY x.Column1,x.Column2,x.column3 ORDER BY x.Column1,x.Column2,x.column3 ) sequence,x.Column1,x.Column2,x.column3,a.Column2 Column4 FROM #XYZ X INNER JOIN #ABC AON X.Column1 = a.Column1)t WHERE t.sequence =1GROUP BY X.Column1, X.Column2, X.column3 |
 |
|
|
|
|
|
|
|