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() 用于不同的行
基础教程推荐
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 在多列上分布任意行 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
