SET DATEFORMAT dmy;
SELECT TPRD.NOMEFANTASIA AS NOME,
(SELECT (SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02'))THEN TITMMOV.QUANTIDADE ELSE '0' END))
FROM TMOV
left JOIN TITMMOV ON (TITMMOV.IDMOV = TMOV.IDMOV
AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
JOIN TPRD ON (TPRD.IDPRD = TITMMOV.IDPRD
AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE TMOV.CODCOLIGADA = '1' AND TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02')
AND TMOV.STATUS not in ('C')
AND TPRD.codigoprd like :Cod_Estruturado
AND TMOV.DATAEMISSAO < :DATAINI_D )
-
(SELECT(SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.01','4.1.03'))THEN TITMMOV.QUANTIDADE ELSE '0' END))
FROM TMOV
left JOIN TITMMOV ON (TITMMOV.IDMOV = TMOV.IDMOV
AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
JOIN TPRD ON (TPRD.IDPRD = TITMMOV.IDPRD
AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE TMOV.CODCOLIGADA = '1' AND TMOV.CODTMV IN ('1.2.01','4.1.03')
AND TMOV.STATUS not in ('C')
AND TPRD.codigoprd like :Cod_Estruturado
AND TMOV.DATAEMISSAO < :DATAINI_D )AS SALDO_ANTERIOR,
SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02') )THEN TITMMOV.QUANTIDADE ELSE '0' END)AS TOT_ENTRADAS,
SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.01','4.1.03'))THEN TITMMOV.QUANTIDADE ELSE '0' END) AS TOTAL_BAIXAS,
(SELECT(SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02'))THEN TITMMOV.QUANTIDADE ELSE '0' END))
FROM TMOV
INNER JOIN TITMMOV
ON (TITMMOV.IDMOV = TMOV.IDMOV
AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
INNER JOIN TPRD
ON (TPRD.IDPRD = TITMMOV.IDPRD
AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE
TMOV.CODCOLIGADA = '1'
AND TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02')
AND TMOV.STATUS not in ('C')
AND TPRD.codigoprd like :Cod_Estruturado
AND TMOV.DATAEMISSAO <= :DATAFIM_D )
-
(SELECT(SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.01','4.1.03'))THEN TITMMOV.QUANTIDADE ELSE '0' END))
FROM TMOV
INNER JOIN TITMMOV
ON (TITMMOV.IDMOV = TMOV.IDMOV
AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
INNER JOIN TPRD
ON (TPRD.IDPRD = TITMMOV.IDPRD
AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE
TMOV.CODCOLIGADA = '1'
AND TMOV.CODTMV IN ('1.2.01','4.1.03')
AND TMOV.STATUS not in ('C')
AND TPRD.codigoprd like :Cod_Estruturado
AND TMOV.DATAEMISSAO <= :DATAFIM_D ) as saldo_atual
FROM TMOV
INNER JOIN TITMMOV ON (TITMMOV.IDMOV = TMOV.IDMOV AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
INNER JOIN TPRD ON (TPRD.IDPRD = TITMMOV.IDPRD AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE TMOV.CODCOLIGADA = '1'
AND TMOV.CODTMV IN ('1.2.01','4.1.03', '1.2.02', '4.1.01', '4.1.02')
aND TMOV.STATUS not in ('C') AND
TPRD.codigoprd like :Cod_Estruturado
AND tmov.dataemissao BETWEEN :DATAINI_D
AND :DATAFIM_D
GROUP BY TPRD.NOMEFANTASIA
ORDER BY TPRD.NOMEFANTASIA
SELECT TPRD.NOMEFANTASIA AS NOME,
(SELECT (SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02'))THEN TITMMOV.QUANTIDADE ELSE '0' END))
FROM TMOV
left JOIN TITMMOV ON (TITMMOV.IDMOV = TMOV.IDMOV
AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
JOIN TPRD ON (TPRD.IDPRD = TITMMOV.IDPRD
AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE TMOV.CODCOLIGADA = '1' AND TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02')
AND TMOV.STATUS not in ('C')
AND TPRD.codigoprd like :Cod_Estruturado
AND TMOV.DATAEMISSAO < :DATAINI_D )
-
(SELECT(SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.01','4.1.03'))THEN TITMMOV.QUANTIDADE ELSE '0' END))
FROM TMOV
left JOIN TITMMOV ON (TITMMOV.IDMOV = TMOV.IDMOV
AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
JOIN TPRD ON (TPRD.IDPRD = TITMMOV.IDPRD
AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE TMOV.CODCOLIGADA = '1' AND TMOV.CODTMV IN ('1.2.01','4.1.03')
AND TMOV.STATUS not in ('C')
AND TPRD.codigoprd like :Cod_Estruturado
AND TMOV.DATAEMISSAO < :DATAINI_D )AS SALDO_ANTERIOR,
SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02') )THEN TITMMOV.QUANTIDADE ELSE '0' END)AS TOT_ENTRADAS,
SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.01','4.1.03'))THEN TITMMOV.QUANTIDADE ELSE '0' END) AS TOTAL_BAIXAS,
(SELECT(SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02'))THEN TITMMOV.QUANTIDADE ELSE '0' END))
FROM TMOV
INNER JOIN TITMMOV
ON (TITMMOV.IDMOV = TMOV.IDMOV
AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
INNER JOIN TPRD
ON (TPRD.IDPRD = TITMMOV.IDPRD
AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE
TMOV.CODCOLIGADA = '1'
AND TMOV.CODTMV IN ('1.2.02', '4.1.01', '4.1.02')
AND TMOV.STATUS not in ('C')
AND TPRD.codigoprd like :Cod_Estruturado
AND TMOV.DATAEMISSAO <= :DATAFIM_D )
-
(SELECT(SUM(CASE WHEN(TMOV.CODTMV IN ('1.2.01','4.1.03'))THEN TITMMOV.QUANTIDADE ELSE '0' END))
FROM TMOV
INNER JOIN TITMMOV
ON (TITMMOV.IDMOV = TMOV.IDMOV
AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
INNER JOIN TPRD
ON (TPRD.IDPRD = TITMMOV.IDPRD
AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE
TMOV.CODCOLIGADA = '1'
AND TMOV.CODTMV IN ('1.2.01','4.1.03')
AND TMOV.STATUS not in ('C')
AND TPRD.codigoprd like :Cod_Estruturado
AND TMOV.DATAEMISSAO <= :DATAFIM_D ) as saldo_atual
FROM TMOV
INNER JOIN TITMMOV ON (TITMMOV.IDMOV = TMOV.IDMOV AND TITMMOV.CODCOLIGADA = TMOV.CODCOLIGADA)
INNER JOIN TPRD ON (TPRD.IDPRD = TITMMOV.IDPRD AND TPRD.CODCOLIGADA = TITMMOV.CODCOLIGADA)
WHERE TMOV.CODCOLIGADA = '1'
AND TMOV.CODTMV IN ('1.2.01','4.1.03', '1.2.02', '4.1.01', '4.1.02')
aND TMOV.STATUS not in ('C') AND
TPRD.codigoprd like :Cod_Estruturado
AND tmov.dataemissao BETWEEN :DATAINI_D
AND :DATAFIM_D
GROUP BY TPRD.NOMEFANTASIA
ORDER BY TPRD.NOMEFANTASIA
Última edição por Carlos Rugno em 19/2/2020, 13:45, editado 1 vez(es)