<tfoot id='Ttbzd'></tfoot>
    1. <legend id='Ttbzd'><style id='Ttbzd'><dir id='Ttbzd'><q id='Ttbzd'></q></dir></style></legend>

    2. <small id='Ttbzd'></small><noframes id='Ttbzd'>

        <bdo id='Ttbzd'></bdo><ul id='Ttbzd'></ul>

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

      2. SQL 平均(计数(*))?

        时间:2023-06-06

      3. <small id='rAtA1'></small><noframes id='rAtA1'>

          <tfoot id='rAtA1'></tfoot>
              <tbody id='rAtA1'></tbody>
            <i id='rAtA1'><tr id='rAtA1'><dt id='rAtA1'><q id='rAtA1'><span id='rAtA1'><b id='rAtA1'><form id='rAtA1'><ins id='rAtA1'></ins><ul id='rAtA1'></ul><sub id='rAtA1'></sub></form><legend id='rAtA1'></legend><bdo id='rAtA1'><pre id='rAtA1'><center id='rAtA1'></center></pre></bdo></b><th id='rAtA1'></th></span></q></dt></tr></i><div id='rAtA1'><tfoot id='rAtA1'></tfoot><dl id='rAtA1'><fieldset id='rAtA1'></fieldset></dl></div>
            <legend id='rAtA1'><style id='rAtA1'><dir id='rAtA1'><q id='rAtA1'></q></dir></style></legend>
              • <bdo id='rAtA1'></bdo><ul id='rAtA1'></ul>

                • 本文介绍了SQL 平均(计数(*))?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我试图找出一个值在列中出现的平均次数,根据另一列对其进行分组,然后对其进行计算.

                  I'm trying to find out the average number of times a value appears in a column, group it based on another column and then perform a calculation on it.

                  我有 3 张桌子,有点像这样

                  I have 3 tables a little like this

                  DVD
                  
                  ID | NAME
                  1  | 1       
                  2  | 1     
                  3  | 2      
                  4  | 3
                  
                  COPY 
                  
                  ID | DVDID   
                  1  | 1  
                  2  | 1  
                  3  | 2  
                  4  | 3  
                  5  | 1
                  
                  LOAN
                  
                  ID | DVDID | COPYID  
                  1  | 1     |  1  
                  2  | 1     |  2  
                  3  | 2     |  3    
                  4  | 3     |  4  
                  5  | 1     |  5
                  6  | 1     |  5
                  7  | 1     |  5
                  8  | 1     |  2
                  

                  基本上,我试图找到出借表中出现的所有副本 ID,其次数少于该 DVD 的所有副本的平均次数.

                  Basically, I'm trying to find all the copy ids that appear in the loan table LESS times than the average number of times for all copies of that DVD.

                  因此在上面的示例中,dvd 1 的副本 5 出现了 3 次,副本 2 两次,副本 1 一次,因此该 DVD 的平均值为 2.我想列出该 DVD 的所有副本(以及每个副本)在 Loan 表中出现的数字小于该数字.

                  So in the example above, copy 5 of dvd 1 appears 3 times, copy 2 twice and copy 1 once so the average for that DVD is 2. I want to list all the copies of that (and each other) dvd that appear less than that number in the Loan table.

                  我希望这更有意义...

                  I hope that makes a bit more sense...

                  谢谢

                  推荐答案

                  类似于 dotjoe 的解决方案,但使用解析函数来避免额外的连接.可能或多或少有效率.

                  Similar to dotjoe's solution, but using an analytic function to avoid the extra join. May be more or less efficient.

                  with 
                  loan_copy_total as 
                  (
                      select dvdid, copyid, count(*) as cnt
                      from loan
                      group by dvdid, copyid
                  ),
                  loan_copy_avg as
                  (
                      select dvdid, copyid, cnt, avg(cnt) over (partition by dvdid) as copy_avg
                      from loan_copy_total
                  )
                  select *
                  from loan_copy_avg lca
                  where cnt <= copy_avg;
                  

                  这篇关于SQL 平均(计数(*))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Oracle 将行转为列 下一篇:嵌套聚合函数

                  相关文章

                  最新文章

                • <legend id='ayqo7'><style id='ayqo7'><dir id='ayqo7'><q id='ayqo7'></q></dir></style></legend>

                  <tfoot id='ayqo7'></tfoot>

                  • <bdo id='ayqo7'></bdo><ul id='ayqo7'></ul>
                    <i id='ayqo7'><tr id='ayqo7'><dt id='ayqo7'><q id='ayqo7'><span id='ayqo7'><b id='ayqo7'><form id='ayqo7'><ins id='ayqo7'></ins><ul id='ayqo7'></ul><sub id='ayqo7'></sub></form><legend id='ayqo7'></legend><bdo id='ayqo7'><pre id='ayqo7'><center id='ayqo7'></center></pre></bdo></b><th id='ayqo7'></th></span></q></dt></tr></i><div id='ayqo7'><tfoot id='ayqo7'></tfoot><dl id='ayqo7'><fieldset id='ayqo7'></fieldset></dl></div>
                  1. <small id='ayqo7'></small><noframes id='ayqo7'>