Link to home
Start Free TrialLog in
Avatar of JCWEBHOST
JCWEBHOST

asked on

Sql Distinct more than 1 coloum

hey i have a problem in my sql statment:

select catalog_id, min(id), min(prod_id), min(prod_title), min(prod_price), min(prod_description), min(prod_image)
  from Products
group by catalog_id

i need to select more than one coloum  but i can't pull the image coloum, can any one help me?
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Can you please elaborate on what you are after with some sample data rows?
What does that query not give you? (it has correct syntax)
Avatar of JCWEBHOST
JCWEBHOST

ASKER

i want an sql statement to display distinct rows :

here is my sql statment:


select DISTINCT  catalog_id, id from Products

outputs:  
catalog_id  id          
----------- -----------
20          25          
20          26          
20          27          
20          28          
20          29          
20          30          
20          31          
20          32          
20          33          
20          36          
20          39          
24          37          
24          38    


i want it to output  only 2 rows

catalog_id  id          
----------- -----------
20          25          
24          37

and i need to out more than 1 coloum e.g images, int varchar
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
can you break that code down and make it easy to under stand? please
ok thanks now i understand
Are these easier to understand?
Works on MySQL as well.

select * from Products
where ID in
(
select min(id)
from Products
group by catalog_id
)

or

select p.*
from
(
select min(id) as MinID
from Products
group by catalog_id
) m inner join Products p on MinID=p.id
For performance sakes, you may want to consider using an exists() based correlated sub-select to get your min IDs.  I've done some performance testing and it is going to be faster.

http://sqlservernation.com/blogs/brandongalderisi/archive/2009/09/20/using-row-number-vs-a-correlated-sub-select.aspx
thanks