I have a Sales
table and a Period
table.
Sales table
+--------------+-------------+
| Country_Code | Period_Code |
+--------------+-------------+
| CH | MAI_18 |
| CH | JUN_18 |
| NO | 2020-01-21 |
| NO | 2020-01-21 |
+--------------+-------------+
Period table
+--------------+-------------+
| Country_Code | Period_Code |
+--------------+-------------+
| NO | 200121 |
| NO | 200122 |
+--------------+-------------+
I am getting the below error while I join these 2 tables.
Below is the sql I used. As per my understanding, since I have given the country filter as NO
in the 2nd line, it should first execute the 2nd line and then do the join only on NO
country. But internally it is considering the CH
country also which is causing to have the below sql fail.
SELECT DISTINCT SAL.COUNTRY_CODE,PER.PERIOD_CODE
FROM (SELECT * FROM MYSCHEMA.SALES WHERE COUNTRY_CODE in('NO')) SAL
JOIN MYSCHEMA.PERIOD PER
ON SAL.COUNTRY_CODE=PER.COUNTRY_CODE
AND TO_CHAR(TO_DATE(SAL.PERIOD_CODE,'YYYY-MM-DD'),'YYMMDD') = PER.PERIOD_CODE
ORDER BY 1
I used the CTE
also which is behaving the same way. There could be some solution for this. I would really appreciate if some one can help on this to have the sql run without any errors.