Howdy all, SQL rookie here.I'm trying to get to some to data from our MS SQL server as our CRM front-end is laughably bad.I can sort of get what I want but I'm having a few issues with the joins and way the info is groupedEffectively I have 3 tables:i = which contains invoice infot = which contains transactionsty = which contains type IDBecause all transactions have a type ID, the only way I can get to them is by summing all instances for each type for every invoice as shown in the below code.SELECT i.invoiceid AS [Inv No], CASE WHEN ty.typeID = '1' THEN SUM(t.TotalExclVAT) ELSE '0' END AS Fees, CASE WHEN ty.typeID = '2' THEN SUM(t.TotalExclVAT) ELSE '0' END AS Payments, CASE WHEN ty.typeID = '3' THEN SUM(t.TotalExclVAT) ELSE '0' END AS Adjustments, CASE WHEN ty.typeID = '4' THEN SUM(t.TotalExclVAT) ELSE '0' END AS Expenses, ty.typeID AS [Type ID]FROM dbo.transactions AS t INNER JOIN dbo.typelist AS ty ON t.transcodeID = ty.transcodeID LEFT OUTER JOIN dbo.invoices AS i ON t.invoiceID = i.invoiceIDWHERE (t.TotalExclVAT <> 0)GROUP BY i.invoiceid, ty.typeID
This however produces a data set which looks like:Inv No Fees Payments Adjustments Expenses Type ID101234 0.00 -100.00 0.00 0.00 2101234 150.00 0.00 0.00 0.00 1101234 0.00 0.00 -50.00 0.00 3
It can obviously can be manipulated in Pivot in excel, but ideally i need it in a tabular form where the sum of each of these transaction types are shown against the specific invoice on one line.Inv No Fees Payments Adjustments Expenses101234 150.00 -100.00 -50.00 0.00
Any clues on how best to do this would be much appreciatedCheers!Danny/----OOOO