Rehman
Rehman

Reputation: 3998

how to pull unique results

im looking for query to load and group the data

i do have 1:n relationship of Parent:Child

and i want to pull latest child,instead of children, of a parent

that means each entry should have unique parent with latest child.

Child Table alt text

Desired Results alt text

Tried:

i have tried following query but it get me oldest results

SELECT c.* FROM child  AS c GROUP BY c.parent_id HAVING(MAX(c.order))

Thanks in advance

Upvotes: 3

Views: 105

Answers (1)

Joe Stefanelli
Joe Stefanelli

Reputation: 135918

select ct.*
    from ChildTable ct
        inner join (select parent_id, max(order) as MaxOrder
                        from ChildTable
                        group by parent_id) q
            on ct.parent_id = q.parent_id
                and ct.order= q.MaxOrder

Upvotes: 1

Related Questions