Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete the duplicate values in the SUM with MySQL or SQL

Hi I am doing a sum of a table, but the problem is that the table has duplicate rows, so I wonder how can I do the sum without duplicated rows:

The main table is this one:

folio cashier_id amount date
0001 1 2500 2022-06-01 00:00:00
0002 2 10000 2022-06-01 00:00:00
0001 1 2500 2022-06-01 00:00:00
0003 1 1000 2022-06-01 00:00:00

If I sum that you can see that the first and the third row are duplicated, so when I do the sum it makes it wrong because, the result will be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

cashier_id cash_amount
1 6000
2 10000

but it should be:

cashier_id cash_amount
1 3500
2 10000

The query that I use to make the sum is this one:

SELECT `jysparki_jis`.`api_transactions`.`cashier_id` AS `cashier_id`,
       SUM(`jysparki_jis`.`api_transactions`.`cash_amount`) AS `cash_amount`,,
       COUNT(0) AS `ticket_number`, 
       DATE(`jysparki_jis`.`api_transactions`.`created_at`) AS `date` 
FROM `jysparki_jis`.`api_transactions` 
WHERE DATE(`jysparki_jis`.`api_transactions`.`created_at`) >= '2022-01-01'     
  AND (`jysparki_jis`.`api_transactions`.`dte_type_id` = 39 
   OR `jysparki_jis`.`api_transactions`.`dte_type_id` = 61)    
  AND `jysparki_jis`.`api_transactions`.`cashier_id` <> 0 
GROUP BY `jysparki_jis`.`api_transactions`.`cashier_id`, 
         DATE(`jysparki_jis`.`api_transactions`.`created_at`)

How you can see the sum is this:

SUM(`jysparki_jis`.`api_transactions`.`cash_amount`).

I wonder how can I do the sum avoiding to duplicate the folio with same cashier_id?

I know that if I filter for the cashier_id and folio I can avoid the duplicate rows but I do not know how to do that, can you help me?

Thanks

>Solution :

Given your provided input tables, you can use the DISTINCT clause inside the SUM aggregation function to solve your problem:

SELECT cashier_id, SUM(DISTINCT amount)
FROM tab
GROUP BY cashier_id

Check the demo here.

Then you can add up your conditions inside your WHERE clause to this query, and your aggregation on the "created_at" field (that should correspond to the "date" field of your sample table – I guess). This solution may give your the general idea.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading