I have 2 tables tbl1, tbl2.
Sample data of tbl1:
| Id | Name | CreatedOn | SpentAmt |
|---|---|---|---|
| 1 | abc | 2023/03/31 | 1000 |
| 2 | Test | 2023/03/31 | 14000 |
| 3 | Mark | 2023/03/31 | 2000 |
| 4,5 | Robert,Gustin | 2023/03/31 | 700 |
Sample data of tbl2:
| Id | Name | CreatedOn | UsedAmt |
|---|---|---|---|
| 1 | abc | 2023/03/31 | 2000 |
| 7 | Grace | 2023/03/31 | 4000 |
| 2 | Test | 2023/03/31 | 10000 |
| 9,1 | Mary,abc | 2023/03/31 | 1000 |
Expected output:
| Id | Name | SpentAmt | SpentAmt |
|---|---|---|---|
| 1 | abc | 1000 | 2000 |
| 2 | Test | 14000 | 10000 |
| 3 | mark | 2000 | null |
| 4,5 | Robert,gustin | 700 | null |
| 7 | grace | null | 4000 |
| 9,1 | Mary,abc | null | 1000 |
This is what I have so far:
SELECT id, name, spentamt AS amt
FROM tbl1
WHERE createdon >= '2021-04-01'
GROUP BY id, name
SELECT id, name, usedamt AS amt
FROM tbl2
WHERE createdon >= '2021-04-01'
GROUP BY id, name
Both individual queries return the expected results, but combining the two isn't working properly
This looks like a
full join; I don't think that you need aggregation here.Note that I moved the date filtering within subqueries, so it happens before the
full join.