I have below 3 tables.
Table 1: Invoice
Columns - ID,SPENT,ROLEID
ID | SPENT | ROLEID
1 | 500 | 1
2 | 400 | 2
Table 2: ROLES
Columns - ROLE_ID (PK),DEPARTMENT_ID,ROLE_NAME
ROLE_ID | DEPARTMENT_ID | ROLE_NAME
1 | 1 | AGENT
2 | 2 | SUPERVISOR
Table 3: DEPARTMENT
Columns - DEP_ID,DEP_NAME
DEP_ID | DEP_NAME
1 | ACCOUNTING
2 | CUSTOMER_SERVICE
3 | HRA
The final output is that if I look for the invoices, I should see all the departments even when I just got a invoices for 1 department.
Below is the query which I tried.
SELECT
D.DEP_NAME
,I.SPENT
FROM INVOICE I
CROSS JOIN DEPARTMENT D
LEFT JOIN ROLES R ON R.ROLE_ID = I.ROLE_ID AND D.DEP_ID = R.DEPARTMENT_ID
ORDER BY D.DEP_NAME
Example output:
DEP_NAME | SPENT
ACCOUNTING | 500
CUSTOMER_SERVICE | 400
HRA | NULL
Above query is giving me duplicate data and also I'm not getting the unmatched values.
Can anyone please advise on this?
