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
 How to Select Distinct row by col1 ?

Author  Topic 

Vaishu
Posting Yak Master

178 Posts

Posted - 2008-01-23 : 10:24:10
HI

Please help I didn't get right solution for this problem. So I am posting again with full details.
Scenario

col1---col2------col3----col4-----col5------col5
123-----AB--------WE-----Name------Add------Prod1
123-----AB--------DC-----Name------Add------Pro512
123-----AB--------FR-----Name------Add------Prt78
389-----AB--------DC-----Name------Add------Prt78
482-----AB--------DC-----Name------Add------Prt78

How do I select Distinct row by col1 from the above scenario. Expected result will be

123-----AB--------WE-----Name------Add------Prod1
389-----AB--------DC-----Name------Add------Prt78
482-----AB--------DC-----Name------Add------Prt78

OR

123-----AB--------DC-----Name------Add------Pro512
389-----AB--------DC-----Name------Add------Prt78
482-----AB--------DC-----Name------Add------Prt78

OR

123-----AB--------FR-----Name------Add------Prt78
389-----AB--------DC-----Name------Add------Prt78
482-----AB--------DC-----Name------Add------Prt78


Please Help.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-23 : 10:28:56
SELECT t1.*
FROM Scenario t1
INNER JOIN (SELECT col1,MAX(col5) AS col5
FROM Scenario
GROUP BY col1)t2
ON t2.col1=t1.col1
AND t2.col5=t1.col5
Go to Top of Page

Vaishu
Posting Yak Master

178 Posts

Posted - 2008-01-23 : 10:37:35
Hi

Why 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 t1
INNER JOIN (SELECT col1,MAX(col5) AS col5
FROM Scenario
GROUP BY col1)t2
ON t2.col1=t1.col1
AND t2.col5=t1.col5


Go to Top of Page

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-01-23 : 10:56:09
quote:
Originally posted by Vaishu

HI

Please help I didn't get right solution for this problem. So I am posting again with full details.
Scenario

col1---col2------col3----col4-----col5------col5
123-----AB--------WE-----Name------Add------Prod1
123-----AB--------DC-----Name------Add------Pro512
123-----AB--------FR-----Name------Add------Prt78
389-----AB--------DC-----Name------Add------Prt78
482-----AB--------DC-----Name------Add------Prt78

How do I select Distinct row by col1 from the above scenario. Expected result will be

123-----AB--------WE-----Name------Add------Prod1
389-----AB--------DC-----Name------Add------Prt78
482-----AB--------DC-----Name------Add------Prt78

OR

123-----AB--------DC-----Name------Add------Pro512
389-----AB--------DC-----Name------Add------Prt78
482-----AB--------DC-----Name------Add------Prt78

OR

123-----AB--------FR-----Name------Add------Prt78
389-----AB--------DC-----Name------Add------Prt78
482-----AB--------DC-----Name------Add------Prt78


Please 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."
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-23 : 11:03:14
quote:
Originally posted by Vaishu

Hi

Why 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 t1
INNER JOIN (SELECT col1,MAX(col5) AS col5
FROM Scenario
GROUP BY col1)t2
ON t2.col1=t1.col1
AND 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
Go to Top of Page

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
Go to Top of Page

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.col5
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col1) AS RowNo,
Col1,
col2,
col3,
col4,
col5
FROM Scenario
)t
WHERE t.RowNo=1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-23 : 12:49:38
quote:
Originally posted by Vaishu

Hi

Why 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 t1
INNER JOIN (SELECT col1,MAX(col5) AS col5
FROM Scenario
GROUP BY col1)t2
ON t2.col1=t1.col1
AND t2.col5=t1.col5






What did you get when you tried this?
Go to Top of Page

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 data
insert 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 t1
INNER JOIN (SELECT col1,MAX(col3) AS col3
FROM foo
GROUP BY col1)t2
ON t2.col1=t1.col1
AND t2.col3=t1.col3

-- results
482 AB DC Name Add Prt78
389 AB DC Name Add Prt78
123 AB WE Name Add Prod1

-- Using code from Visakh
-- do the work
SELECT t1.*
FROM foo t1
INNER JOIN (SELECT col1,MAX(col5) AS col5
FROM foo
GROUP BY col1)t2
ON t2.col1=t1.col1
AND t2.col5=t1.col5

-- results
482 AB DC Name Add Prt78
389 AB DC Name Add Prt78
123 AB FR Name Add Prt78
123 AB WE Name Add Prod1
123 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.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

Vaishu
Posting Yak Master

178 Posts

Posted - 2008-01-24 : 07:59:41
Hi

Thanks 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.ID

Final 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
Go to Top of Page

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.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

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.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp

Go to Top of Page

Manjula
Starting Member

1 Post

Posted - 2009-04-24 : 07:34:02
hi friends try this......

SELECT tbl.col1, tbl.col2
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS RowNo,
col1, col2 FROM Scenario
)tbl
WHERE tbl.RowNo=1



Go to Top of Page
   

- Advertisement -