我有STATUS专栏。值被批准,待定,拒绝。我必须计算机票在PENDING中的天数。
ID Status Date
BNM Pend 11/nov/16
BNM Pend 12/nov/16
ABC Pend 11/nov/16
BNM Pend 12/nov/16
ABC Pend 11/nov/16
ABC Pend 12/nov/16
CVD pend 11/nov/16
CVD pend 12/nov/16
BNM Pend 10/NOV/16
ABC Pend 10/NOV/16
CVD Pend 10/NOV/16
CVD Approv 09/NOV/16
CVD PEND 08/NOV/16
当我在13 / nov / 16上运行查询时。
BNM-3 days
ABC-3 days
"CVD-5days" (counted as 5 days but it should count as 3 days).
从08年11月开始计算,但它应该从10月16日开始计算。有关状态的最新消息将于09年11月9日开始。
Select t.ID,(t.date),t2.nofdays,t1.status
from table1 t
inner join
( select tt.id,max(tt.days)
from table1 tt
group by ...)t2
on t.id=t2.id
inner join
( ( select ttt.id,max(ttt.days), count(ttt.days) as noofdays
from table1 ttt
where status like 'PEND%'
group by ...)t2
on t1.id=t2.id and t1.date=t2.date
where trunc(t.date)=trunc(sysdate-1)
group by .....)
答案 0 :(得分:0)
我们假设您要查找待处理的实际天数。您可以通过运行此选择来执行此操作:
SELECT (MAX(t.status_date) - MIN(t.status_date) + 1) nodays, -- +1 is for adding a current day of pending
t.id
FROM table1 t
JOIN (
-- For every ID, find max date of not pending status or return "01/01/1900"
SELECT t.id,
NVL(MAX(CASE
WHEN t.status <> 'Pend' THEN
status_date
END),
to_date('01/01/1900', 'dd/mm/yyyy')) max_date
FROM table1 t
GROUP BY t.id
--
) m
-- keep only rows that are newer than max_date
ON t.id = m.id
AND t.status_date > m.max_date
--
GROUP BY t.id;
此选择将返回从第一个待处理状态到上一个待处理状态的天数。