Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the OneLake & Platform Admin teams for an ask US anything on July 16th. Join now.

Reply
Ornella
Regular Visitor

Unexpected Duplicates in SQL Query

Hello everyone,


Could someone please explain why I'm getting duplicate values even though my query is supposed to prevent them? And if possible, suggest a solution to fix it.

 

Best regards,

Ornella

Capture d'écran 2025-05-19 115058.pngCapture d'écran 2025-05-19 115148.png

1 ACCEPTED SOLUTION
v-bmanikante
Community Support
Community Support

Hi @Ornella ,

 

Thank you for reaching out to Microsoft Fabric Community Forum.

@burakkaragoz @lutz_bendlin Thank you for your quick response.

 

Could you please try the below SQL query:

WITH CTE AS (

    SELECT *,

           ROW_NUMBER() OVER (PARTITION BY CAST(DATE_EXEC AS DATE) ORDER BY DATE_EXEC DESC) AS rn

    FROM XXXX_CTRL_CARD

    WHERE CAST(DATE_EXEC AS DATE) = CAST(GETDATE() AS DATE)

)

DELETE FROM CTE WHERE rn > 1;

 

If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it!

 

Regards,

B Manikanteswara Reddy

View solution in original post

4 REPLIES 4
v-bmanikante
Community Support
Community Support

Hi @Ornella ,

 

Thank you for reaching out to Microsoft Fabric Community Forum.

@burakkaragoz @lutz_bendlin Thank you for your quick response.

 

Could you please try the below SQL query:

WITH CTE AS (

    SELECT *,

           ROW_NUMBER() OVER (PARTITION BY CAST(DATE_EXEC AS DATE) ORDER BY DATE_EXEC DESC) AS rn

    FROM XXXX_CTRL_CARD

    WHERE CAST(DATE_EXEC AS DATE) = CAST(GETDATE() AS DATE)

)

DELETE FROM CTE WHERE rn > 1;

 

If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it!

 

Regards,

B Manikanteswara Reddy

Hello @v-bmanikante 


Thank you for your prompt response. I had already implemented it.

 

Best regards,

Ornella

 

burakkaragoz
Community Champion
Community Champion

Hi @Ornella ,

 

Unexpected duplicates usually show up when your JOIN brings in more rows than expected—especially if the table you're joining to has multiple matching rows.

A couple of things to check:

  • Are you joining on a unique key? If not, even a small mismatch can cause row multiplication.
  • Try using DISTINCT or GROUP BY to see if that helps reduce the duplicates—but that’s more of a patch than a fix.
  • You can also use ROW_NUMBER() or RANK() to filter down to just one row per group if needed.

If you can share a simplified version of your query and table structure, happy to take a closer look!


If my response resolved your query, kindly mark it as the Accepted Solution to assist others. Additionally, I would be grateful for a 'Kudos' if you found my response helpful.

lutz_bendlin
Advocate V
Advocate V

Isn't your query equivalent to 

 

DELETE FROM XXXX_CTRL_CARD 

WHERE CAST(DATE_EXEC as DATE) <= GETDATE() 

 

?

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.