一尘不染

MYSQL sum()用于不同的行

mysql

我在SQL查询中使用sum()寻找帮助:

SELECT links.id, 
       count(DISTINCT stats.id) as clicks, 
       count(DISTINCT conversions.id) as conversions, 
       sum(conversions.value) as conversion_value 
FROM links 
LEFT OUTER JOIN stats ON links.id = stats.parent_id 
LEFT OUTER JOIN conversions ON links.id = conversions.link_id 
GROUP BY links.id 
ORDER BY links.created desc;

我使用它DISTINCT是因为我正在执行“分组依据”,这可以确保同一行不会被重复计数。

问题是SUM(conversions.value)对每行的“值”计数超过一次(由于分组依据)

我基本上想SUM(conversions.value)为每个DISTINCT conversions.id 做。

那可能吗?


阅读 284

收藏
2020-05-17

共1个答案

一尘不染

我可能是错的,但据我了解

  • conversions.id* 是表 转换主键 *
  • stats.id* 是表 统计信息主键 *

因此,对于每个conversions.id,您最多影响一个link.id。

您的要求有点像做2组的笛卡尔积:

[clicks]
SELECT *
FROM links 
LEFT OUTER JOIN stats ON links.id = stats.parent_id

[conversions]
SELECT *
FROM links 
LEFT OUTER JOIN conversions ON links.id = conversions.link_id

对于每个链接,您将获得sizeof([clicks])x sizeof([conversions])行

如前所述,您可以通过以下方式获得请求中唯一身份转化的次数:

count(distinct conversions.id) = sizeof([conversions])

这种独特的方法可以删除笛卡尔积中的所有[clicks]行

但显然

sum(conversions.value) = sum([conversions].value) * sizeof([clicks])

就您而言,

count(*) = sizeof([clicks]) x sizeof([conversions])
count(*) = sizeof([clicks]) x count(distinct conversions.id)

你有

sizeof([clicks]) = count(*)/count(distinct conversions.id)

所以我会用

SELECT links.id, 
   count(DISTINCT stats.id) as clicks, 
   count(DISTINCT conversions.id) as conversions, 
   sum(conversions.value)*count(DISTINCT conversions.id)/count(*) as conversion_value 
FROM links 
LEFT OUTER JOIN stats ON links.id = stats.parent_id 
LEFT OUTER JOIN conversions ON links.id = conversions.link_id 
GROUP BY links.id 
ORDER BY links.created desc;

让我发布!杰罗姆

2020-05-17