Skip to content
Advertisement

SQL how to create a view with 2 sums of diferent tables, when those tables have diferent number of rows

I have 3 tables, “accounts” “charges” and “payments” I want to create a view with the account data the sum of the charges, and the sum of the payments, but payments have usually more records than charges for the same account when I create the view and GROUP BY charges end up “duplicated” like by the number of rows mismatch. y tried using COALLECE but still repeat the charge by the number of payments. this is the query:

also tried with coallece

enter image description here

hope I was concise (enough) I tried StackOverflow in my native language but is deserted

Advertisement

Answer

You can use two sub-selects:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement