Link to home
Start Free TrialLog in
Avatar of dontso
dontso

asked on

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?
Avatar of wykabryan
wykabryan
Flag of United States of America image

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.
Avatar of dontso
dontso

ASKER

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"
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.
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.
bdreed does provide a crystal solution.
Avatar of dontso

ASKER

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)
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.
Avatar of dontso

ASKER

Yes. I forgot one line in the sql sorry. I do not have a problem with the sql, but with Crystal Reports. :o)
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?
Avatar of dontso

ASKER

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".
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?
Avatar of dontso

ASKER

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?
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.
Avatar of dontso

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of wykabryan
wykabryan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial