Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH CTE ( DATA,CODPROD,DESCPROD,UNID,QTDTOTALVENDIDA )
- AS (
- SELECT
- B.EA01DATA,
- B.EA03CODEPROD,
- P.CAD_NOMEDOPROD,
- B.EA07UNID,
- SUM( B.EA05QTD )
- FROM ESTOQUEMOVIMENTOB B
- INNER JOIN CADPRODUTOS P ON ( P.CAD_CEAN13 = B.EA03CODEPROD)
- WHERE B.EA01DATA BETWEEN :PDATEINI AND :PDATEFIM
- GROUP BY
- B.EA01DATA,
- B.EA03CODEPROD,
- P.CAD_NOMEDOPROD,
- B.EA07UNID
- ),
- CTE2( CODPROD,DESCPROD,UNID,QTDTOTALVENDIDA ) AS
- (
- SELECT CODPROD,
- DESCPROD,
- UNID,
- QTDTOTALVENDIDA
- FROM CTE C
- UNION ALL
- SELECT
- P.CAD_CEAN13,
- P.CAD_NOMEDOPROD,
- P.SAID_UNVENDA,
- CAST((0) AS NUMERIC(15,2))QTDTOTALVENDIDA
- FROM CADPRODUTOS P
- )
- SELECT CODPROD,
- MAX(DESCPROD)DESCPROD,
- MAX(UNID)UNID,
- MAX(COALESCE(QTDTOTALVENDIDA,0))QTDTOTALVENDIDA
- FROM CTE2 CC
- GROUP BY CODPROD
- ORDER BY 4 DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement