question

Jeff 1 avatar image
Jeff 1 asked

SQL question on duplicate records - HELP! - THANKS!!

Up against a SQL question that is difficult to code. Any help is greatly appreciated.

General example. I want to select duplicate records that have the same INVOICE NUMBER with one or more occurences with an INVOICE AMOUNT = $10.00.

DATABASE = TEST                    
TABLE = INVOICES                    
                    
INVOICE NUMBER     INVOICE AMOUNT                    
123                 $20.00                    
234                 $50.00                    
123                 $10.00                    
657                 $11.00                    
123                 $40.00                    
234                 $20.00                    

I can select duplicates by INVOICE NUMBER using the following:

SELECT [INVOICE NUMBER],[INVOICE AMOUNT]                     
FROM [TEST].[dbo].[INVOICES]                    
WHERE ((([INVOICES].[INVOICE NUMBER]) In                     
     (SELECT [INVOICE NUMBER]                     
      FROM [test].[dbo].[INVOICES] AS Tmp                     
      GROUP BY [INVOICE NUMBER]                     
      HAVING Count(*)>1)));                    

Resulting set is:

123                 $20.00                    
234                 $50.00                    
123                 $10.00                    
123                 $40.00                    
234                 $20.00                       

But, I only want to select those duplicates that have one or more occurences where the INVOICE AMOUNT = $10.00.

Desired set would be:

123                 $20.00                    
123                 $10.00                    
123                 $40.00                    

Can you help??? - THANKS!!!!!

delete-duplicates
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Scot Hauder avatar image
Scot Hauder answered
SELECT [INVOICE NUMBER]            
      ,[INVOICE AMOUNT]             
FROM [INVOICES]             
WHERE [INVOICE NUMBER] IN (SELECT i1.[INVOICE NUMBER]            
                           FROM [INVOICES] AS i1            
                           JOIN [INVOICES] i2 ON (i1.[INVOICE NUMBER] = i2.[INVOICE NUMBER])            
                           WHERE i1.[INVOICE AMOUNT] = 10.00            
                           GROUP BY i1.[INVOICE NUMBER]             
                           HAVING COUNT(*) > 1);            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Brian Perrin avatar image
Brian Perrin answered

the self-join is elegant, but perhaps a little overkill - try:

SELECT [INVOICE NUMBER] ,[INVOICE AMOUNT] FROM [INVOICES] i WHERE EXISTS (select 1 from [INVOICES] t where t.[INVOICE NUMBER] = i.[INVOICE NUMBER] and t.[INVOICE AMOUNT] = 10)

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.