I have a table that lists charges against an order and also an ID of the invoice associated with the order.
orderID, financialType, invoiceID
I am looking to get all orders that have a financialType='17' but have different invoiceIDs
An example table:
orderID financialType invoiceID
123000 10 NULL
123000 17 900
123000 17 901
123111 17 902
123111 17 902
123222 10 NULL
123333 17 900
123444 17 900
123444 17 901
123444 17 902
In this case I am interested in 123000 (invoiced on 2 different invoices) and 123444 (invoiced 3 times).
My current query is:
SELECT orderID, count(*) as freq FROM orders WHERE financialType='17' group by orderID, invoiceID having freq > 1 order by freq desc
But this is giving me the total number of times each order was charged, not the number of times it was charged on different invoices. In my example I don't care about 123111 - this was charged twice but on the same invoice, this is fine.
I would hope to see:
orderID freq
123000 2
123444 3
select orderID, count(distinct invoiceID)may work ?