Relative Content

Tag Archive for sqlsql-serverssms

SUM function in SubQuery using Group By

I have a table that stores data for invoices for my various suppliers. I am trying to write a query to determine which supplier I have spent the most money on, so I have currently written a query that gets the sum of the InvoiceTotals while grouping by the SupplierID. The part that is tripping me up is that I need to account for the InvoiceType – if it’s ‘credit’ the InvoiceTotal for that one is actually negative (so it should be subtracted). The CurrentStatus should only be ‘Paid’ too.