baaroz
baaroz

Reputation: 19587

Distinct on multi-columns in sql

I have this query in sql

select cartlines.id,cartlines.pageId,cartlines.quantity,cartlines.price
from orders
INNER JOIN
cartlines on(cartlines.orderId=orders.id)where userId=5 

I want to get rows distinct by pageid ,so in the end I will not have rows with same pageid more then once(duplicate)

any Ideas

Thanks

Baaroz

Upvotes: 0

Views: 95

Answers (2)

valverij
valverij

Reputation: 4941

Going by what you're expecting in the output and your comment that says "...if there rows in output that contain same pageid only one will be shown...," it sounds like you're trying to get the top record for each page ID. This can be achieved with ROW_NUMBER() and PARTITION BY:

SELECT *
FROM (
    SELECT 
        ROW_NUMBER() OVER(PARTITION BY c.pageId ORDER BY c.pageID) rowNumber,
        c.id,
        c.pageId,
        c.quantity,
        c.price
    FROM orders o
    INNER JOIN cartlines c ON c.orderId = o.id 
    WHERE userId = 5 
) a
WHERE a.rowNumber = 1

You can also use ROW_NUMBER() OVER(PARTITION BY ... along with TOP 1 WITH TIES, but it runs a little slower (despite being WAY cleaner):

SELECT TOP 1 WITH TIES c.id, c.pageId, c.quantity, c.price
FROM orders o
    INNER JOIN cartlines c ON c.orderId = o.id 
WHERE userId = 5 
ORDER BY ROW_NUMBER() OVER(PARTITION BY c.pageId ORDER BY c.pageID)

Upvotes: 2

Menelaos
Menelaos

Reputation: 25727

If you wish to remove rows with all columns duplicated this is solved by simply adding a distinct in your query.

select distinct cartlines.id,cartlines.pageId,cartlines.quantity,cartlines.price
from orders
INNER JOIN
cartlines on(cartlines.orderId=orders.id)where userId=5 

If however, this makes no difference, it means the other columns have different values, so the combinations of column values creates distinct (unique) rows.

As Michael Berkowski stated in comments:

DISTINCT - does operate over all columns in the SELECT list, so we need to understand your special case better.

In the case that simply adding distinct does not cover you, you need to also remove the columns that are different from row to row, or use aggregate functions to get aggregate values per cartlines.

Example - total quantity per distinct pageId:

select distinct cartlines.id,cartlines.pageId, sum(cartlines.quantity)
from orders
INNER JOIN
cartlines on(cartlines.orderId=orders.id)where userId=5 

If this is still not what you wish, you need to give us data and specify better what it is you want.

Upvotes: 0

Related Questions