I need help with sum and join self.
I have table like
Buyer Info2 LinkKey Date Credit Debit
Samuel 20 S15 2012-03-15 500 0
Samuel 20 S15 2012-04-26 0 300
Maria 20 123 2012-05-03 300 0
Maria 20 123 2012-07-20 0 300
Maria 20 456 2012-02-09 150 0
I need get sum(credit-debit) where buyer=buyer, info2=info2, linkKey=linkKey by period (period from credit)
I need result like
Buyer Januar Februar Mart April May AMOUNT
Samuel 0 0 200 0 0 200
Maria 0 150 0 0 0 150
Best Answer
I hope this helps. Used a union to denormalize your data and then wrap the 3 union queries with the sum of each month so each buyer will only have a single row. Then you join this result with total (credit + debit) for each buyer for the last column of the final result.