sql - How to select id's wich contains just special values? -
i need sql query. have table this:
id booktype date ----------- ---------- ------ 1 85 01.01.2014 1 86 01.01.2014 1 88 01.01.2014 1 3005 01.01.2014 1 3028 01.01.2014 2 74 01.01.2016 2 85 01.01.2016 2 86 01.01.2016 3 88 01.01.2015 3 3005 01.01.2015
i need query, returns id's booktype 85, 86 , not id's booktype 88,3005,3028. other types not relevant, can included.
example:
i want id 2, because there no booktype of 88, 3005, 3028. have id 74, doesn't matter, can included.
i tried this:
select bookid id, count(bookid) number books date between '01.01.2014' , '01.01.2016' , booktype in (85,86) group bookid having count(bookid) >1 minus select bookid id, count(bookid) number books date between '01.01.2014' , '01.01.2016' , booktype in (88,3005,3028) group bookid;
it doesn't work. every time results booktype 88 or other included. tried except
, oracle sql developer doesn't know ist.
any ideas?
i see inconsistency between sql , columns names.
there no bookid in table , miss booktype...
so assuming first query is:
select id id, count(id) number books date between '2014-01-01' , '2016-01-01' , booktype in (85,86) group id having count(id) >1;
this have result set:
id number 1 2 2 2
your second query
select id id, count(id) number books date between '2014-01-01' , '2016-01-01' , booktype in (88, 3005, 3028) group id;
this have result set:
id number 1 3 3 2
the minus operator in oracle returns unique rows returned first query not second. whole query return first record set both results of first query different results of second.
if drop count statement in query have:
first query
select id id books date between '2014-01-01' , '2016-01-01' , booktype in (85,86) group id having count(id) >1;
result set
id 1 2
second query:
select id id books date between '2014-01-01' , '2016-01-01' , booktype in (88, 3005, 3028) group id;
result set
id 1 3
and applying minus operator 2 required, 1 in second result set.
this confirm logic right, did not take in account way minus operates on result sets.
so query has be:
select id id books date between '2014-01-01' , '2016-01-01' , booktype in (85,86) group id having count(id) >1 minus select id id books date between '2014-01-01' , '2016-01-01' , booktype in (88, 3005, 3028) group id;
last remarks:
- i left date between '2014-01-01' , '2016-01-01', thought relative other requirements if not relevant in example
- i left having count(id) >1, thought relative other requirements if not relevant in example
regards
Comments
Post a Comment