(select t1.ACODIGO,t1.ADESCRI,t1.AUNIDAD,t1.Linea,t1.Linea2 from(
SELECT
row_number() over (partition by decodigo,datepart(month,CAFECDOC) order by CAFECDOC desc, CATIPMOV desc ,CACODMOV desc,CAALMA desc,CATD desc,CANUMDOC desc,DEITEM desc)[Fila],
DECODIGO as ACODIGO,DEDESCRI as ADESCRI,DEUNIDAD as AUNIDAD,
(case when DATEPART(month, CAFECDOC)=MONTH(GETDATE()) -1 then cast(DEPRECIO as money) else 0 end) Linea,
(case when DATEPART(month, CAFECDOC)=MONTH(GETDATE()) then cast(DEPRECIO as money) else 0 end) Linea2
from
[025BDCOMUN].DBO.MOVALMCAB AS B INNER JOIN (MOVALMDET AS A
LEFT JOIN MAEART AS M ON A.DECODIGO = M.ACODIGO) ON B.CAALMA = A.DEALMA AND B.CATD = A.DETD AND B.CANUMDOC = A.DENUMDOC
WHERE
YEAR(CAFECDOC) = '2020' AND
CASITGUI <> 'A' AND
NOT (CATD = 'GS' AND CACODMOV = 'GF' AND CASITGUI = 'F') AND
NOT (M.AFSTOCK = 'N' AND M.AFSERIE = 'N' AND M.AFLOTE = 'N')
AND DECODIGO like '100%'
AND (DECODIGO <> 'TEXTO')
AND M.AFAMILIA >= '001' AND M.AFAMILIA <= '027') t1
where [Fila]=1
group by [Fila],t1.ACODIGO,t1.ADESCRI,t1.AUNIDAD,t1.Linea,t1.Linea2)
order by ACODIGO
(select t1.ACODIGO,t1.ADESCRI,t1.AUNIDAD,t1.Linea,t1.Linea2 from(
SELECT
row_number() over (partition by decodigo,datepart(month,CAFECDOC) order by CAFECDOC desc, CATIPMOV desc ,CACODMOV desc,CAALMA desc,CATD desc,CANUMDOC desc,DEITEM desc)[Fila],
DECODIGO as ACODIGO,DEDESCRI as ADESCRI,DEUNIDAD as AUNIDAD,
(case when DATEPART(month, CAFECDOC)=MONTH(GETDATE()) -1 then cast(DEPRECIO as money) else 0 end) Linea,
(case when DATEPART(month, CAFECDOC)=MONTH(GETDATE()) then cast(DEPRECIO as money) else 0 end) Linea2
from
[025BDCOMUN].DBO.MOVALMCAB AS B INNER JOIN (MOVALMDET AS A
LEFT JOIN MAEART AS M ON A.DECODIGO = M.ACODIGO) ON B.CAALMA = A.DEALMA AND B.CATD = A.DETD AND B.CANUMDOC = A.DENUMDOC
WHERE
YEAR(CAFECDOC) = '2020' AND
CASITGUI <> 'A' AND
NOT (CATD = 'GS' AND CACODMOV = 'GF' AND CASITGUI = 'F') AND
NOT (M.AFSTOCK = 'N' AND M.AFSERIE = 'N' AND M.AFLOTE = 'N')
AND DECODIGO like '100%'
AND (DECODIGO <> 'TEXTO')
AND M.AFAMILIA >= '001' AND M.AFAMILIA <= '027') t1
where [Fila]=1
group by [Fila],t1.ACODIGO,t1.ADESCRI,t1.AUNIDAD,t1.Linea,t1.Linea2)
order by ACODIGO