SQL - 使用Union All并添加重复项

时间:2016-11-14 02:17:15

标签: sql postgresql

我正在使用Union All进行查询,我想确保将重复的行添加到结果查询中。使用“全部联盟”时,不会正确添加重复的行。当谷歌搜索“sql union with duplicates”时,它声明:“联盟将删除重复项.Union All不会。您在查询的第一部分中没有重复项(可能由左连接生成)。据我了解UNION,它不会添加到已经存在的结果集行,但它不会删除第一个数据集中已存在的重复项。“在我的例子中,我想要添加已经存在的结果集行。我该怎么做?

这是我的问题:

SELECT player_name_fkey, school_name_fkey, score_simple, sum(score_simple) over (partition by player_name_fkey) as sum_scores

FROM (

    SELECT player_name_fkey, school_name_fkey, SUM(score) as score_simple 

        FROM simple LEFT JOIN players on players.name=simple.player_name_fkey 

        WHERE category='Cadet' AND gender='Féminin' GROUP BY player_name_fkey, school_name_fkey

    UNION ALL

        SELECT player_name_fkey, school_name_fkey, SUM(score) as score_simple

        FROM double LEFT JOIN players on players.name=double.player_name_fkey 

        WHERE category='Cadet' AND gender='Féminin' GROUP BY player_name_fkey, school_name_fkey) t1

GROUP BY player_name_fkey, school_name_fkey, score_simple

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

如Felix Pamittan所述,最后删除GROUP BY条款解决了我的问题。

var firstFunction = function() { alert('First') };
var secondFunction = function() { alert('Second') };
$('#press').click(function() {
  firstFunction();
  secondFunction();
});