I have a mail ledger table that I'm trying to query for return percentages. I am doing a GROUP BY to get unique records based on order_no, county and zip_code.
SELECT, COUNT(*) as TotalMailed, COUNT(*) (WHERE Returned = 1) as ReturnedMailed, Order_No, Zip_Code,County FROM MailLedger GROUP BY Order_No, Zip_Code, County
I am getting the proper count of each record grouping, however I need to get the count of the subset of grouped records that has Returned set to true.
I am running SQL Server, so I do have OVER and PARTITION at my disposal, however, I'd love to know the proper way to handle it in Postgres/MySQL as well.