Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: I may need a Command statement... Post Reply Post New Topic
Author Message
rbmauder
Newbie
Newbie
Avatar

Joined: 27 Mar 2012
Online Status: Offline
Posts: 9
Quote rbmauder Replybullet Topic: I may need a Command statement...
     Posted: 27 Mar 2012 at 12:25pm
Hi everyone,

I'm new to the forum and have what very well may be a very basic question but here goes.  (I work for an animal shelter.)

I am looking to see what animals who were admitted to our shelter on a specific date have NOT yet had a treatment recorded for them after the intake date.

From what I can see I need a statement which in essence says 'not greater than' because there very well may be treatments recorded from an earlier date.  But how do I test to see if a record doesn't exist?
IP IP Logged
epement
Newbie
Newbie
Avatar

Joined: 27 Mar 2012
Location: United States
Online Status: Offline
Posts: 6
Quote epement Replybullet Posted: 27 Mar 2012 at 8:48pm
I am not a Crystal expert and I need a lot of help, but I am assuming that you have one table which contains animal_ID_number and admit date, and another table which contains animal_ID_number and treatment dates.

Now, I'm guessing, but you probably also have some type of death date, and a sale date (when the animal passed out of the system into the hands of a pet owner). You want to select all the animal_ID_numbers from the first table where the death-date is null and the sale-date is null. This should give you a census of all the animals currently in the system.

Inner Join the Census (left) to the Treatment table (right), using the animal ID number as the join field. That will give you a list of all the animals currently in the hospital that have been treated.

Use this list as a subquery, so that you want a list of all the animals in the Census that are NOT in (subquery results).

My problem is that I know how to write this thing easier in SQL than I am able to write it in Crystal. But I believe that's the basic idea.
IP IP Logged
rbmauder
Newbie
Newbie
Avatar

Joined: 27 Mar 2012
Online Status: Offline
Posts: 9
Quote rbmauder Replybullet Posted: 28 Mar 2012 at 7:00am
I appreciate your reply.

It is the SQL side of things that trip me up.  Writing up a SQL command statement is where I struggle.  I've been told that Crystal cannot do the 'not exists' side of things without the SQL command being part of the report.

I've tried unsuccessfully to create the SQL code but cannot seem to get the syntax correct.
IP IP Logged
kostya1122
Senior Member
Senior Member
Avatar

