Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 多对多表分组_Sql_Postgresql_Group By - Fatal编程技术网

Sql 多对多表分组

Sql 多对多表分组,sql,postgresql,group-by,Sql,Postgresql,Group By,我有下表: performance --id --color --installs --date performance_groups --id --performance_id --group_id 我想要一个类似以下内容的SQL: SELECT color, targeting_id, SUM(installs) as installs FROM performance, performance_groups GROUP BY color, group_i

我有下表:

performance
  --id
  --color
  --installs
  --date

performance_groups
  --id
  --performance_id
  --group_id
我想要一个类似以下内容的SQL:

 SELECT color, targeting_id, SUM(installs) as installs
 FROM performance, performance_groups
GROUP BY color, group_id
color group_ids installs
Blue  1,2     15
Red   3       10
Blue  1,3     10
但我希望对所有组进行分组

例如:

performance
id     color     installs   date
1      Blue      5          2017-07-05
2      Red       10         2017-07-04
3      Blue      10         2017-07-04
4      Blue      10         2017-07-05

performance_groups
id   performance_id   group_id
1    1                1
2    1                2
3    2                3
4    3                1
5    3                2
6    4                1
7    4                3
我希望得到如下结果:

 SELECT color, targeting_id, SUM(installs) as installs
 FROM performance, performance_groups
GROUP BY color, group_id
color group_ids installs
Blue  1,2     15
Red   3       10
Blue  1,3     10
切勿在
FROM
子句中使用逗号。始终使用正确、明确的
JOIN
语法

然后,您的查询似乎是一个
JOIN
groupby

select p.color, string_agg(pg.group_id) as groups, 
       sum(installs) as installs
from performance p join
     performance_groups pg
     on pg.performance_id = p.id
group by color;

使用
array\u agg
,不要忘记不同的字符。联接可能会产生重复项

select p.color, array_agg(distinct pg.group_id) as groups, 
       sum(distinct installs) as installs
from performance p 
join performance_groups pg
     on pg.performance_id = p.id
group by color;

代码:

    select f.color,
    (SELECT STUFF(( SELECT ',' + convert(varchar(5),j.group_id) 
    FROM (select distinct g.group_id from performance_groups g 
    where g.performance_id in (select id from performance where color= 
    f.color)) j FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, 
    '')) as group_ids,
    SUM(f.installs) installs
    from performance f group by color
输出:

color   group_ids   installs
Blue    1,2         15
Red     3           10

多亏了戈登和镭,我的答案才有了灵感

最后的解决办法是:

with pg as(
    select performance_id, array_agg(distinct group_id) as groups from performance_groups
    group by performance_id
) 
select groups, sum(installs) from performance join pg 
on pg.performance_id = performance.id
group by groups

请参见工作

谢谢。我正在检查-您能详细说明join和逗号的区别吗?
join
是正确的语法。逗号是过时了二十多年的古老语法。听起来是正确的答案。将检查更多和更新,但我正在使用postgres sql-它有什么帮助?