我有这张桌子:
// controller
$scope.toggleModal = function () {
$scope.isModalOpen = !$scope.isModalOpen;
}
<div ...
ng-click="toggleModal()">
<button ng-click="toggleModal()">...
我需要每天显示累积差异。实际上是平均的,但至少应该是累积的。
所以输出看起来像这样:
id
entry_date (timestamp)
exit_date (timestamp)
含义
2015 1 1 33
2015 1 3 56
2015 2 4 77
2015 3 12 123
我检查了类似的主题:
MySQL cumulative sum grouped by date
Cumulative sum over a set of rows in mysql
Optimal query to fetch a cumulative sum in MySQL
Create a Cumulative Sum Column in MySQL
但这些解决方案都不适用于我。我正急于用SQL来做这件事。 但这些任务听起来很简单,很难相信解决方案很难找到。
答案 0 :(得分:0)
你可能能够更有效地工作,但我相信这是让你前进的东西。而且您的解释并不清楚,因此您可能希望提供一些示例数据来阐明您的需求。
select
event_date,
(
select sum(+1) from T as t2 where cast(t2.entry_date as date) <= d.event_date +
select sum(-1) from T as t3 where cast(t3.exit_date as date) <= d.event_date
) as cumulative_total /* at end of day */
from (
select cast(entry_date as date) as event_date from T union all
select cast(exit_date as date) from T
) as d
group by event_date
将T替换为您的表名。我还使用了一个演员来消除时间,但我不确定它在MySQL中是否真的有效。
平均天数(不包括任何未通过入口或出口表示的日期)也很容易:
select avg(cumulative_total) as average_per_day
from (
select
(
select sum(+1) from T as t2 where cast(t2.entry_date as date) <= d.event_date +
select sum(-1) from T as t3 where cast(t3.exit_date as date) <= d.event_date
) as cumulative_total /* at end of day */
from (
select cast(entry_date as date) as event_date from T union all
select cast(exit_date as date) from T
) as d
group by event_date
) as d2