I have the below design of my tables:-
Table A
--------------------------
subCatId | catId | catDesc
--------------------------
1 | 3 | 100
2 | 3 | 100
3 | 5 | 100
4 | 5 | 100
5 | | 100
Table B
--------------------------
subCatId | amount
--------------------------
1 | 10
2 | 20
3 | 5
4 | 15
5 |
Third table, i.e. Table AB, is the one where the records are to be inserted. On the basis of the above tables the query should: 1. check Table AB, whether any subCatId exists in it or not. 2. if the table is empty then get all the subCatId & catId present in Table A which has catDesc=100 and based on Table A's subCatId take Table B's amount.
Table AB
--------------------------
subCatId | catId | amount
--------------------------
1 | 3 | 10
2 | 3 | 20
3 | 5 | 35
4 | 5 | 15
5 | | 50
As you can see above in Table AB for subCatId 1 & 2 the catId is 3 so the amount values for 1 & 2 should sum up and show up for subCatId 3 (including the amount value 5 which was already there in Table B). Similary, for subCatId 5 the amount value should sum up from the subCatId 3 & 4.
I will really appreciate if someone can help me on getting the expected result as shown above for TableAB.
I have tried the below query separately
SELECT A.CATID, SUM(B.AMOUNT)
FROM A LEFT OUTER JOIN B
ON A.SUBCATID = B.SUBCATID
WHERE A.CATDESC=100
AND A.SUBCATID NOT IN
(SELECT AB.SUBCATID FROM AB)
GROUP BY CATID;
However, it only gives catid and the total amount values but I could not find a way to get the subCatId
and their respective amounts also. Please help...thanks. The reason for applying left outer join is that, if a subCatId does not exist in Table B, but it exists in Table A then it should also be shown with the results.