Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
query help

user626688
Member Posts: 416 Bronze Badge
please see the rows
ID DEF_BILL DEF_SH
20 N N
20 N Y
20 Y N
30 N N
30 N N
40 Y Y
40 N N
40 N Y
Here I want those ID's which has DEF_SH and DEF_BILL 'N' in all the instances of that particular ID.
In the above example only ID 30 has 'N' for DEF_SH and DEF_BILL in the available two values of 30.
But 20 and 40 has 'Y' somewhere. If you see the ID 40, the second instance of 40 has N for both DEF_BILL and DEF_SH but this does not qualify because in other instances, Y is available for either DEF_SH or DEF_BILL or for both.
So I want to select all those ID's which does not have 'Y' in any of its occurances.
Please help if anybody can... Thanks in advance
Message was edited by:
user626688
ID DEF_BILL DEF_SH
20 N N
20 N Y
20 Y N
30 N N
30 N N
40 Y Y
40 N N
40 N Y
Here I want those ID's which has DEF_SH and DEF_BILL 'N' in all the instances of that particular ID.
In the above example only ID 30 has 'N' for DEF_SH and DEF_BILL in the available two values of 30.
But 20 and 40 has 'Y' somewhere. If you see the ID 40, the second instance of 40 has N for both DEF_BILL and DEF_SH but this does not qualify because in other instances, Y is available for either DEF_SH or DEF_BILL or for both.
So I want to select all those ID's which does not have 'Y' in any of its occurances.
Please help if anybody can... Thanks in advance
Message was edited by:
user626688
Comments
-
Use a WHERE clause.
WHERE col1 <> 'Y'
AND col2 <> 'Y' -
No. It will not work.. because by using <>'Y' i will get ID 40 also, which i donot want because in some other occurences of 40 there are 'Y'. I want only those values for which there is no 'Y' in any of its occurences. If an ID has 'Y' anywhere then that ID disqualifies even if it has 'N' for both DEF_BILL and DEF_SH in any of its occurences... Thanks.. I think we may have to think further with GROUP BY....
-
This should be of help...
ME_XE?with data as
2 (
3 select 1 as col1, 'Y' as col2 from dual union all
4 select 1 as col1, 'N' as col2 from dual union all
5 select 2 as col1, 'Y' as col2 from dual union all
6 select 3 as col1, 'N' as col2 from dual
7 )
8 select col1, col2, SUM(decode(col2, 'Y', 1, 'N', 0)) over (partition by col1) as keepers
9 from data
10 /
COL1 COL KEEPERS
------------------ --- ------------------
1 Y 1
1 N 1
2 Y 1
3 N 0
4 rows selected.
Elapsed: 00:00:00.01
ME_XE?
ME_XE?
ME_XE?with data as
2 (
3 select 1 as col1, 'Y' as col2 from dual union all
4 select 1 as col1, 'N' as col2 from dual union all
5 select 2 as col1, 'Y' as col2 from dual union all
6 select 3 as col1, 'N' as col2 from dual
7 ),
8 step_2 as
9 (
10 select col1, col2, SUM(decode(col2, 'Y', 1, 'N', 0)) over (partition by col1) as keepers
11 from data
12 )
13 select *
14 from step_2
15 where keepers = 0
16 /
COL1 COL KEEPERS
------------------ --- ------------------
3 N 0
1 row selected.
Elapsed: 00:00:00.00
ME_XE? -
table t_idfile
data in T_idfile is as below:
ID col1 col2
20 N N
20 N Y
20 Y N
30 N N
30 N N
40 Y Y
40 N N
40 N Y
select id from t_idfile where col1 = 'N' and col2 = 'N' and id not in (select id from t_idfile where col1= 'Y' or col2 = 'Y'); -
Although, it might be better to specify the predicates"col1 = 'N' and col2 = 'N'" in WHERE clause for performance,
I thought that it is not necessary to specify the predicates.
Like this:SELECT DISTINCT id FROM ID_Tbl WHERE id NOT IN (SELECT id FROM ID_Tbl WHERE def_bill = 'Y' OR def_sh = 'Y' ) ;
Generally speaking, you can use [NOT] EXISTS predicate instead of [NOT] IN predicate.SELECT DISTINCT id FROM ID_Tbl A WHERE NOT EXISTS (SELECT * FROM ID_Tbl B WHERE B.id = A.id AND (def_bill = 'Y' OR def_sh = 'Y') ) ;
Another ideas are:SELECT id FROM ID_Tbl GROUP BY id HAVING COUNT(CASE def_bill WHEN 'N' THEN 0 END) = COUNT(*) AND COUNT(CASE def_sh WHEN 'N' THEN 0 END) = COUNT(*) ;
SELECT id FROM ID_Tbl GROUP BY id HAVING SUM(INSTR(def_bill, 'N')) = COUNT(*) AND SUM(INSTR(def_sh , 'N')) = COUNT(*) ;
SELECT id FROM ID_Tbl GROUP BY id HAVING MAX(def_bill) = 'N' AND MIN(def_bill) = 'N' AND MAX(def_sh ) = 'N' AND MIN(def_sh ) = 'N' ;
-
Thanks
-
Thanks dear
This discussion has been closed.