Home » SQL & PL/SQL » SQL & PL/SQL » Fetch records based on 2 status (Oracle 10g )
| Fetch records based on 2 status [message #575677] |
Fri, 25 January 2013 10:27  |
 |
ashagang
Messages: 5 Registered: December 2012
|
Junior Member |
|
|
I have a table with below description.
create table test_exp (oid number, ioid number, status varchar2(20));
Below are the insert statements.
Insert into TEST_EXP (OID, IOID, STATUS) Values (1, 100, 'NEW');
Insert into TEST_EXP (OID, IOID, STATUS) Values (1, 101, 'DISCO');
Insert into TEST_EXP (OID, IOID, STATUS) Values (1, 102, 'CANCELLED');
Insert into TEST_EXP (OID, IOID, STATUS) Values (2, 103, 'NEW');
Insert into TEST_EXP (OID, IOID, STATUS) Values (3, 104, 'DISCO');
Insert into TEST_EXP (OID, IOID, STATUS) Values (4, 105, 'DISCO');
Insert into TEST_EXP (OID, IOID, STATUS) Values (4, 106, 'NEW');
Insert into TEST_EXP (OID, IOID, STATUS) Values (5, 107, 'NEW');
Insert into TEST_EXP (OID, IOID, STATUS) Values (5, 108, 'CANCELLED');
The output looks like below:
OID|IOID|STATUS
1 |100 |NEW
1 |101 |DISCO
1 |102 |CANCELLED
2 |103 |NEW
3 |104 |DISCO
4 |105 |DISCO
4 |106 |NEW
5 |107 |NEW
5 |108 |CANCELLED
Now my problem is we should fetch the data based on the below rules
If an OID contains 2 IOIDs for which there is a NEW and DISCO status attached, then fetch the 2 records
If an OID has only 1 of these status, then ignore the same
If an OID has none of the 2 status, then ignore the same.
The expected output will be like below:
OID|IOID|STATUS
1 |100 |NEW
1 |101 |DISCO
4 |105 |DISCO
4 |106 |NEW
Can somebody please help.
[Edit MC: change quote tags to code ones]
[Updated on: Fri, 25 January 2013 10:32] by Moderator Report message to a moderator
|
|
|
|
|
|
|
|
|
|
| Re: Fetch records based on 2 status [message #575715 is a reply to message #575714] |
Fri, 25 January 2013 14:58  |
 |
Michel Cadot
Messages: 54129 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Something like:
SQL> with
2 data as (
3 select t.*, count(*) over (partition by oid) cnt
4 from test_exp t
5 where status in ('NEW','DISCO')
6 )
7 select oid, ioid, status
8 from data
9 where cnt = 2
10 order by 1, 2, 3
11 /
OID IOID STATUS
---------- ---------- --------------------
1 100 NEW
1 101 DISCO
4 105 DISCO
4 106 NEW
Regards
Michel
|
|
|
|
Goto Forum:
Current Time: Sun May 19 23:57:21 CDT 2013
Total time taken to generate the page: 0.10054 seconds
|