3
Good!
I have two tables:
A: PRENUMERE (ID), DATE, STORAGE, TYPE
B: Autoreg (ID), PRENUMERE, PRODUCT
And I wanted to have a result like:
Totalcombustible, Totalloja, Misto
Where the Fuel is when the product is = 1, the Store is >1 and the mixed the two
If I see all Rows from the table I have give this:
Now I made the call like this:
SELECT
(SELECT COUNT(DISTINCT [VendasPOS_Linhas].[PRENUMERO])
FROM
[VendasPOS_Linhas]
JOIN [VendasPOS_Cabecalhos]
ON [VendasPOS_Linhas].[PRENUMERO] = [VendasPOS_Cabecalhos].[PRENUMERO]
WHERE
[VendasPOS_Cabecalhos].[FACT_VD] like 'T' AND
[VendasPOS_Cabecalhos].[DATA] > '2015-06-01 00:00:00.000' AND
[VendasPOS_Cabecalhos].[Armazem] = '454' AND
[VendasPOS_Linhas].[PRODUTO] > 1) AS QtdLoja,
(SELECT COUNT(DISTINCT [VendasPOS_Linhas].[PRENUMERO])
FROM
[VendasPOS_Linhas]
JOIN [VendasPOS_Cabecalhos]
ON [VendasPOS_Linhas].[PRENUMERO] = [VendasPOS_Cabecalhos].[PRENUMERO]
WHERE
[VendasPOS_Cabecalhos].[FACT_VD] like 'T' AND
[VendasPOS_Cabecalhos].[DATA] > '2015-06-01 00:00:00.000' AND
[VendasPOS_Cabecalhos].[Armazem] = '454' AND
[VendasPOS_Linhas].[PRODUTO] = 1) AS QtdCombustivel,
(SELECT COUNT(DISTINCT [VendasPOS_Cabecalhos].[PRENUMERO]) from VendasPOS_Linhas JOIN [VendasPOS_Cabecalhos]
ON [VendasPOS_Linhas].[PRENUMERO] = [VendasPOS_Cabecalhos].[PRENUMERO] where DATA> '2015-06-01 00:00:00.000' and Armazem = '454' AND [VendasPOS_Cabecalhos].[FACT_VD] like 'T') AS Total
And the result is this:
But right here I see some mistakes.
Total looks like it’s too much.
2º The totalComb and Totalloja are counting those that are mixed as well
3º In line 11 and 12 we can see that it has PRENUMERES equal but the product is both >1 and in the bottom line = 1 and this is part of the mixed. How do I see if a PRENUMERO has either 1 or >1 and it gives different lines. I mean, I have to see which are the duplicates and see if the same PRENUMERE has = 1 and >1 but not if it is possible.
RESOLVED:
SELECT
(SELECT COUNT(DISTINCT [VendasPOS_Linhas].[PRENUMERO]) FROM [VendasPOS_Linhas]
INNER JOIN
(
SELECT [VendasPOS_Linhas].[PRENUMERO]
FROM
[VendasPOS_Linhas]
JOIN [VendasPOS_Cabecalhos]
ON [VendasPOS_Linhas].[PRENUMERO] = [VendasPOS_Cabecalhos].[PRENUMERO]
WHERE
[VendasPOS_Cabecalhos].[FACT_VD] like 'T' AND
[VendasPOS_Cabecalhos].[DATA] > '2015-06-01 00:00:00.000' AND
[VendasPOS_Cabecalhos].[Armazem] = '454'
GROUP BY [VendasPOS_Linhas].[PRENUMERO]
HAVING COUNT(DISTINCT [VendasPOS_Linhas].[PRODUTO]) = 1
) combustivel ON combustivel.PRENUMERO = [VendasPOS_Linhas].[PRENUMERO]
WHERE [VendasPOS_Linhas].[PRODUTO] = 1
) AS QtdeCombustivel,
(SELECT COUNT(DISTINCT [VendasPOS_Linhas].[PRENUMERO]) FROM [VendasPOS_Linhas]
INNER JOIN
(
SELECT [VendasPOS_Linhas].[PRENUMERO]
FROM
[VendasPOS_Linhas]
JOIN [VendasPOS_Cabecalhos]
ON [VendasPOS_Linhas].[PRENUMERO] = [VendasPOS_Cabecalhos].[PRENUMERO]
WHERE
[VendasPOS_Cabecalhos].[FACT_VD] like 'T' AND
[VendasPOS_Cabecalhos].[DATA] > '2015-06-01 00:00:00.000' AND
[VendasPOS_Cabecalhos].[Armazem] = '454'
GROUP BY [VendasPOS_Linhas].[PRENUMERO]
HAVING COUNT(DISTINCT [VendasPOS_Linhas].[PRODUTO]) = 1
) loja ON loja.PRENUMERO = [VendasPOS_Linhas].[PRENUMERO]
WHERE [VendasPOS_Linhas].[PRODUTO] > 1
) AS QtdeLoja,
(SELECT COUNT(DISTINCT [VendasPOS_Linhas].[PRENUMERO]) FROM [VendasPOS_Linhas]
INNER JOIN
(
SELECT [VendasPOS_Linhas].[PRENUMERO]
FROM
[VendasPOS_Linhas]
JOIN [VendasPOS_Cabecalhos]
ON [VendasPOS_Linhas].[PRENUMERO] = [VendasPOS_Cabecalhos].[PRENUMERO]
WHERE
[VendasPOS_Cabecalhos].[FACT_VD] like 'T' AND
[VendasPOS_Cabecalhos].[DATA] > '2015-06-01 00:00:00.000' AND
[VendasPOS_Cabecalhos].[Armazem] = '454'
GROUP BY [VendasPOS_Linhas].[PRENUMERO]
HAVING COUNT(DISTINCT [VendasPOS_Linhas].[PRODUTO]) > 1
) mista ON mista.PRENUMERO = [VendasPOS_Linhas].[PRENUMERO]
) AS QtdeMista
Your
total
may be returning more because you didn’t do theJoin
to make sure that will only count those who have relationship. As for the rest is a little confused for me :(– Maicon Carraro
Yes that was missing, I fixed the total and it appears correctly now. For the rest, here is an image to see if it helps: ! http://i.imgur.com/toYKknJ.png
– Enato
What is DBMS? SQL Server? Mysql?
– bruno
@Bruno It is Sqlserver
– Maicon Carraro