Joined: 13 Jun 2011
Online Status: Offline
Posts: 475
Quote kostya1122 Replybullet Posted: 28 Mar 2012 at 7:31am
But how do I test to see if a record doesn't exist?
if this is your question
then try to write a simple test formula something like
if table.treatment date > table.intake date and
not(isnull(table.treatment date) then "yes" else "no"

place it into your report
IP IP Logged
rbmauder
Newbie
Newbie
Avatar

Joined: 27 Mar 2012
Online Status: Offline
Posts: 9
Quote rbmauder Replybullet Posted: 28 Mar 2012 at 8:07am
I tried that and it doesn't seem to work.  It comes up with records which do have a later treatment date and ones which do not have later treatments.
IP IP Logged
kostya1122
Senior Member
Senior Member
Avatar

Joined: 13 Jun 2011
Online Status: Offline
Posts: 475
Quote kostya1122 Replybullet Posted: 28 Mar 2012 at 8:16am
here is the only other thing i could think of

group on animal_ID_number
formula like
if table.treatment date > maximum(table.intake date,animal_ID_number)
 then "yes" else "no"

IP IP Logged
rbmauder
Newbie
Newbie
Avatar

Joined: 27 Mar 2012
Online Status: Offline
Posts: 9
Quote rbmauder Replybullet Posted: 28 Mar 2012 at 9:00am
Still no-go - I can't get it to pull the right records.

I've taken a shot at writing some SQL code to do this but haven't been able to get the second 'no exists' syntax correct.  What I have so far is this:
select *
from sysadm.kennel k
inner join sysadm.animal a on k.animal_id=a.animal_id
inner join sysadm.person p on k.owner_id=p.person_id
where k.outcome_type='ADOPTION'

and not exists (
select 'Y' from sysadm.kennel k2
where k.animal_id=k2.animal_id
and k2.intake_date+k2.intake_time>k.outcome_date+k.outcome_time
and k2.intake_type='RETURN')

************
This code selects the animals who have been adopted from he shelter and have not been returned.

However, here is where I get stuck.  How do I code another 'not exists' clause?  I then have to say 'no exists' for any treatments later than the intake date.  I can't seem to get it to work from with CR so SQL is the only way to accomplish it but I just cannot figure out the syntax.
IP IP Logged
rkrowland
Senior Member
Senior Member
Avatar

Joined: 20 Dec 2011
Location: England
Online Status: Offline
Posts: 259
Quote rkrowland Replybullet Posted: 29 Mar 2012 at 12:32am

Hi I'm not sure on field names and how your database works etc, but hopefully you'll be able to figure out what you need from the following.

 
This won't exclude records that have had treatment or have been returned, instead it will return all animals and flag where they've been returned or had treatment - this will allow you to filter them out using Crystal Record select.
 
Hopefully that all makes sence.
 
SELECT *
FROM Kennel k
JOIN Animal a
ON k.animal_id = a.animal_id
AND k.outcome_type='ADOPTION'
JOIN person p
ON k.owner_id = p.person_id

LEFT JOIN (
SELECT
k.animal_id,
k.intake_date,
k.intake_time,
'Returned' [Returned]
FROM Kennel k
WHERE k.intake_type='RETURN') r
ON r.animal_id = a.animal_id
AND r.intake_date + r.intake_time > k.outcome_date+k.outcome_time

LEFT JOIN (
SELECT
k.animal_id,
k.treatment_date,
k.treatment_time,
'Treatment Given' [Treatment]
FROM Kennel k
WHERE k.field = 'TREATMENT') t
ON t.animal_id = a.animal_id
AND t.treatment_date + t.treatment_time > r.intake_date+r.intake_time
 
The part in bold is where I'm not sure whether we should be comparing the treatment date with the intake_date of the return or with something in the main table.
 
Regards,
Ryan.


Edited by rkrowland - 29 Mar 2012 at 5:09am
IP IP Logged
rbmauder
Newbie
Newbie
Avatar

Joined: 27 Mar 2012
Online Status: Offline
Posts: 9
Quote rbmauder Replybullet Posted: 04 Apr 2012 at 7:23am
Thanks for your responses.  I appreciate it.  The final SQL code came about as a result of many people's responses.

In case you are interested, here is the final SQL code:
SELECT DISTINCT
 p.person_id,
 p.first_name,
 p.last_name,
 p.street_no,
 p.street_no_suffix,
 p.street_dir,
 p.street_name,
 p.street_type,
 p.quadrant,
 p.apt,
 p.city,
 p.state,
 p.zip_code,
 p.zip_code_suffix,
 p.email_addr,
 p.phone_area_code,
 p.phone_number,
 p.alt_area_code,
 p.alt_phone,
 k.impound_no,
 k.outcome_date,
 a.animal_id,
 a.animal_name,
 a.sex,
 a.age_now,
 a.primary_breed,
 a.secondary_breed,
 a.primary_color,
 a.secondary_color
from
sysadm.kennel k inner join sysadm.animal a on k.animal_id=a.animal_id
  inner join sysadm.person p on k.owner_id=p.person_id
where
k.outcome_type='ADOPTION' and
not exists (select 'Y'
  from sysadm.kennel k2
  where
   k2.animal_id = k.animal_id and
   k2.intake_date >= k.outcome_date and
   k2.source_id = k.owner_id and
   k2.intake_type='RETURN') and
not exists (select 'Y'
  from sysadm.memo m
  where
   m.memo_id = k.animal_id and
   m.memo_date > k.outcome_date and
   m.memo_type = 'FOLLOW UP')

IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum