Newbies DLB Posted October 8, 2014 Newbies Posted October 8, 2014 I have a Computer table and a Service table. They are related on field pk_ComputerId and fk_ComputerId respectively. I am trying to find all the computers that do not have a service call. I have been able to find the computers that do no problem as follows: ExecuteSQL ( "SELECT pk_ComputerId FROM Computer WHERE pk_ComputerId = ANY(SELECT fk_ComputerId FROM Service)" ; "" ; "" ) -or- ExecuteSQL ( "SELECT pk_ComputerId FROM Computer c JOIN Service s ON c.pk_ComputerId = s.fk_ComputerId)" ; "" ; "" ) I must be missing something in my logic because I would think you could replace the = with a <> in either of the statements above and get a list of computers that do not have service calls. But the result I get is all of the records in the Computer table regardless if they have an entry in the Service table or not. Can someone please help me adjust my logic on this!
eos Posted October 8, 2014 Posted October 8, 2014 You want all computerIDs from Computers that are not represented in the Services table. The easiest way to express that statement in SQL is probably: ExecuteSQL ( " SELECT pk_ComputerId FROM Computer WHERE pk_ComputerId NOT IN ( SELECT DISTINCT ( fk_ComputerId ) FROM Service ) " ; "" ; "" )
Wim Decorte Posted October 8, 2014 Posted October 8, 2014 make sure you take empty values into account: http://www.soliantconsulting.com/blog/2013/11/executesql-and-empty-values 1
Newbies DLB Posted October 10, 2014 Author Newbies Posted October 10, 2014 Thank you eos! That worked great. I had tried NOT but was not familiar with NOT IN. WIM thanks for the heads up, but in this case I am dealing with keys so if I have a blank I have other problems.
Kris M Posted October 10, 2014 Posted October 10, 2014 DLB.. I would guess that the "ANY" part of this expression is NOT supported by ExecuteSQL ...ExecuteSQL ( "SELECT pk_ComputerId FROM Computer WHERE pk_ComputerId = ANY(SELECT fk_ComputerId FROM Service)" ; "" ; "" )
Recommended Posts
This topic is 3834 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now