<tfoot id='AKLbU'></tfoot>

    <legend id='AKLbU'><style id='AKLbU'><dir id='AKLbU'><q id='AKLbU'></q></dir></style></legend>
  • <small id='AKLbU'></small><noframes id='AKLbU'>

      • <bdo id='AKLbU'></bdo><ul id='AKLbU'></ul>

        <i id='AKLbU'><tr id='AKLbU'><dt id='AKLbU'><q id='AKLbU'><span id='AKLbU'><b id='AKLbU'><form id='AKLbU'><ins id='AKLbU'></ins><ul id='AKLbU'></ul><sub id='AKLbU'></sub></form><legend id='AKLbU'></legend><bdo id='AKLbU'><pre id='AKLbU'><center id='AKLbU'></center></pre></bdo></b><th id='AKLbU'></th></span></q></dt></tr></i><div id='AKLbU'><tfoot id='AKLbU'></tfoot><dl id='AKLbU'><fieldset id='AKLbU'></fieldset></dl></div>
      1. MYSQL sum() 用于不同的行

        时间:2023-06-02
          1. <i id='QZB8D'><tr id='QZB8D'><dt id='QZB8D'><q id='QZB8D'><span id='QZB8D'><b id='QZB8D'><form id='QZB8D'><ins id='QZB8D'></ins><ul id='QZB8D'></ul><sub id='QZB8D'></sub></form><legend id='QZB8D'></legend><bdo id='QZB8D'><pre id='QZB8D'><center id='QZB8D'></center></pre></bdo></b><th id='QZB8D'></th></span></q></dt></tr></i><div id='QZB8D'><tfoot id='QZB8D'></tfoot><dl id='QZB8D'><fieldset id='QZB8D'></fieldset></dl></div>
            <legend id='QZB8D'><style id='QZB8D'><dir id='QZB8D'><q id='QZB8D'></q></dir></style></legend>

            <small id='QZB8D'></small><noframes id='QZB8D'>

                <tbody id='QZB8D'></tbody>
              <tfoot id='QZB8D'></tfoot>

                  <bdo id='QZB8D'></bdo><ul id='QZB8D'></ul>
                  本文介绍了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 进入输出文件“拒绝访问"- 但我的用户有“全部"访问..文件夹是CHMOD 777 下一篇:mySQL 经纬度查询 x 英里半径内的其他行

                  相关文章

                  1. <small id='7N9PO'></small><noframes id='7N9PO'>

                    <i id='7N9PO'><tr id='7N9PO'><dt id='7N9PO'><q id='7N9PO'><span id='7N9PO'><b id='7N9PO'><form id='7N9PO'><ins id='7N9PO'></ins><ul id='7N9PO'></ul><sub id='7N9PO'></sub></form><legend id='7N9PO'></legend><bdo id='7N9PO'><pre id='7N9PO'><center id='7N9PO'></center></pre></bdo></b><th id='7N9PO'></th></span></q></dt></tr></i><div id='7N9PO'><tfoot id='7N9PO'></tfoot><dl id='7N9PO'><fieldset id='7N9PO'></fieldset></dl></div>

                    <legend id='7N9PO'><style id='7N9PO'><dir id='7N9PO'><q id='7N9PO'></q></dir></style></legend>

                        <bdo id='7N9PO'></bdo><ul id='7N9PO'></ul>
                      <tfoot id='7N9PO'></tfoot>