我正在寻找一个解决方案来计算联接表中的分组行。
表参与者
id | email | date
----+---------------------------+--------------
1 | john@example.com | 14/01/2015
2 | sally@mailing.com | 14/01/2015
3 | sally@mailing.com | 01/01/2015
4 | sally@mailing.com | 20/01/2015
5 | edward@halloworld.com | 13/02/2015
6 | edward@halloworld.com | 15/01/2015
表数据
pid | fieldid | data
----+---------+--------------
1 | 1 | Dutch
1 | 2 | Cheese
2 | 1 | German
2 | 2 | Soup
3 | 1 | German
3 | 2 | Bread
4 | 1 | German
4 | 2 | Sausage
5 | 1 | Italian
5 | 2 | Meat balls
6 | 1 | Italian
6 | 2 | Pizza
我想看看用户(根据他们的电子邮件地址)参与的频率。但是下面的查询会返回预期的结果:
SELECT
COUNT(*) AS participated,
p.email,
coalesce(MAX(case when fieldid = 1 then data end), 0) as "language"
FROM participants p
LEFT JOIN data d
ON d.pid = p.id
GROUP BY p.email
结果:
participated | email | language
-------------+---------------------------+------------
2 | john@example.com | Dutch
6 | sally@mailing.com | German
4 | edward@halloworld.com | Italian
'计数'结果不是我所期望的。它似乎算了两倍。这可能是因为参与者表中每条记录的数据表中有两条记录。
使用正确的查询是什么?
答案 0 :(得分:0)
如果您使用的数据加倍,那么您可能需要count(distinct d.pid)
。