問題描述
我試圖找出一個(gè)值在列中出現(xiàn)的平均次數(shù),根據(jù)另一列對(duì)其進(jìn)行分組,然后對(duì)其進(jìn)行計(jì)算.
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 張桌子,有點(diǎn)像這樣
I have 3 tables a little like this
等
基本上,我試圖找到出借表中出現(xiàn)的所有副本 ID,其次數(shù)少于該 DVD 的所有副本的平均次數(shù).
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 出現(xiàn)了 3 次,副本 2 兩次,副本 1 一次,因此該 DVD 的平均值為 2.我想列出該 DVD 的所有副本(以及每個(gè)副本)在 Loan 表中出現(xiàn)的數(shù)字小于該數(shù)字.
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 的解決方案,但使用解析函數(shù)來避免額外的連接.可能或多或少有效率.
Similar to dotjoe's solution, but using an analytic function to avoid the extra join. May be more or less efficient.
這篇關(guān)于SQL 平均(計(jì)數(shù)(*))?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!