0
点赞
收藏
分享

微信扫一扫

sql_每组占比问题

单调先生 2022-07-18 阅读 71


一张表有 group ,tablename,staragesize(存储大小)几个字段,
求出每组存储大小占当比前80%的表,返回group,tablename

select  
a.group,
a.tablename
from
table as a
join
(
select
group,
sum(staragesize)*0.2 as sum_size20
from
group
group by group
) as b
where a.group=b.group
having a.staragesize>a.sum_size20 ;

求某张表里的某个种类的占比问题
原来我写的这样,但是一直报错

select
space_id,status, sum_status/sum_id
from
(
select
select space_id, status,count(*) as sum_status
from space_data_info
where floor_num=11 and date_time
between '2020-11-16 07:00:00' and '2020-11-20 21:00:00' and module= 'comfort'
group by space_id, status
) a
join
(
select space_id, count(*) as sum_id
from space_data_info
where floor_num=11 and date_time
between '2020-11-16 07:00:00' and '2020-11-20 21:00:00' and module= 'comfort'
group by space_id
) b
where a.space_id=b.space_id;

sql_每组占比问题_字段


很是最后改了这里,好了

sql_每组占比问题_字段_02

select
a.space_id,a.status,a.sum_status/b.sum_id
from
(
select space_id, status,count(*) as sum_status
from space_data_info
where floor_num=11 and date_time
between '2020-11-16 07:00:00' and '2020-11-16 21:00:00' and module= 'comfort'
group by space_id, status
) a
join
(
select space_id, count(*) as sum_id
from space_data_info
where floor_num=11 and date_time
between '2020-11-16 07:00:00' and '2020-11-20 21:00:00' and module= 'comfort'
group by space_id
) b
where a.space_id=b.space_id;


举报

相关推荐

0 条评论