Reputation: 33
I'm using the following query to count all scripts runs, but when there are 2 types for a software (pacote column), it doubles the count result.
SELECT
p.produto, p.pacote, COUNT(p.produto) AS Execuções, t.tempo_minutos AS [Tempo Médio (Manual)]
FROM
[log].pdq AS p
INNER JOIN
infra.tempo_medio_execucao AS t ON t.produto = p.produto
WHERE
(p.status IS NOT NULL) AND (p.status = 'Sucesso') AND (p.produto = 'SafeSign')
GROUP BY
p.produto, p.pacote, t.tempo_minutos
This query is returning the following result:
╔══════════╦═══════════════╦═══════════╦══════════════════════╗
║ produto ║ pacote ║ Execuções ║ Tempo Médio (Manual) ║
╠══════════╬═══════════════╬═══════════╬══════════════════════╣
║ SafeSign ║ Desinstalação ║ 6 ║ 20 ║
║ SafeSign ║ Instalação ║ 18 ║ 20 ║
╚══════════╩═══════════════╩═══════════╩══════════════════════╝
But if I remove the INNER JOIN part of the code, it returns the desired count value. But my query requires an INNER JOIN with infra.tempo_medio_execucao table.
SELECT
p.produto, p.pacote, COUNT(p.produto) AS Execuções
FROM
[log].pdq AS p
WHERE
(p.status IS NOT NULL) AND (p.status = 'Sucesso') AND (p.produto = 'SafeSign')
GROUP BY
p.produto, p.pacote
This new query will produce the following result (which is the expected result when executing the INNER JOIN query):
╔══════════╦═══════════════╦═══════════╗
║ produto ║ pacote ║ Execuções ║
╠══════════╬═══════════════╬═══════════╣
║ SafeSign ║ Desinstalação ║ 3 ║
║ SafeSign ║ Instalação ║ 9 ║
╚══════════╩═══════════════╩═══════════╝
Here are some additional information from the tables related to this query:
Data from infra.tempo_medio_execucao:
╔══════════╦═══════════════╦═══════════════╗
║ produto ║ pacote ║ tempo_minutos ║
╠══════════╬═══════════════╬═══════════════╣
║ SafeSign ║ Instalação ║ 20 ║
║ SafeSign ║ Desinstalação ║ 20 ║
╚══════════╩═══════════════╩═══════════════╝
Data from log.pdq:
╔══════════╦═══════════════╦═════════════════════════╦═════════════════════════╦══════════════════╦═════════╗
║ produto ║ pacote ║ inicio ║ fim ║ duracao ║ status ║
╠══════════╬═══════════════╬═════════════════════════╬═════════════════════════╬══════════════════╬═════════╣
║ SafeSign ║ Instalação ║ 2018-11-09 13:06:00.000 ║ 2018-11-09 13:07:29.000 ║ 00:01:28.0000000 ║ Sucesso ║
║ SafeSign ║ Desinstalação ║ 2018-11-09 13:21:19.000 ║ 2018-11-09 13:21:20.000 ║ 00:00:00.0000000 ║ Sucesso ║
║ SafeSign ║ Instalação ║ 2018-11-09 13:27:52.000 ║ 2018-11-09 13:28:55.000 ║ 00:01:03.0000000 ║ Sucesso ║
║ SafeSign ║ Instalação ║ 2018-11-09 13:34:08.000 ║ 2018-11-09 13:34:58.000 ║ 00:00:50.0000000 ║ Sucesso ║
║ SafeSign ║ Desinstalação ║ 2018-11-09 13:38:19.000 ║ 2018-11-09 13:38:19.000 ║ 00:00:00.0000000 ║ Sucesso ║
║ SafeSign ║ Desinstalação ║ 2018-11-09 14:04:34.000 ║ 2018-11-09 14:04:35.000 ║ 00:00:00.0000000 ║ Sucesso ║
║ SafeSign ║ Instalação ║ 2018-11-09 14:09:41.000 ║ 2018-11-09 14:10:15.000 ║ 00:00:33.0000000 ║ Sucesso ║
║ SafeSign ║ Instalação ║ 2018-11-09 14:09:57.000 ║ 2018-11-09 14:11:49.000 ║ 00:01:52.0000000 ║ Sucesso ║
║ SafeSign ║ Instalação ║ 2018-11-09 13:17:01.000 ║ 2018-11-09 13:19:37.000 ║ 00:02:36.0000000 ║ Sucesso ║
║ SafeSign ║ Instalação ║ 2018-11-09 13:17:01.000 ║ 2018-11-09 13:17:44.000 ║ 00:00:42.0000000 ║ Sucesso ║
║ SafeSign ║ Instalação ║ 2018-11-09 14:28:16.000 ║ 2018-11-09 14:30:32.000 ║ 00:02:15.0000000 ║ Sucesso ║
║ SafeSign ║ Instalação ║ 2018-11-09 15:30:34.000 ║ 2018-11-09 15:31:06.000 ║ 00:00:32.0000000 ║ Sucesso ║
╚══════════╩═══════════════╩═════════════════════════╩═════════════════════════╩══════════════════╩═════════╝
As per log.pdq table, there are 12 entries. 3 from Desinstalação and 9 from Instalação.
For some reason, when using count produto and not using the INNER JOIN function, it will return the correct count. But when using it, it will double the results.
I have already asked this question, but with poor formatting and info quality. Thus, it could not be answered. Then this question as created with better explanation, data, formatting, making it get answered in a few minutes.
Thanks all in advance.
Upvotes: 2
Views: 82
Reputation: 272106
You probably need to include the pacote
field in the JOIN
condition:
infra.tempo_medio_execucao AS t ON t.produto = p.produto AND t.pacote = p.pacote
Without this condition each row is multiplied by both rows in the tempo_medio_execucao table creating the duplicate.
Upvotes: 2