How to Use Multiple Columns in Partition By And Ensure No Duplicate Row is Returned
Asked Answered
C

5

46

I have used multiple columns in Partition By statement in SQL but duplicate rows are returned back. I only want distinct rows being returned back.

This is what I have coded in Partition By:

SELECT DATE, STATUS, TITLE, ROW_NUMBER() OVER (PARTITION BY DATE, STATUS, TITLE ORDER BY QUANTITY ASC) AS Row_Num
    FROM TABLE

This is the output I get currently: (Where there are duplicate rows being returned - Please Refer to Row 6 to 8)

enter image description here

This is the output I want to achieve: (no duplicate row being returned - Please Refer to Row 6 to 8)

enter image description here

Question: How can I place multiple columns in 1 Partition By and Ensure No Duplicate Row is Returned?

Appreciate if someone can provide me help on this, thanks a lot!!

Cathepsin answered 8/10, 2015 at 9:17 Comment(0)
A
43

Try this, It worked for me

SELECT * FROM (
            SELECT
                [Code],
                [Name],
                [CategoryCode],
                [CreatedDate],
                [ModifiedDate],
                [CreatedBy],
                [ModifiedBy],
                [IsActive],
                ROW_NUMBER() OVER(PARTITION BY [Code],[Name],[CategoryCode] ORDER BY ID DESC) rownumber
            FROM MasterTable
          ) a
        WHERE rownumber = 1 
Aeon answered 13/4, 2018 at 7:3 Comment(1)
Why is it that you have to name all the columns instead of using * for the interior SELECT statement?Chenault
Z
14

If your table columns contains duplicate data and If you directly apply row_ number() and create PARTITION on column, there is chance to have result in duplicated row and with row number value.

To remove duplicate row, you need one more INNER query in from clause which eliminates duplicate rows and then it will give output to it's foremost outer FROM clause where you can apply PARTITION and ROW_NUMBER ().

As like below example:

SELECT DATE, STATUS, TITLE, ROW_NUMBER() OVER (PARTITION BY DATE, STATUS, TITLE ORDER BY QUANTITY ASC) AS Row_Num
FROM (
     SELECT DISTINCT <column names>...
) AS tbl
Zygapophysis answered 8/10, 2015 at 9:30 Comment(3)
hi @Bhavesh Harsora, have tried this method but doesn't work. This is what I have tried: SELECT tbl.DATE, STATUS, tbl.TITLE, ROW_NUMBER() OVER (PARTITION BY DATE, STATUS, TITLE ORDER BY QUANTITY ASC) AS Row_Num FROM (SELECT DISTINCT TITLE FROM TABLE) AS tblCathepsin
Your given query is missing {tbl.DATE, STATUS} columns From your Inner Query you specified where you have taken only one TITLE column. You can also try an alternative way like removing duplicated value using GROUP BY and HAVING clause.Zygapophysis
hi @Bhavesh Harsora, have tried on this two methods but do not work as well. I tried to add in the missing columns that you have mentioned but still same rows bring returned. Then next I tried to use GROUP BY and HAVING clause inside the inner query but still the same rows being returned as well.Cathepsin
A
3

I'd create a cte and do an inner join. It's not efficient but it's convenient

with table as (
SELECT DATE, STATUS, TITLE, ROW_NUMBER() 
OVER (PARTITION BY DATE, STATUS,  TITLE ORDER BY QUANTITY ASC) AS Row_Num
 FROM TABLE)

select *

from table t
join select(
max(Row_Num) as Row_Num
,DATE
,STATUS
,TITLE
from table 
group by date, status, title) t2  
on t2.Row_Num = t.Row_Num and t2
and t2.date = t.date
and t2.title = t.title
Ambler answered 23/2, 2017 at 17:42 Comment(0)
A
0

Try DENSE_RANK() instead of ROW_NUMBER() and select for DR = 1

Acadian answered 19/7, 2022 at 20:31 Comment(0)
R
0

DENSE_RANK () works better with multiple fields in the partition. syntax is the same.

Rhizomorphous answered 23/3, 2023 at 23:49 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Elderberry

© 2022 - 2024 — McMap. All rights reserved.