21,886
社区成员
发帖
与我相关
我的任务
分享
SELECT company.name AS name, AVG( sort1 ) AS avgsort1, AVG( sort2 ) AS avgsort2
FROM company, dp
WHERE STATUS =1
GROUP BY dp.userid
SELECT c.*,AVG(d.sort1) avgsort1,AVG(d.sort2) avgsort2
FROM company c,dp d
WHERE c.userid=d.userid AND c.status=1
GROUP BY c.userid;
--测试数据
if Object_id('company') is not null
drop table company
create table company(
id bigint not null,
[name] varchar(50) not null,
state int not null
)
insert into company
select '1','天才','1'
union all
select '2','英雄','1'
union all
select '3','鬼才','0'
if Object_id('dp') is not null
drop table dp
create table dp(
gid bigint not null,
id bigint not null,
sort1 float not null,
sort2 float not null
)
insert into dp
select '1','1','4','3'
union all
select '2','2','2','3'
union all
select '3','2','2','1'
union all
select '4','2','3','2'
union all
select '5','2','3','2'
--查询
select a.[name],b.sort1,b.sort2 from ((select * from company where state='1')a left join (select id,avg(sort1*1.0) as sort1,avg(sort2*1.0) as sort2 from dp group by id)b on a.id=b.id)
--结果
天才 4.000000 3.000000
英雄 2.500000 2.000000