Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Formula to retreive date in database Post Reply Post New Topic
Author Message
nickyjacob
Newbie
Newbie


Joined: 07 Dec 2007
Online Status: Offline
Posts: 3
Quote nickyjacob Replybullet Topic: Formula to retreive date in database
     Posted: 07 Dec 2007 at 12:59am

I need to create a forumla that will allow me to extract a date from a database table.I have a table with a field called datePerformed and and another table with a field called activityPerformed,both tables are joined together in crystal.In the application I have if a certain activity is performed an entry is written in the database for both the datePerformed and activityPerformed.

My problem is that I need to be able to search the table for a certain activity performed and get the corresponding date that the activity was performed.
Below is what I am trying to do if it makes sense, I know what I want to do but I cannot do it in Crystal reports
 
dateVar startDate = select table1.datePerformed where table2.activityPerformed = "The ActivityName1"
date Var endDate = select table1.datePerformed where table2.activityPerformed = "The ActivityName2"
dateDiff("d",startDate,endDate)


Edited by nickyjacob - 07 Dec 2007 at 1:00am
IP IP Logged
Lugh
Senior Member
Senior Member
Avatar

Joined: 14 Nov 2007
Online Status: Offline
Posts: 377
Quote Lugh Replybullet Posted: 07 Dec 2007 at 7:57am
OK, critical question:  For a given activity, will there be one and only one datePerformed value in the table?  Based on your description, I'm guessing that is the case.  But, the solution depends very much on whether that is absolutely true.

How are table1 and table2 linked?

When you retrieve the data from the database, what does a typical record look like?

For a given "The ActivityName1" how do I know what "The ActivityName2" is going to be? 

If I am thinking about this correctly, it is not going to be a simple solution.  You are going to have much better luck doing this in SQL, with a somewhat sophisticated self-join of the inner join result. 
IP IP Logged
nickyjacob
Newbie
Newbie


Joined: 07 Dec 2007
Online Status: Offline
Posts: 3
Quote nickyjacob Replybullet Posted: 09 Dec 2007 at 6:18am

There may be more than one datePerformed in the table, an activity can move from one state to another and then back. So I need to get the most recent datePerformed

Table 1 and table 2 are linked through the primary key of the table 1
 
I know the 2 activities thjat I need to look for so the Activity name can be inserted into lets say a SQL statement to search
 
What is the easiest way to insert the SQl statement, I will also need tro insert a formula
IP IP Logged
Lugh
Senior Member
Senior Member
Avatar

Joined: 14 Nov 2007
Online Status: Offline
Posts: 377
Quote Lugh Replybullet Posted: 10 Dec 2007 at 5:16am
OK, let's take a shot at this.  I'm going to assume, for the sake of this, that all you need is the date.  If you need other information from the record with the latest date, it gets a little trickier.

There are a couple different approaches. 


SELECT tblStart.datePerf AS StartDate,
    tblEnd.datePerf AS EndDate,
    DateDiff(d, tblStart.datePerf, tblEnd.datePerf) AS DaysOpen
FROM
    (SELECT Max(table1.datePerformed) AS datePerf
      FROM table1
      JOIN table2
      ON table1.PrimaryKey = table2.ForeignKey
      WHERE table2.activityPerformed = 'The ActivityName1') tblStart
JOIN
    (SELECT Max(table1.datePerformed) AS datePerf
      FROM table1
      JOIN table2
      ON table1.PrimaryKey = table2.ForeignKey
      WHERE table2.activityPerformed = 'The ActivityName2') tblEnd


This method figures the maximum values for each date in separate subqueries, performs a cross product (since there's only one record in each result, this is pretty trivial), and takes the result.



SELECT
    MAX(CASE table2.activityPerformed WHEN 'The ActivityName1'
            THEN table1.datePerformed ELSE '01/01/1970' END) AS StartDate,
    MAX(CASE table2.activityPerformed WHEN 'The ActivityName2'
            THEN table1.datePerformed ELSE '01/01/1970' END) AS EndDate,
    DATEDIFF(d,
            MAX(CASE table2.activityPerformed WHEN 'The ActivityName1'
                    THEN table1.datePerformed ELSE '01/01/1970' END),
            MAX(CASE table2.activityPerformed WHEN 'The ActivityName2'
                    THEN table1.datePerformed ELSE '01/01/1970' END)) AS DaysOpen
FROM table1
JOIN table2
ON table1.PrimaryKey = table2.ForeignKey


This method is much better if you do want to get additional information out.  But, the CASE construction can be tricky, and it's easy for bugs to sneak in.






IP IP Logged
nickyjacob
Newbie
Newbie


Joined: 07 Dec 2007
Online Status: Offline
Posts: 3
Quote nickyjacob Replybullet Posted: 11 Dec 2007 at 2:03am
This may sound likw a stupid question but here goes
 
Where do I put the SQL statement in a formula or somewhere else
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 13 Dec 2007 at 12:14am
You can put it in a SQL Command object (found in the Data tab under the currently open connection node). 
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
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