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 2005 Forums
 Transact-SQL (2005)
 Distinct question

Author  Topic 

mike13
Posting Yak Master

219 Posts

Posted - 2010-06-03 : 13:13:58
Hi all,

I want to select the Distinct Price from product table but i also want the product ID with it

Table
Productid,price
1,10,00
2,23,00
3,24,00
4,35,00
5,10,00
6,10,00
7,23,00
8,24,00
9,35,00
10,10,00

I want it to return:
1,10,00
2,23,00
3,24,00
4,35,00

only distinct prices and the first product id it finds with that price

Thanks a lot,

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-06-03 : 13:19:04
[code]SELECT price,MIN(Productid)
FROM Table
GROUP BY price[/code]
Go to Top of Page

mike13
Posting Yak Master

219 Posts

Posted - 2010-06-04 : 01:56:34
quote:
Originally posted by vijayisonly

SELECT price,MIN(Productid)
FROM Table
GROUP BY price




Thanks that did the Trick :-)
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-06-04 : 09:30:59
Np. You're welcome.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-06-07 : 06:20:41
use row_number function also.......
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-07 : 06:24:20
quote:
Originally posted by bklr

use row_number function also.......


Why?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-07 : 06:32:49
quote:
Originally posted by webfred

quote:
Originally posted by bklr

use row_number function also.......


Why?


No, you're never too old to Yak'n'Roll if you're too young to die.


I think he meant it as different approach as the question is posted in version 2005

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-06-07 : 06:43:06
quote:
Originally posted by webfred

quote:
Originally posted by bklr

use row_number function also.......


Why?


No, you're never too old to Yak'n'Roll if you're too young to die.


another way.... using row_number
Go to Top of Page
   

- Advertisement -