Running Count of Duplicate Values

I have a table showing pallets and the amount of product ("units") on those pallets. Individual pallets can have multiple records due to multiple possible defect codes. This means when I am trying to sum the total units on all pallets, the same pallet could get counted more than once, which is undesirable. I would like (but don't know how) to add a running tally column to show how many times a specific pallet ID has appeared so that I can filter out any record where the count is greater than 1:

| Pallet_ID | Units | Defect_Code | COUNT |
+-----------+-------+-------------+-------+
| A1        | 100   | 03          | 1     |
| A1        | 100   | 05          | 2     |
| B1        | 95    | 03          | 1     |
| C1        | 300   | 05          | 1     |
| C1        | 300   | 06          | 2     |
| D1        | 210   | 03          | 1     |
| A1        | 100   | 10          | 3     |
| D1        | 210   | 03          | 2     |

In the above example, the correct sum total of units should be 705. A solution in SQL or in DAX would work (although I lean towards SQL). I have searched for a long time but could not find a solution that fits this particular scenario. Many thanks in advance for your time and consideration!

0

1 Answer

You may use the windowing function row_number() with the over clause where you partition by the pallet. Within each partition you can control which row is assigned the number 1 by using the order by inside the over clause.

select
*
from (
    select 
      Pallet_ID 
    , Units 
    , Defect_Code
    , row_number() over(partition by Pallet_ID order by defect_code) as count_of
    from yourtable
    ) 
where count_of = 1

Note I have arbitrability use the column defect_code to order by as I don't know what other columns may exist. If your table has a date/time value for when the row was created you could use this instead, or perhaps the unique key of the table.

side note: I would not recommend using column alias of "count" as it's a SQL reserved word

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest