Estoy intentando crear una alerta, que me avise cuando detecta un cambio de algún articulo en su lista de precios de venta (Lista=1). Pero al intentar ejecutar esta consulta me devuelve un error en el SQL anidado.
error: ‘correlated subquery cannot have TOP or ORDER BY’ y no se como solucionarlo.
La alerta me tendría devolver los artículos que han cambiado de precio en la tarifa de venta, ese mismo dia, para revisar la causa de este cambio de la lista de precios de ese Ítem, y no encuentro la solución
SELECT
T0."ItemCode",
T1."ItemName",
T0."Price",
(SELECT Top 1 T5."Price" from "AIT1" T5 where T5."ItemCode" = T0."ItemCode" and T5."PriceList" = '1' and T5."Price" is not null and T0."Price" <> T5."Price" order By T5."LogInstanc" Desc) AS "OldPrice",
T1."UpdateDate"
FROM ITM1 T0
LEFT JOIN OITM T1 ON T0."ItemCode" = T1."ItemCode" WHERE T0."PriceList" ='1' and DAYS_BETWEEN(T1."UpdateDate",CURRENT_TIMESTAMP)=0```
Hola yo no uso lista de precios pero prueba con este código: (usando max en el Loginstanc, en lugar de top 1), espero te sea de utilidad o por lo menos darte una idea de como adaptar tu codigo.
SELECT
T0."ItemCode",
T1."ItemName",
T0."Price",
(SELECT T5."Price"
from "AIT1" T5
where T5."ItemCode" = T0."ItemCode" and
T5."PriceList" = '1' and
T5."Price" is not null and
T0."Price" <> T5."Price" and
T5."LogInstanc" = (select max(L."LogInstanc")
from "AIT1" L
where T5."ItemCode" = L."ItemCode")
) AS "OldPrice",
T1."UpdateDate"
FROM ITM1 T0
LEFT JOIN OITM T1 ON T0."ItemCode" = T1."ItemCode" WHERE T0."PriceList" ='1' and DAYS_BETWEEN(T1."UpdateDate",CURRENT_TIMESTAMP)=0
Añadiendo el filtro de que solo muestre los articulos en donde el precio antiguo no sea nulo, parece que funcionar correctamente ahora
SELECT
T0."ItemCode",
T1."ItemName",
T0."Price" as "Nuevo Precio Lista Venta",
(SELECT T5."Price"
from "AIT1" T5
where T5."ItemCode" = T0."ItemCode" and
T5."PriceList" = '1' and
T5."Price" is not null and
T0."Price" <> T5."Price" and
T5."LogInstanc" = (select max(L."LogInstanc")
from "AIT1" L
where T5."ItemCode" = L."ItemCode")
) AS "Antiguo Precio Lista Venta",
T1."UpdateDate"
FROM ITM1 T0
LEFT JOIN OITM T1 ON T0."ItemCode" = T1."ItemCode"
WHERE T0."PriceList" ='1' and DAYS_BETWEEN(T1."UpdateDate",CURRENT_TIMESTAMP)=0 and
(SELECT T5."Price"
from "AIT1" T5
where T5."ItemCode" = T0."ItemCode" and
T5."PriceList" = '1' and
T5."Price" is not null and
T0."Price" <> T5."Price" and
T5."LogInstanc" = (select max(L."LogInstanc")
from "AIT1" L
where T5."ItemCode" = L."ItemCode")
) is not null
ORDER BY T0."ItemCode"```