Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Current and previous date Post Reply Post New Topic
Author Message
Ethiopia@SMS
Newbie
Newbie


Joined: 02 Dec 2010
Online Status: Offline
Posts: 4
Quote Ethiopia@SMS Replybullet Topic: Current and previous date
     Posted: 02 Dec 2010 at 2:59am
Hei,
 
I need help on getting first row of the current date and last row of the previous date. Is it possible to make it by formula or use sql to get the exact data?
 
Thank you!
Patiency is a key for development
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 02 Dec 2010 at 4:04am
are you speaking of a grouping by date?  This would imply a first row of one group and a last row of another group.
 
If so, I would use the Previous command in a formula that I would place the group header.  In the group header you are 'sitting' on the first record of the current group so the previous record is the last record of the prior group.
 
HTH
IP IP Logged
c16271
Groupie
Groupie
Avatar

Joined: 24 Aug 2010
Location: United States
Online Status: Offline
Posts: 48
Quote c16271 Replybullet Posted: 02 Dec 2010 at 6:36am
Look at the SQL built-in: ROWNUM

Example: Getting the first record with today's date:

SELECT * FROM xxtable1
where xxtable1.date = SYSDATE
AND ROWNUM = 1


NOTE--this is a horrible way to code. But this should answer your question.

Ideally, you use ROWNUM = 1 when you know with absolute certainty, there should only be 1 record returned--allows for better SQL performance.


Edited by c16271 - 02 Dec 2010 at 6:37am
IP IP Logged
Ethiopia@SMS
Newbie
Newbie


Joined: 02 Dec 2010
Online Status: Offline
Posts: 4
Quote Ethiopia@SMS Replybullet Posted: 02 Dec 2010 at 9:19pm
Hi lockwelle ,
 
Thank you for your reply.
 
No not grouping it is on detail section. I have made a sql to get all the fields from several tables. And all tables are joined "left join". So what I need now is to get one field first row from today's date and  the same field last row from yesterday date. I think these should work in the formula like
 
if {datefield} = currentdate then
"First row"(amountfield}
 
So here I need the correct formula.
And for the previous date
 
if {datefield}= previousdate then
 
"Last row"(amountfield)
 
But if you have any idea on Sql to (sub query...) please....
 
Eth
 
 
Patiency is a key for development
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 03 Dec 2010 at 3:46am
grouping would be a solution, depending on what is required of the report.
 
a stored procedure would allow you to put all the data on 1 line so that no grouping is necessary
 
Here's the problem, Crystal doesn't make multiple passes through the data (well it does, but not for this), and you cannot request information from Crystal or the database ie, you cannot find a record in Crystal once you know another.  Crystal reads the results of the query from top to bottom...once it has been sorted, so if the 2 records that you are seeking are not next to each other, accessing information becomes difficult.
 
One could store data in a shared variable.  If there are lots of records that you are tracking, you could use an array...but Crystal arrays are very simple, only 1 dimension, which means to track several fields means at least as many arrays and they all have to be indexed the same way. Seems like a nightmare to me.
 
An alternative is to add groups into the report, so that the 2 records that you seek are next to each other, then you can use Previous or Next to access the data.
 
Or you can gather your data from a stored procedure, where you can put all the data on the same row, and use whatever logic you want to move things around
 
Or you can create subreports, since Crystal will read the data again as it creates the new report.  This is a last resort, as everytime you call the subreport, Crystal is going to hit the database and retrieve the data again, which impacts performance not just on the report, but potentially the database server and network.
 
And there are probably more ways...there is always more than 1 way get there, it's just a matter of what works for you.
 
HTH
IP IP Logged
Ethiopia@SMS
Newbie
Newbie


Joined: 02 Dec 2010
Online Status: Offline
Posts: 4
Quote Ethiopia@SMS Replybullet Posted: 07 Dec 2010 at 1:40am
Hi lockwelle,
 
I managed to get the first and last rows. But the thing is thatI need to display both on group header. No problem for the first row but the last row couldn't happen on group header. Do u have any idea? And how can I sum only this first records of different grouping?
Patiency is a key for development
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 07 Dec 2010 at 3:48am
if I understand correctly, you want to display the first and last row together in a group header?
 
the only way that I can think of doing that is to use either:
a) a stored procedure where both rows have been consolidated into one 'super' row, so that both rows of data are available on the one line or...
 
b) a subreport which will impact the performance of the report depending on the number of group headers you have (it will connecting to the database and reading data for each subreport)
 
HTH
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