Buen día estimados, tengo una duda y requiero de su ayuda:
Tengo un Query de ventas netas por SN pero cuando declaro la variable pongo los rangos de fechas que corresponde al año anterior al año actual en ventas pero esto no me es funcional ya que debo de manipular el query. Como asigno para que el usuario ponga la selección de fechas a la hora de ejecutar la consulta.
declare Y1 date = '20210405';
declare Y2 date = '20220405';
select "CardCode", "CardName", SUM("Y1") as "Y1", SUM("Y2") as "Y2"
from(
select "CardCode"
, "CardName"
, CASE WHEN "Year" = YEAR(:Y1) then (("DocTotal")/1.13) else 0 end as "Y1"
, CASE WHEN "Year" = YEAR(:Y2) then (("DocTotal")/1.13) else 0 end as "Y2"
from (
select T0."CardCode", T1."CardName", SUM("DocTotal") "DocTotal", YEAR(T0."DocDate") "Year"
from OINV T0
left join OCRD T1 on T0."CardCode" = T1."CardCode"
where
(YEAR(T0."DocDate") = YEAR(:Y1) and T0."DocDate" <= :Y1) or (YEAR(T0."DocDate") = YEAR(:Y2) and T0."DocDate" <= :Y2)
group by T0."CardCode", T1."CardName", YEAR(T0."DocDate")
union all
select T0."CardCode", T1."CardName", SUM(T0."DocTotal")*-1 "DocTotal", YEAR(T0."DocDate") "Year"
from ORIN T0
left join OCRD T1 on T0."CardCode" = T1."CardCode"
where
(YEAR(T0."DocDate") = YEAR(:Y1) and T0."DocDate" <= :Y1) or (YEAR(T0."DocDate") = YEAR(:Y2) and T0."DocDate" <= :Y2)
group by T0."CardCode", T1."CardName", YEAR(T0."DocDate")
))
group by "CardCode", "CardName"
order by "CardCode" asc;
Agradezco la ayuda que se me pueda brindar.