?
Solved

Conversion of SQL "NOT EXISTS" to Crystal Reports

Posted on 2006-04-10
18
Medium Priority
?
3,586 Views
Last Modified: 2010-08-05
Can anyone help me with an example of how to do a classic sql "not exists" in Crystal Reports?
0
Comment
Question by:dontso
16 Comments
 
LVL 16

Expert Comment

by:wykabryan
ID: 16416959
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
 

Author Comment

by:dontso
ID: 16416980
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
 
LVL 28

Expert Comment

by:bdreed35
ID: 16417023
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 16

Expert Comment

by:wykabryan
ID: 16417027
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
 
LVL 16

Expert Comment

by:wykabryan
ID: 16417040
bdreed does provide a crystal solution.
0
 

Author Comment

by:dontso
ID: 16417292
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
 
LVL 16

Expert Comment

by:wykabryan
ID: 16417337
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
 

Author Comment

by:dontso
ID: 16417385
Yes. I forgot one line in the sql sorry. I do not have a problem with the sql, but with Crystal Reports. :o)
0
 
LVL 16

Expert Comment

by:wykabryan
ID: 16417517
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
 

Author Comment

by:dontso
ID: 16417561
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
 
LVL 16

Expert Comment

by:wykabryan
ID: 16417610
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
 

Author Comment

by:dontso
ID: 16417649
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
 
LVL 16

Expert Comment

by:wykabryan
ID: 16417669
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
 

Author Comment

by:dontso
ID: 16417681
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
 
LVL 16

Accepted Solution

by:
wykabryan earned 1000 total points
ID: 16417991
hmmm... From my knowledge, I do not believe so. I believe you will have to create a database view.  
0
 
LVL 101

Assisted Solution

by:mlmcc
mlmcc earned 1000 total points
ID: 16625718
>>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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hot fix for .Net Crystal Reports 10.2.3600.0 to fix problems with sub reports running on 64 bit operating systems ISSUE: Reports which contain subreports fail with error "Missing Parameter Value" DEPLOYMENT SERVER OS: Windows 2008 with 64 bi…
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question