I’m trying to handle this issue in my code that should be handled in SQL. The problem is I want to take this
ID COL 1 | ID COL 2 | CHARGE | PAYMENT
2 | 3 | 17 | 0
2 | 3 | 0 | 17
and turn it into this
ID COL 1 | ID COL 2 | CHARGE | PAYMENT
2 | 3 | 17 | 17
table1
id | whatever | whatever1
5 | null | null
table2
id | id col 1 | id col 2 | charge | payment
5 | 2 | 3 | 17 | 0
5 | 2 | 3 | 0 | 17
current result:
id | whatever | whatever1 | idcol1 | idcol2 | charge | payment
5 | null | null | 2 | 3 | 17 | 0
5 | null | null | 2 | 3 | 0 | 17
want:
id | whatever | whatever1 | idcol1 | idcol2 | charge | payment
5 | 2 | 3 | 17 | 17
The problem is during my sql call I’m doing an inner join, which does a cartesian product for some of the values rather than doing what I’d want above. Does anyone have an idea how this could be accomplished?
You can use group by for this:
This gets the maximum charge and maximum payment from all rows with the same id columns. If you have more than one row with charges or payment, you might prefer SUM() to MAX().
With the joins, this would look like: