Extensibility Rules - SQL - Custom Data Tables
This syntax only inserts one line and the line does not have 'entity currency' or 'group currency' description in the [currency] column. Please advise on what the issue could be:
INSERT INTO XFC_005_RCP_FinResults ( [PROJ_ID], [PROJ_DESC], [SCENARIO], [TIME], [VIEW], [ACCOUNT], [EOJ], [EOJ_AVG_FX_RATE], [AMOUNT_INPUT], [AMOUNT_FACTORISED], [FACTOR_PERC], CURRENCY )
SELECT [PROJ_ID], [PROJ_DESC], [SCENARIO], [TIME], [VIEW], [ACCOUNT], [EOJ], [EOJ_AVG_FX_RATE], [AMOUNT_INPUT], [AMOUNT_FACTORISED], [FACTOR_PERC],
CASE WHEN RowNum = 1 THEN 'Entity currency' WHEN RowNum = 2 THEN 'Group currency' END AS CURRENCY
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY [PROJ_ID] ORDER BY [SCENARIO], [TIME], [ACCOUNT], [CURRENCY]) AS RowNum
FROM XFC_005_RCP_FinResults
WHERE CURRENCY = 'Project currency'
) AS t
WHERE RowNum <= 2
AND NOT EXISTS (
SELECT 1
FROM XFC_005_RCP_FinResults
WHERE [PROJ_ID] = t.[PROJ_ID]
AND CURRENCY = CASE WHEN RowNum = 1 THEN 'Entity currency' WHEN RowNum = 2 THEN 'Group currency' END
);