Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Table Join Selecting Maximum Value Post Reply Post New Topic
Author Message
apbeaulne
Newbie
Newbie
Avatar

Joined: 05 Dec 2007
Location: Canada
Online Status: Offline
Posts: 11
Quote apbeaulne Replybullet Topic: Table Join Selecting Maximum Value
     Posted: 08 Dec 2007 at 7:52am
I am joining 3 table by a 5 field key
 
 
Tables
 
Patient Information
Patient Weight
Patient Height
 
The Patient Information is the Primary Table and has only one record per key combination.
 
The Weight Table has many records that match the Key in the Patient Table
 
The Height Record can have many records that match the Key in the Patient Table
 
 
I only want the Height Record within a given Patient Chart Number that has the most recent date or the Maximum Date within the Chart Number
 
I have been using the Database Expert to join the tables but I sure wish I could get at the SQL statement so I could simply get the records I want.
 
 
How else would I make the selection I need
 
 
Patient Table - Institution Site ChartNumber CreateDate Type
Height Table  - Institution Site ChartNumber CreateDate Type Weight WeightDate
 
 
I need the Maximum WeightDate and Weight within the ChartNumber
 
Paul
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 08 Dec 2007 at 1:34pm
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
apbeaulne
Newbie
Newbie
Avatar

Joined: 05 Dec 2007
Location: Canada
Online Status: Offline
Posts: 11
Quote apbeaulne Replybullet Posted: 09 Dec 2007 at 6:34am
I suppose I am more of a beginner than I thought.  I read these suggested before I post and did not see how they applied to my situation.
 
Thank you though.
Paul
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 09 Dec 2007 at 12:03pm
Well, you originally asked for the SQL way to do this, so here is another post I wrote about the way to get the data from the max record using a SQL query. Since you use three tables, I suggest that you first try getting it working with just two tables and make sure you understand it. Then modify the SQL to add the third table in.

Crystal Reports max record
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
apbeaulne
Newbie
Newbie
Avatar

Joined: 05 Dec 2007
Location: Canada
Online Status: Offline
Posts: 11
Quote apbeaulne Replybullet Posted: 09 Dec 2007 at 1:32pm

Sorry I am not explaining the situation properly

 
In your example I do not see how this code would give the maximum heightdate within the Patient ID and WeightDate.
 
Also the code looks for a data that exists in both tables but this is not the case in my situation.
 
I have copied the example below
+++++++++++++++++++++++++++++
 
 
 
I am joining 3 table by a 5 field key
 
 
Tables
 
Patient Information
Patient Weight
Patient Height
 
The Patient Information is the Primary Table and has only one record per key combination.
 
The Weight Table has many records that match the Key in the Patient Table
 
The Height Record can have many records that match the Key in the Patient Table
 
 
I only want the Height Record within a given Patient Chart Number that has the most recent date or the Maximum Date within the Chart Number
 
I have been using the Database Expert to join the tables but I sure wish I could get at the SQL statement so I could simply get the records I want.
 
 
How else would I make the selection I need
 
 
Patient Table - Institution Site ChartNumber CreateDate Type
Weight Table - Institution Site ChartNumber CreateDate Type Weight WeightDate
Height Table  - Institution Site ChartNumber CreateDate Type Height HeightDate
 
 
The table below shows I am getting 2 HeightDates for every WeightDate.
 
I only want the most recent HeightDate
 
 
rptWeight
Chart LastName FirstName Room NursingUnit IsLastAssessment Gender Kilograms WeightDate Height HeightDate
000000308 Beaulne SANDRA 232 B 2B -1 Female 39.8 20030101 146.0 20060804
000000308 Beaulne SANDRA 232 B 2B -1 Female 39.8 20030101 152.0 20030514
000000308 Beaulne SANDRA 232 B 2B -1 Female 37.9 20030201 146.0 20060804
000000308 Beaulne SANDRA 232 B 2B -1 Female 37.9 20030201 152.0 20030514
 
 
 
 
 
 
 
I need the Maximum WeightDate and Weight within the ChartNumber
Paul
IP IP Logged
apbeaulne
Newbie
Newbie
Avatar

Joined: 05 Dec 2007
Location: Canada
Online Status: Offline
Posts: 11
Quote apbeaulne Replybullet Posted: 09 Dec 2007 at 4:34pm
Brian I was able to use access to build this Query and it return the correct records.
 
In Crystal Report is there a way to by pass the Database Expert and join the tables using SQL directly.
 
SELECT max(height.cdsiteno) as Institution,max(height.cdinstno) as Facility,max(height.cdassessdate) as CreateDate, max(height.cdassesstype) as Assessment, max( height.cdchartno) as Chart, max(height.cddate) as  HeightDate, max(height.cdvaluescm) as HeightCM, MAX(patinfo.cdlastassess) as LastPlan, weight.cddate as WeightDate, weight.cdvalueskg as WeightKilo
FROM (HEIGHT as HEIGHT
LEFT JOIN PATINFO AS PATINFO
ON ((((`PatInfo`.`CDSiteNo`       =`Height`.`CDSiteNo`)
  AND (`PatInfo`.`CDInstNo`           =`Height`.`CDInstNo`))
  AND (`PatInfo`.`CDChartNo`        =`Height`.`CDChartNo`))
  AND (`PatInfo`.`CDAssessDate`   =`Height`.`CDAssessDate`))
  AND (`PatInfo`.`CDAssessType`   =`Height`.`CDAssessType`))
left JOIN Weight AS Weight
ON ((((`Height`.`CDSiteNo`       =`Weight`.`CDSiteNo`)
  AND (`Height`.`CDInstNo`           =`Weight`.`CDInstNo`))
  AND (`Height`.`CDChartNo`        =`Weight`.`CDChartNo`))
  AND (`Height`.`CDAssessDate`   =`Weight`.`CDAssessDate`))
  AND (`Height`.`CDAssessType`   =`Weight`.`CDAssessType`)
WHERE PATINFO.CDLASTASSESS = TRUE
GROUP BY weight.CDChartNo, weight.cddate, weight.cdvalueskg
ORDER BY  weight.cdchartno asc, weight.cddate desc
Paul
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 09 Dec 2007 at 5:33pm
wow. That is quite some SQL statement. Yes, you can enter this directly into CR. Use the Database Expert to connect to SQL and then under the connection name choose Add Command. Enter the SQL in the Command dialog box. That should get you going.
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
apbeaulne
Newbie
Newbie
Avatar

Joined: 05 Dec 2007
Location: Canada
Online Status: Offline
Posts: 11
Quote apbeaulne Replybullet Posted: 09 Dec 2007 at 5:44pm
I am very new to CR could you guide me to where I might find the SQL.  What steps would I take.
Paul
IP IP Logged
apbeaulne
Newbie
Newbie
Avatar

Joined: 05 Dec 2007
Location: Canada
Online Status: Offline
Posts: 11
Quote apbeaulne Replybullet Posted: 09 Dec 2007 at 5:48pm
I found it thank you this is so great.  I am so happy I do not have to use the Wizard.
 
It worked first time out.
Paul
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