Conversion of SQL "NOT EXISTS" to Crystal Reports

Can anyone help me with an example of how to do a classic sql "not exists" in Crystal Reports?
dontsoAsked:
Who is Participating?
 
wykabryanCommented:
hmmm... From my knowledge, I do not believe so. I believe you will have to create a database view.  
0
 
wykabryanCommented:
select employee_name, employee_id, employee_hours from tblemployee as a
where not exists(select 9 from tempemployee as b where b.employee_id = a.employee_id)


There has to be a link between the two tables.  

Hope this helps.
0
 
dontsoAuthor Commented:
So what you suggest is actually inputting the sql AS sql into crystal reports?
I was thinking that there might have been a way to do it with the "report expert"
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
bdreed35Commented:
You can accomplish in Crystal by doing a left join from one table to the other, then in your record selection, check for the field in the other table to be equal to NULL.
0
 
wykabryanCommented:
Not sure what you are looking for. Could you provide more information about what the problem is and what you are looking to do?  There are many ways to approach this.  Obviously, doing it in the sql should be the easiest if you are looking to do a basic sql string.  But in most cases, that is not what the problem calls for.
0
 
wykabryanCommented:
bdreed does provide a crystal solution.
0
 
dontsoAuthor Commented:
Please see my example below with the two tables: employee_table and employee_settings_table.

employee_table:
-------------------
employee_id


employee_settings_table:
-----------------------------
employee_settings_id
employee_id
employee_setting

One employee can have many employee_settings_id's. What I need to pull from Crystal Reports is eg. all the employee_id's from the employee_table that in the employee_settings_table have the value "one" and "two" in employee_setting, but they must not have value "three" in employee_setting. (remember one employee can have many different employee_settings_id's. Hence also many different employee_settings.

What I have tried (which does not work) is to have three tables linked together: employee_table, employee_settings_table_1 (an alias for employee_settings_table) and employee_settings_table_2 (also alias for employee_settings_table).
I have then made a select formula like this one:

{employee_settings_table_1.employee_setting} in ["one", "two"] and
not ({employee_settings_table_2.employee_setting} in ["three"])

but this does not work.

If I were to make it in sql it would be:

select *
from employee_table et, employee_settings_table est1
where et.employee_id = est1.employee_id and
est1.employee_setting in ("one","two") and
not exists (
      select *
      from employee_settings_table est2
      where est2.employee_setting in ("three")
)


and this is the kind of sql I would like to convert to be used in my crystal report instead.
Is it more clear now or did I really not help that much? ;o)
0
 
wykabryanCommented:
select *
from employee_table et, employee_settings_table est1
where et.employee_id = est1.employee_id and
est1.employee_setting in ("one","two") and
not exists (
     select 9
     from employee_settings_table est2
     where
     est2.employee_id = et.employee_id and
     est2.employee_setting in ("three")
)


This should work.
0
 
dontsoAuthor Commented:
Yes. I forgot one line in the sql sorry. I do not have a problem with the sql, but with Crystal Reports. :o)
0
 
wykabryanCommented:
Just had another question as I was writting that sql and before the computer crashed on me, if the employee_id does contain one, two and three; than you want to exclude that employee_id all together?
0
 
dontsoAuthor Commented:
Exactly! Thank you :o)
The problem with my Crystal Report is that it does not exclude the employee_id...it just excludes the employee_settings_id's with the value "three", but the employee_id is still pulled because the employee also has values "one" and "two".
I need the employees that ONLY have "one" and "two", but NEVER "three".
0
 
wykabryanCommented:
Ok. Then yeah that above should work for you.

select *
from employee_table et, employee_settings_table est1
where et.employee_id = est1.employee_id and
est1.employee_setting in ("one","two") and
not exists (
     select 9
     from employee_settings_table est2
     where
     est2.employee_id = et.employee_id and
     est2.employee_setting in ("three")
)

so the first select statement reads all records where employee_setting = 1 or 2.  It will then compare it against the sub-query that is pulling all employees that have an setting of 3.  The end result is all employees that only have a setting of 1 or 2.

Right?
0
 
dontsoAuthor Commented:
It does work when I use sql which I do not in Crystal Reports. What I am looking for is to make Crystal Reports give me the result of the sql you suggest, but I would like to do it using the Crystal Report expert. Is that possible and how?
0
 
wykabryanCommented:
What version of Crystal are you using?  There is a way to port that sql into CR, by using command objects.  This feature is only available in 9.0 or higher.
0
 
dontsoAuthor Commented:
Unfortunately using 8.0, but I can´t imagine that it is not possible to make something similar to "not exists" in Crystal reports. The question is how?
0
 
mlmccCommented:
>>Unfortunately using 8.0, but I can´t imagine that
Crystal 8 only supports a single SELECT statement in the CR query.  The only way to do this is through a database view or stored procedure.

mlmcc
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.