MYSQL sum() for distinct rows(MYSQL sum() 用于不同的行)
问题描述
我正在寻求在 SQL 查询中使用 sum() 的帮助:
I'm looking for help using sum() in my SQL query:
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
因为我在做分组依据",这确保同一行不会被计算多次.
I use DISTINCT
because I'm doing "group by" and this ensures the same row is not counted more than once.
问题是 SUM(conversions.value) 对每一行的值"计算不止一次(由于分组原因)
The problem is that SUM(conversions.value) counts the "value" for each row more than once (due to the group by)
我基本上想为每个 DISTINCT conversions.id 做 SUM(conversions.value)
.
I basically want to do SUM(conversions.value)
for each DISTINCT conversions.id.
这可能吗?
推荐答案
我可能错了,但据我所知
I may be wrong but from what I understand
- conversions.id 是表的主键conversions
- stats.id 是表的主键stats
- conversions.id is the primary key of your table conversions
- stats.id is the primary key of your table stats
因此,对于每个 Conversions.id,您最多会受到一个 links.id 的影响.
Thus for each conversions.id you have at most one links.id impacted.
你的请求有点像做2组的笛卡尔积:
You request is a bit like doing the cartesian product of 2 sets :
[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]) 行
and for each link, you get sizeof([clicks]) x sizeof([conversions]) lines
正如您所指出的,您的请求中的唯一转化次数可以通过
As you noted the number of unique conversions in your request can be obtained via a
count(distinct conversions.id) = sizeof([conversions])
这个独特的设法删除了笛卡尔积中的所有 [clicks] 行
this distinct manages to remove all the [clicks] lines in the cartesian product
但很明显
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;
给我发消息!杰罗姆
这篇关于MYSQL sum() 用于不同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MYSQL sum() 用于不同的行


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01