| Author |
Topic |
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2008-01-23 : 10:24:10
|
| HIPlease help I didn't get right solution for this problem. So I am posting again with full details.Scenariocol1---col2------col3----col4-----col5------col5123-----AB--------WE-----Name------Add------Prod1123-----AB--------DC-----Name------Add------Pro512123-----AB--------FR-----Name------Add------Prt78389-----AB--------DC-----Name------Add------Prt78482-----AB--------DC-----Name------Add------Prt78How do I select Distinct row by col1 from the above scenario. Expected result will be123-----AB--------WE-----Name------Add------Prod1389-----AB--------DC-----Name------Add------Prt78482-----AB--------DC-----Name------Add------Prt78 OR123-----AB--------DC-----Name------Add------Pro512389-----AB--------DC-----Name------Add------Prt78482-----AB--------DC-----Name------Add------Prt78 OR123-----AB--------FR-----Name------Add------Prt78389-----AB--------DC-----Name------Add------Prt78482-----AB--------DC-----Name------Add------Prt78Please Help. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-23 : 10:28:56
|
| SELECT t1.* FROM Scenario t1INNER JOIN (SELECT col1,MAX(col5) AS col5 FROM Scenario GROUP BY col1)t2ON t2.col1=t1.col1AND t2.col5=t1.col5 |
 |
|
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2008-01-23 : 10:37:35
|
HiWhy Do I have use Col5. It is just sample how the data will be in the table. IN SHORT IF I GET MORE THAN ONE SAME VALUE IN col1, I NEED TO SELECT ANY ONE OF THE ROW WHICH HAS MORE THAN ONE VALUE IN col1 quote: Originally posted by visakh16 SELECT t1.* FROM Scenario t1INNER JOIN (SELECT col1,MAX(col5) AS col5 FROM Scenario GROUP BY col1)t2ON t2.col1=t1.col1AND t2.col5=t1.col5
|
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-23 : 10:56:09
|
quote: Originally posted by Vaishu HIPlease help I didn't get right solution for this problem. So I am posting again with full details.Scenariocol1---col2------col3----col4-----col5------col5123-----AB--------WE-----Name------Add------Prod1123-----AB--------DC-----Name------Add------Pro512123-----AB--------FR-----Name------Add------Prt78389-----AB--------DC-----Name------Add------Prt78482-----AB--------DC-----Name------Add------Prt78How do I select Distinct row by col1 from the above scenario. Expected result will be123-----AB--------WE-----Name------Add------Prod1389-----AB--------DC-----Name------Add------Prt78482-----AB--------DC-----Name------Add------Prt78 OR123-----AB--------DC-----Name------Add------Pro512389-----AB--------DC-----Name------Add------Prt78482-----AB--------DC-----Name------Add------Prt78 OR123-----AB--------FR-----Name------Add------Prt78389-----AB--------DC-----Name------Add------Prt78482-----AB--------DC-----Name------Add------Prt78Please Help.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=95089--------------------------------------------------------------------JD"Research is what I'm doing when I don't know what I'm doing." |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-23 : 11:03:14
|
quote: Originally posted by Vaishu HiWhy Do I have use Col5. It is just sample how the data will be in the table. IN SHORT IF I GET MORE THAN ONE SAME VALUE IN col1, I NEED TO SELECT ANY ONE OF THE ROW WHICH HAS MORE THAN ONE VALUE IN col1 quote: Originally posted by visakh16 SELECT t1.* FROM Scenario t1INNER JOIN (SELECT col1,MAX(col5) AS col5 FROM Scenario GROUP BY col1)t2ON t2.col1=t1.col1AND t2.col5=t1.col5
Not mandatory.You may apply MAX(),MIN() etc on any of other column that contain unique values for same value of Col1 |
 |
|
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2008-01-23 : 12:36:08
|
| PLEASE SOMBODY HELP NONE OF THIS SOLUCTION WORKS?IF I GET MORE THAN ONE SAME VALUE IN col1, I NEED TO SELECT ANY ONE OF THE ROW WHICH HAS MORE THAN ONE VALUE IN col1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-23 : 12:46:38
|
Are you using SQL Server 2005? Then use this:-SELECT t.Col1,t.col2,t.col3,t.col4,t.col5FROM(SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col1) AS RowNo,Col1,col2,col3,col4,col5FROM Scenario)tWHERE t.RowNo=1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-23 : 12:49:38
|
quote: Originally posted by Vaishu HiWhy Do I have use Col5. It is just sample how the data will be in the table. IN SHORT IF I GET MORE THAN ONE SAME VALUE IN col1, I NEED TO SELECT ANY ONE OF THE ROW WHICH HAS MORE THAN ONE VALUE IN col1 quote: Originally posted by visakh16 SELECT t1.* FROM Scenario t1INNER JOIN (SELECT col1,MAX(col5) AS col5 FROM Scenario GROUP BY col1)t2ON t2.col1=t1.col1AND t2.col5=t1.col5
What did you get when you tried this? |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2008-01-23 : 13:04:48
|
-- create our table create table foo (col1 int, col2 char(2), col3 char(2) ,col4 char(4),col5 char(3),col6 varchar(6))-- insert datainsert into foo select 123,'AB','WE','Name','Add','Prod1'union all select 123,'AB','DC','Name','Add','Pro512'union all select 123,'AB','FR','Name','Add','Prt78'union all select 389,'AB','DC','Name','Add','Prt78'union all select 482,'AB','DC','Name','Add','Prt78'-- do the work SELECT t1.*FROM foo t1INNER JOIN (SELECT col1,MAX(col3) AS col3FROM fooGROUP BY col1)t2ON t2.col1=t1.col1AND t2.col3=t1.col3-- results482 AB DC Name Add Prt78389 AB DC Name Add Prt78123 AB WE Name Add Prod1-- Using code from Visakh-- do the work SELECT t1.*FROM foo t1INNER JOIN (SELECT col1,MAX(col5) AS col5FROM fooGROUP BY col1)t2ON t2.col1=t1.col1AND t2.col5=t1.col5-- results482 AB DC Name Add Prt78389 AB DC Name Add Prt78123 AB FR Name Add Prt78123 AB WE Name Add Prod1123 AB DC Name Add Pro512 You have to use another column that is not going to be unique is all. Could probably use more sample data to be sure.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2008-01-24 : 07:59:41
|
| HiThanks a lot 'visak' and 'doneAtwork'. Multiple rows in my table have identical data and the solution doesn't work, So I created ID (autonumber)in my table and used YOUR CODE and IT WORKS. SELECT t1.* FROM FEEDER t1 INNER JOIN (SELECT [order-id], MAX(ID) AS IDS FROM FEEDER GROUP BY [order-id])t2 ON t2.IDS=t1.IDFinal Question: Now I have autonumber colum (ID). Is there any way I can get the same result without using inner join ?THANKS A LOT GUYS |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2008-01-24 : 10:44:21
|
| why would you not want to use an inner join?[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2008-01-24 : 12:58:16
|
The Reason I am using dataset in c# and try to sum the two colum using INNER JOIN, Group etc. But the dataset throwing error (Couldn't get the value within the sub select command (temp table). But I have sorted by other method.quote: Originally posted by DonAtWork why would you not want to use an inner join?[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp
|
 |
|
|
Manjula
Starting Member
1 Post |
Posted - 2009-04-24 : 07:34:02
|
hi friends try this......SELECT tbl.col1, tbl.col2FROM (SELECT ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS RowNo,col1, col2 FROM Scenario)tbl WHERE tbl.RowNo=1 |
 |
|
|
|