Boa tarde,
Realmente Tiago, conheço a CalcEst, porem só dentro do protheus teria como utilizar. Mas estou utilizando o Power BI para criar o dashboard, fora do protheus, apenas utilizando o SQL
Felipe, estou ja analisando as tabelas SB9, SD1, SD2 e SD3, mas sem muito sucesso.
Segue a sentença que fiz:
SELECT '' AS DOCUMENTO,
RTRIM(B9COD) AS CODIGO,
B9_LOCAL AS LOCAL,
B9_DATA AS DATA,
B9_QINI AS QUANT,
B1.B1_UPRC AS VLR,
B1_TIPO AS TIPO,
RTRIM(B1_DESC) AS DESCRICAO,
BM.BM_GRUPO AS GRUPO
FROM SB9010 B9
INNER JOIN SB1010 B1 ON B1.B1_COD = B9.B9_COD
INNER JOIN SBM010 BM ON BM.BM_GRUPO = B1.B1_GRUPO
WHERE B9.B9_DATA <> ''
AND B9.B9_QINI > 0
AND B1.B1_TIPO IN ('MP','PA','II','OI','ME')
AND B9_LOCAL NOT IN ('05','06','07','11')
AND B9.D_E_L_E_T <> ''
AND B1.DE_L_E_T <> ''
AND BM.DE_L_E_T <> ''
UNION
SELECT F1.F1DOC AS DOCUMENTO,
RTRIM(D1_COD) AS CODIGO,
D1.D1_LOCAL AS LOCAL,
F1.F1_DTDIGIT AS DATA,
D1_QUANT AS QUANT,
B1.B1_UPRC AS VLR,
B1_TIPO AS TIPO,
RTRIM(B1_DESC) AS DESCRICAO,
BM.BM_GRUPO AS GRUPO
FROM SD1010 D1
INNER JOIN SF1010 F1 ON F1.F1_DOC = D1.D1_DOC
AND F1.F1_FORNECE = D1.D1_FORNECE
INNER JOIN SF4010 F4 ON F4.F4_CODIGO = D1.D1_TES
INNER JOIN SB1010 B1 ON B1.B1_COD = D1.D1_COD
INNER JOIN SBM010 BM ON BM.BM_GRUPO = B1.B1_GRUPO
WHERE F1.F1_DTDIGIT >= '20190101'
AND F4.F4_ESTOQUE = 'S'
AND B1.B1_TIPO IN ('MP','PA','II','OI','ME')
AND D1_LOCAL NOT IN ('05','06','07','11')
AND D1.D_E_L_E_T <> ''
AND F1.DE_L_E_T <> ''
AND F4.DE_L_E_T <> ''
AND B1.DE_L_E_T <> ''
AND BM.DE_L_E_T <> ''
UNION
SELECT F2.F2DOC AS DOCUMENTO,
RTRIM(D2_COD) AS CODIGO,
D2.D2_LOCAL AS LOCAL,
F2.F2_EMISSAO AS DATA,
-D2_QUANT AS QUANT,
B1.B1_UPRC AS VLR,
B1_TIPO AS TIPO,
RTRIM(B1_DESC) AS DESCRICAO,
BM.BM_GRUPO AS GRUPO
FROM SD2010 D2
INNER JOIN SF2010 F2 ON F2.F2_DOC = D2.D2_DOC
AND F2.F2_CLIENTE = D2.D2_CLIENTE
INNER JOIN SF4010 F4 ON F4.F4_CODIGO = D2.D2_TES
INNER JOIN SB1010 B1 ON B1.B1_COD = D2.D2_COD
INNER JOIN SBM010 BM ON BM.BM_GRUPO = B1.B1_GRUPO
WHERE F2.F2_EMISSAO >= '20190101'
AND F4.F4_ESTOQUE = 'S'
AND B1.B1_TIPO IN ('MP','PA','II','OI','ME')
AND D2_LOCAL NOT IN ('05','06','07','11')
AND D2.D_E_L_E_T <> ''
AND F2.DE_L_E_T <> ''
AND F4.DE_L_E_T <> ''
AND B1.DE_L_E_T <> ''
AND BM.DE_L_E_T <> ''
UNION
SELECT D3DOC AS DOCUMENTO,
RTRIM(D3_COD) AS CODIGO,
D3_LOCAL AS LOCAL,
D3_EMISSAO AS DATA,
D3_QUANT AS QUANT,
B1.B1_UPRC AS VLR,
B1_TIPO AS TIPO,
RTRIM(B1_DESC) AS DESCRICAO,
BM.BM_GRUPO AS GRUPO
FROM SD3010 D3
INNER JOIN SB1010 B1 ON B1.B1_COD = D3.D3_COD
INNER JOIN SBM010 BM ON BM.BM_GRUPO = B1.B1_GRUPO
WHERE D3.D3_EMISSAO >= '20190101'
AND D3_TM <= '500'
AND D3_LOCAL NOT IN ('05','06','07','11')
AND B1.B1_TIPO IN ('MP','PA','II','OI','ME')
AND D3.D_E_L_E_T <> ''
AND B1.DE_L_E_T <> ''
AND BM.DE_L_E_T <> ''
UNION
SELECT D3DOC AS DOCUMENTO,
RTRIM(D3_COD) AS CODIGO,
D3_LOCAL AS LOCAL,
D3_EMISSAO AS DATA,
-D3_QUANT AS QUANT,
B1.B1_UPRC AS VLR,
B1_TIPO AS TIPO,
RTRIM(B1_DESC) AS DESCRICAO,
BM.BM_GRUPO AS GRUPO
FROM SD3010 D3
INNER JOIN SB1010 B1 ON B1.B1_COD = D3.D3_COD
INNER JOIN SBM010 BM ON BM.BM_GRUPO = B1.B1_GRUPO
WHERE D3.D3_EMISSAO >= '20190101'
AND D3_TM > '500'
AND D3_LOCAL NOT IN ('05','06','07','11')
AND B1.B1_TIPO IN ('MP','PA','II','OI','ME')
AND D3.D_E_L_E_T <> ''
AND B1.DE_L_E_T <> ''
AND BM.DE_L_E_T <> '*'