简体   繁体   English

如何找到拥有比所有用户平均“计数”高的“计数”数量的“成员”?

[英]How to find 'members' who have a higher number of 'counts' than the average 'count' for all users?

I have two tables; 我有两个桌子。 Members and streams. 成员和流。 I want to find the average number of streams for all members and then see which individual members (including first name) have streamed more than the average number of streams. 我想查找所有成员的平均流数,然后查看哪些单个成员(包括名字)流过的数据流大于平均流数。

Currently i have this query which finds the average number of streams and the number of streams each member has; 目前,我有这个查询,可以找到平均流数和每个成员拥有的流数;

select stream.m_ID, count(*), (select avg (count(*)) from stream group by stream.m_ID) from stream group by stream.m_ID;

I am struggling to find how to discard member who have a lower number of streams than the average number AND to join the member and stream table together 我正在努力寻找如何丢弃流数量少于平均数量的成员并将成员和流表连接在一起的方法

CREATE TABLE "M_5006677"."MEMBER" 
   (    "M_ID" NUMBER(2,0), 
    "FNAME" VARCHAR2(15 BYTE) NOT NULL ENABLE, 
    "MNAME" VARCHAR2(15 BYTE), 
    "LNAME" VARCHAR2(15 BYTE) NOT NULL ENABLE, 
    "EMAIL" VARCHAR2(25 BYTE) NOT NULL ENABLE, 
    "R_DATE" DATE NOT NULL ENABLE, 
    "L_DATE" DATE, 
     CONSTRAINT "PK_M_ID" PRIMARY KEY ("M_ID")

CREATE TABLE "M_5006677"."STREAM" 
   (    "STREAM_ID" NUMBER(2,0), 
    "SDATE" DATE, 
    "SPOSITION" NUMBER(5,2), 
    "F_ID" NUMBER(2,0) NOT NULL ENABLE, 
    "M_ID" NUMBER(2,0) NOT NULL ENABLE, 
    "LIVE" VARCHAR2(3 BYTE), 
     CONSTRAINT "PK_STREAM" PRIMARY KEY ("STREAM_ID")
CONSTRAINT "FK_F_ID" FOREIGN KEY ("F_ID") REFERENCE FILM
CONSTRAINT "FK_M_ID" FOREIGN KEY ("M_ID") REFERENCE MEMBER

I would use window functions. 我会使用窗口功能。 If you only want to count members who have ever streamed: 如果您只想统计曾经直播过的成员:

select m_id
from (select m_id, count(*) as cnt, avg(count(*)) over () as avg_overall
      from streams
      group by m_id
     ) s
where cnt > avg_overall;

If you want to count members who never streamed: 如果要计算从未流过的成员:

select m_id
from (select m_id, count(*) as cnt, avg(count(s.m_id)) over () as avg_overall
      from members m left join
           streams s
           on m.m_id = s.m_id
      group by m_id
     ) s
where cnt > avg_overall;

This would work for sql server: 这将适用于SQL Server:

select stream.m_ID, count(*), (select avg (count(*)) from stream group by stream.m_ID) from stream group by stream.m_ID 
having count(*) < (select avg (count(*)) from stream group by stream.m_ID)

If you want also get data about members then you should include M_ID into your query so you can join with members table like that: 如果您还希望获取有关成员的数据,则应在查询中包含M_ID,以便可以像这样与members表联接:

select stream.m_ID, M_ID, count(*), (select avg (count(*)) from stream group by stream.m_ID) from stream group by stream.m_ID ,M_ID
having count(*) < (select avg (count(*)) from stream group by stream.m_ID)

after that you can join on M_ID with Members table: 之后,您可以使用Members表加入M_ID:

select FNAME from Member m
join (select stream.m_ID, M_ID, count(*), (select avg (count(*)) from stream 
group by stream.m_ID) from stream group by stream.m_ID ,M_ID
    having count(*) < (select avg (count(*)) from stream group by 
stream.m_ID)) X on m.M_ID = X.M_ID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每天查看活动表中所有用户的计数,而不会丢失计数为0的用户 - Viewing counts for all users in an activity table by the day without losing users who have 0 counts 获取 2018 年 3 月薪水高于其之前所有月份平均月薪的员工姓名 - obtain the employee names who made a higher salary in March 2018 than their average monthly salary for all previous months 设置计数大于指定数字的标志 - set flag who have count greater than a specified number 选择计数值高于平均计数值的每个计数行 - Select every counted row which count-number is higher than the average count-numbers SQL:计数值高于组的平均值 - SQL: Count values higher than average for a group 想要计算出现次数,则只打印高于所有计数值平均值的计数值 - Want to count occurrence, then only print count values which are higher than the average of all counted values 试图显示工资高于平均工资的导师姓名 - Trying to display the tutor names who salary is higher than the average salary 如何查找所有在同一日期内完成一项以上考试的学生整个考试期间 - How to find all students who have more than one exams in same date for whole period of exams 我如何找到部门的平均工资高于公司的平均工资? - How do I find departments with higher average salary than the average salary of the company? 如何选择提交超过10个提交的所有用户 - How to select all users who made more than 10 submissions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM