ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Here's something that's been bugging me with SQL recently. Is it possible to create a single query that that can create a list of one non-unique value which *never* has a certain corresponding value in another field? ok.. doesn't make sense... take this table:
Code:
A B
1 no
1 no
1 yes
2 no
2 no
3 no
4 yes
and to derive the results
Code:
A
2
3
i.e a list of A which have NOT ever had a "yes" next to them. I know this is pretty easy to find using a subquery (i.e. find all the ones that HAVE had a yes and subtract that from a full list) and the equivalent join too, however I'm wondering if there's a way to do this with just ONE query, possibly using a small inline function to make any "yes"'s be selected as the result of an aggregate of some sort, and then lose those from a HAVING or such.....
Give me 2 hours, I think I might know how to do it but before posting something that makes me look like an idiot I am going to finish up the code I am writing and test my solution
select distinct A from table where B='no' and A not in (select distinct A from table where B='yes')
That should be it, but using 'in' can be a little slow.... but I guess if you can make an OUTER JOIN, you could ask it to select A NOT MATCHING As NOT in the other select (A where B='yes').
SELECT fld1 from table1
minus
SELECT fld1 from table1 where
fld2='YES';
This executes fast - the reason is that you MUST do a full-table scan to complete the query anyway. So both 'selects' actually use data from a single full table scan. Sub-queries are not necessarily evil. In this instance (using Oracle) the performance will be the same whether you use the above query or something more complex. You may even degrade performance by using something else.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.