我有一个SQL查询,该查询将几个表联接在一起,从而在两行中产生重复。我使用DISTINCT
关键字来消除重复项:
SELECT DISTINCT
o.day as day,
g.id AS id,
g.name AS name,
o.num AS num,
o.version as version
FROM
table_one o
INNER JOIN table_two t ON
o.ID = t.ID
INNER JOIN table_three g ON
t.ID = g.ID
INNER JOIN table_four gs ON
g.ID = gs.ID
AND
INNER JOIN table_five s ON
gs.ID = s.ID
INNER JOIN table_six z ON
s.ID = z.ID
INNER JOIN table_seven bg ON
bg.ID = g.ID;
这将返回我想要的两行,否则,如果我不使用DISTINCT
,我将看到重复的行:
1/2/19, 5, first, 25, 1
1/5/19, 7, second, 20, 1
如果我删除DISTINCT
,则两行重复,给了我四行:
1/2/19, 5, first, 25, 1
1/2/19, 5, first, 25, 1
1/5/19, 7, second, 20, 1
1/5/19, 7, second, 20, 1
因此,我的最终目标是使用GROUP BY
函数,以便我可以将o.num
字段加起来并按其余字段分组。如果我将GROUP BY
函数添加到上面的查询中,如下所示:
SELECT DISTINCT
o.day as day,
g.id AS id,
g.name AS name,
SUM(o.num) AS num,
o.version as version
FROM
table_one o
INNER JOIN table_two t ON
o.ID = t.ID
INNER JOIN table_three g ON
t.ID = g.ID
INNER JOIN table_four gs ON
g.ID = gs.ID
AND
INNER JOIN table_five s ON
gs.ID = s.ID
INNER JOIN table_six z ON
s.ID = z.ID
INNER JOIN table_seven bg ON
bg.ID = g.ID
GROUP BY
o.day as day,
g.id AS id,
g.name AS name,
o.version as version;
我退回了两行,但是o.num
的数量增加了一倍(基本上是在没有GROUP BY
的情况下执行DISTINCT
:
1/2/19, 5, first, 50, 1
1/5/19, 7, second, 40, 1
注意:您可能想知道为什么我在第一次查询中得到想要的结果时尝试使用GROUP BY
。我只包括要复制的行。由于某些原因,所有其他行都没有看到此行为。有什么方法可以使GROUP BY
和DISTINCT
一起工作?
答案 0 :(得分:4)
如果您要删除重复项然后求和,请将该查询插入子查询中。
select day, id, name, sum(num) num, version
from (
-- your query here with DISTINCT clause
)
group by day, id, name, version
如果重复,则连接条件可能存在一些问题。对我来说不难判断,也不知道数据集。
答案 1 :(得分:2)
您可以使用SUM(DISTINCT o.num)
:
SELECT o.day as day,
g.id AS id,
g.name AS name,
SUM(DISTINCT o.num) AS num,
o.version as version
FROM table_one o
INNER JOIN table_two t ON o.ID = t.ID
INNER JOIN table_three g ON t.ID = g.ID
INNER JOIN table_four gs ON g.ID = gs.ID
INNER JOIN table_five s ON gs.ID = s.ID
INNER JOIN table_six z ON s.ID = z.ID
INNER JOIN table_seven bg ON bg.ID = g.ID
GROUP BY o.day as day,
g.id AS id,
g.name AS name,
o.version as version;