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?
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?
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
can you break that code down and make it easy to under stand? please
ASKER
ok thanks now i understand
you could read this article to understand:
https://www.experts-exchange.com/A_3203.html
https://www.experts-exchange.com/A_3203.html
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
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
http://sqlservernation.com/blogs/brandongalderisi/archive/2009/09/20/using-row-number-vs-a-correlated-sub-select.aspx
ASKER
thanks
What does that query not give you? (it has correct syntax)