我有下表,在这里,我想获取值 STAT_NEW = 'Sent to xxxx Project Team' 的计数。在出现值 stat_new='Sent to VMO' 之前,我们需要计数值。
SQL> Select * from sample_test;
ID CONTRACT_ID STAT_NEW UPD_DT
----------------------------- -------------------------------------- ----------
0 CR 01 to MWO 1 Sent to xxxx Project Team 11-AUG-13
1 CR 01 to MWO 1 Sent to xxxx Project Team 11-AUG-13
2 CR 01 to MWO 1 Sent to xxxx Project Team 11-AUG-13
3 CR 01 to MWO 1 Sent to VMO 12-AUG-13
4 CR 01 to MWO 1 Sent to xxxx Project Team 11-AUG-13
5 CR 01 to MWO 1 Sent to xxxx Project Team 11-AUG-13
6 CR 01 to MWO 1 Sent to VMO 12-AUG-13
7 CR 01 to MWO 2 Sent to xxxx Project Team 11-AUG-13
8 CR 01 to MWO 2 Sent to xxxx Project Team 11-AUG-13
9 CR 01 to MWO 2 Sent to xxxx Project Team 11-AUG-13
10 CR 01 to MWO 2 Sent to VMO 12-AUG-13
11 CR 01 to MWO 3 Sent to xxxx Project Team 12-AUG-13
12 CR 01 to MWO 3 Sent to xxxx Project Team 12-AUG-13
13 CR 01 to MWO 3 Sent to VMO 13-AUG-13
7 rows selected
我已经尝试过下面的场景,其中给出了特定的合同 ID,如果只有一个发送到 VMO,那么下面的场景是有效的。
select count(*) from sample_test where upd_dt <= (select UPD_DT from sample_test where stat_new='Sent to VMO' and contract_id='CR 01 to MWO 1') AND stat_new='Sent to xxxx Project Team';
我的预期输出如下所示....
CONTRACT_ID count of STAT_NEW='Sent to xxxx Project Team'
-------------- ------------
CR 01 to MWO 1 3
CR 01 to MWO 1 2
CR 01 to MWO 2 3
CR 01 to MWO 3 2