I am not able to run these queries in sql 2008 I took the test in 2014 and it worked could someone help me? I'm too close to the bridge to find a solution.
Follow the query:
SELECT D1_COD, D1_FORNECE, A2_NOME,
--1ª COMPRAS--
MIN(COMPRAS.D1_EMISSAO) AS '1ª COMPRA',
(SELECT D1_VUNIT FROM SD1010 AS COMPRAS1
WHERE COMPRAS1.D1_EMISSAO = (SELECT MIN(D1_EMISSAO) FROM SD1010 AS COMPRAS2
WHERE COMPRAS2.D1_COD = COMPRAS.D1_COD
AND COMPRAS2.D1_FORNECE = COMPRAS.D1_FORNECE
AND COMPRAS2.D_E_L_E_T_ <> '*'
GROUP BY D1_COD)
AND COMPRAS1.D_E_L_E_T_ <> '*'
AND COMPRAS1.D1_FORNECE = COMPRAS.D1_FORNECE
AND COMPRAS1.D1_COD = COMPRAS.D1_COD
GROUP BY D1_COD, D1_VUNIT
) AS 'PRC_1ª_COMPRA',
--ULT COMPRA--
MAX(COMPRAS.D1_EMISSAO) AS 'ULT COMPRA',
(SELECT D1_VUNIT FROM SD1010 AS COMPRAS1
WHERE COMPRAS1.D1_EMISSAO = (SELECT MAX(D1_EMISSAO) FROM SD1010 AS COMPRAS2
WHERE COMPRAS2.D1_COD = COMPRAS.D1_COD
AND COMPRAS2.D1_FORNECE = COMPRAS.D1_FORNECE
AND COMPRAS2.D_E_L_E_T_ <> '*'
GROUP BY D1_COD)
AND COMPRAS1.D_E_L_E_T_ <> '*'
AND COMPRAS1.D1_FORNECE = COMPRAS.D1_FORNECE
AND COMPRAS1.D1_COD = COMPRAS.D1_COD
GROUP BY D1_COD, D1_VUNIT
) AS 'PRC_ULT_COMPRA'
FROM SD1010 AS COMPRAS
INNER JOIN SA2010
ON COMPRAS.D1_FORNECE = A2_COD
AND COMPRAS.D1_LOJA = A2_LOJA
--AND COMPRAS.D1_COD >= '' AND COMPRAS.D1_COD <= --'ZZZZZZ'
AND COMPRAS.D1_FORNECE >= '' AND COMPRAS.D1_FORNECE <= 'ZZZZZZ'
GROUP BY COMPRAS.D1_COD, COMPRAS.D1_FORNECE, A2_NOME
ORDER BY D1_COD, D1_FORNECE
the other and this thing:
SELECT
--VENDAS--
D2_COD, B1_DESC, D2_FILIAL, SUM(D2_QUANT) AS QUANT_VEND,
MAX(D2_PRCVEN) AS PRECO_ULT_VENDA,
B0_PRV1 AS PRECO_ATUAL,
MAX(D2_EMISSAO) AS DATA_ULT_VENDA,
B1_GRUPO AS GRUPO,
--DEVOLUCOES--
(SELECT D1_QUANT FROM SD1010
WHERE D1_FILIAL = D2_FILIAL
AND D1_COD = D2_COD
-- AND D1_EMISSAO = D2_EMISSAO
AND D1_TIPO = 'D'
AND SD1010.D_E_L_E_T_ <> '*'
GROUP BY D1_COD, D1_EMISSAO, D1_QUANT
) AS DEVOLUCOES,
--ESTOQUE--
(SELECT B2_QATU FROM SB2010
WHERE B2_FILIAL = D2_FILIAL
AND B2_COD = D2_COD
AND SB2010.D_E_L_E_T_ <> '*'
GROUP BY B2_FILIAL, B2_QATU
) AS ESTOQUE,
(SELECT B2_CM1 FROM SB2010
WHERE B2_FILIAL = D2_FILIAL
AND B2_COD = D2_COD
AND SB2010.D_E_L_E_T_ <> '*'
GROUP BY B2_FILIAL, B2_CM1
) AS CUSTO
FROM SD2010 AS VENDAS
INNER JOIN SB1010 AS PRODUTO
ON D2_COD = B1_COD
LEFT JOIN SB0010 AS PRECO
ON D2_COD = B0_COD
INNER JOIN SB2010 AS ESTOQUE
ON D2_COD = B2_COD
AND D2_FILIAL = B2_FILIAL
AND VENDAS.D_E_L_E_T_ <> '*'
AND ESTOQUE.D_E_L_E_T_ <> '*'
GROUP BY D2_COD, B1_DESC, D2_FILIAL, B0_PRV1, B1_UPRC, B1_UCOM, B1_GRUPO
ORDER BY D2_COD, D2_FILIAL