【发布时间】:2018-09-11 19:57:39
【问题描述】:
我有一张这样的数据表
所以我想让结果看起来像
唯一 ID、MIN(input_date)、MIN(date) where op='u'、max(date) where status=closed 以及关闭它的用户
这用于帮助台和跟踪何时输入帮助请求 (op=I)、何时分配帮助请求 (op='U' and status='open') 以及何时关闭 (op=u & status =关闭)。 I 的名字是需要帮助的用户,实际上并不关心他们,但跟踪提出请求的人很重要,因此最终想知道从输入到分配到完成需要多长时间。
我的查询正在使用子查询,但它将每列中的日期输出为同一日期,并且所有行的同一个人。
select ident,
(select ident, MIN(input_date)
from table
where op='I' and input_date >'2017-12-31') as 'input date'
(select MIN(date)
from table
where op='U' and input_date > '2017-12-31') as 'date assigned',
(select MAX(date)
from table
where op='U' and status='closed' and input_date > '2017-12-31') as 'date closed',
(select max(user)
from table
where op='u' and status='closed' and input_date >'2017-12-31') as 'tech'
from table
where input_date >'2017-12-31'
group by ident
order by ident asc
如果什么都没发生,我需要它显示空值。所以对于 ident 3,它应该在分配的数据中读取 null 并且日期关闭,因为两者都没有发生。在 ident 2 中,它应该读取 null 表示关闭日期,因为它还没有关闭。
这是我此时得到的
【问题讨论】:
-
如果您使用的是子查询,则根据外部表使用 where 条件
标签: sql sql-server