您能帮我计算
的比例吗Table
user_id attendance Date
1 1 01.01.2018
2 Null 01.01.2018
3 1 02.01.2018
4 Null 03.01.2018
5 1 03.01.2018
如果用户出席,则为1,否则为-空。
需要计算01.01.18-02.01.18期间的出勤百分比。 例如,在2018年1月1日,两个用户中有1个用户(50%),在02.01.2018-100%。
谢谢!
答案 0 :(得分:0)
您可以尝试以下查询:
<div id="slider">
<figure>
<div class="page">
<div class="project">
<div class="col project-img">
<img src="https://via.placeholder.com/350x350">
</div>
<div class="col project-info">info 1</div>
</div>
</div>
<div class="page">
<div class="project">
<div class="col project-img">
<img src="https://via.placeholder.com/350x350">
</div>
<div class="col project-info">info 2</div>
</div>
</div>
<div class="page">
<div class="project">
<div class="col project-img">
<img src="https://via.placeholder.com/350x350">
</div>
<div class="col project-info">info 3</div>
</div>
</div>
<div class="page">
<div class="project">
<div class="col project-img">
<img src="https://via.placeholder.com/350x350">
</div>
<div class="col project-info">info 4</div>
</div>
</div>
<div class="page">
<div class="project">
<div class="col project-img">
<img src="https://via.placeholder.com/350x350">
</div>
<div class="col project-info">info 5</div>
</div>
</div>
</figure>
</div>
@keyframes slidy {
0% { left: 0%; }
20% { left: 0%; }
25% { left: -100%; }
45% { left: -100%; }
50% { left: -200%; }
70% { left: -200%; }
75% { left: -300%; }
95% { left: -300%; }
100% { left: -400%; }
}
body { margin: 0; }
div#slider { overflow: hidden; }
div#slider figure .page { width: 100vw; float: left; }
div#slider figure {
position: relative;
width: 500vw;
margin: 0;
left: 0;
text-align: left;
font-size: 10;
animation: 30s slidy infinite;
}
.page {
display: flex;
justify-content: center;
}
.project {
display: flex;
align-items: center;
}
这假定给定用户在给定日期最多只能出现一次出席。它还假定您的SELECT
Date,
100.0 * COUNT(attendance) / COUNT(*) AS perc_attendance
FROM yourTable
WHERE Date BETWEEN '2018-01-01' AND '2018-01-02'
GROUP BY
Date;
列是实际的日期列,而不仅仅是文本。
答案 1 :(得分:0)
select v.date1 , case when s.no_people is null and v.showup_people then 100
when v.showup_people is null and s.no_people is not null then 0
else (v.showup_people / (v.showup_people + s.no_people))*100 end outcome
from
(select count(*) as no_people , date as date1
from yourTable
where attendance is null
group by date) s
right outer join
(select count(*) as showup_people , date as date1
from yourTable
where attendance is not null
group by date) v
on(v.date1 = s.date1)
将“ from”表替换为您的表名。
这将始终有效,无论您有多少个约会。 情况是因为null + mysql中的数字为null。 在oracle sql中,您可以只使用最后一个else语句。
这是一个Demo
答案 2 :(得分:0)
如果要显示为新表,则可以使用此查询,但必须先创建一个:
SELECT Date,100.0 * COUNT(attendance) / COUNT(*) AS attendancepercent
FROM Table WHERE Date BETWEEN '2018-01-01' AND '2018-01-02'
GROUP BY Date;