[英]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.