Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 一栏计算_Sql - Fatal编程技术网

Sql 一栏计算

Sql 一栏计算,sql,Sql,其中id_sts 19-打开,21-工作,18-关闭 用户创建一个任务并在一段时间后关闭它。示例:在3月20日,用户创建了一个任务,该任务处于打开状态,然后在3月22日,该任务处于关闭状态。问题是,当3月21日和3月22日的任务打开时,我如何显示这段时间。在一列中有id_user、open、close和update任务列。我想您可以与我们一起使用,或者计算此任务状态打开之前的天数。请求是正确的,但以下是未解决问题的总数 upd_date id_sts id_

其中id_sts 19-打开,21-工作,18-关闭 用户创建一个任务并在一段时间后关闭它。示例:在3月20日,用户创建了一个任务,该任务处于打开状态,然后在3月22日,该任务处于关闭状态。问题是,当3月21日和3月22日的任务打开时,我如何显示这段时间。在一列中有id_user、open、close和update任务列。我想您可以与我们一起使用,或者计算此任务状态打开之前的天数。请求是正确的,但以下是未解决问题的总数

upd_date                id_sts     id_tsk
20/03/21 09:00        19          01
22/03/21 11:05        18          01
22/03/21 11:04        21          01
20/03/21 09:06        19          02
23/03/21 11:05        18          02
23/03/21 11:04        21          02

每天在
状态中有许多打开的任务\u hist

select count(a.*), date_trunc('day', sh.upd_status)  from f.f_task a
join f.status_hist sh
on a.idtask = sh.idtask
where b.id_status not in (17,18) 
group by date_trunc('day', sh.upd_status)
order by date_trunc('day', sh.upd_status)

a和b是糟糕的表别名。选择一些有意义的东西,比如sh代表历史地位。
select td.dupd, count(*) tasks_open
from (
  select distinct date_trunc('day', upd_date) dupd
  from status_hist) td
join (
  select id_tsk,
        max(case when id_sts = 19 then date_trunc('day', upd_date) end) dstart,
        max(case when id_sts = 18 then date_trunc('day', upd_date) end) dend,
  from status_hist
  group by id_tsk
) tsk on td.dupd between tsk.dstart and tsk.dend
group by td.dupd