0
The script below calculates the consumption value for materials and medicines , grouping by patient.
select
coalesce( sum (
(
(L.QTDE) - coalesce(
sum(
(
select sum(QTDE)
from GECADDEV CD
inner join GELANDEV LD on CD.ID = LD.ID_GECADDEV
where L.ID_GELANSAI = LD.ID_GELANSAI
and LD.CONSOL = 'T'
)
),
0
)
) *
-- Nesse trecho que calculo acréscimo ou redução
(
SELECT VALOR1
FROM SPBRAVEN(5, L.ITEM, current_date, 1)
) * (1.10)),0)
from GECADSAI D
inner join RECADATE A on D.PAC = A.REG
inner join GELANSAI L on D.DOC = L.DOC
and D.ANO = L.ANO
and D.MES = L.MES
inner join GEITENS I on L.ITEM = I.COD
inner join RICADPAC CP on A.PRONT = CP.PRONT
where L.CONSOL = 'T'
and D.CONV = e.conv
and D.PAC = e.reg
and char_length(D.PAC) > 6
and A.PEXT = 'S'
and i.classif in (2) --classificação do item sendo : 2 = medicamento e 1 material
However, I need to apply a rule that : If the item is classified as material, I apply a reduction of -40% on the unit value.
And if it is medicine, apply an over 10% increase on the value
Anyone has any idea , I tried to use case and iff but could not
Thank you @Rovann, your solution perfectly met your needs. This factor is already parameterized via application, but many tables involve such management. As the Percentage does not undergo readjustment , and be only a report for an end user I chose to treat in sql same
– Bruno Fabiano