Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Counting data based on date range Post Reply Post New Topic
Author Message
JustinB
Newbie
Newbie


Joined: 12 Oct 2011
Location: Australia
Online Status: Offline
Posts: 3
Quote JustinB Replybullet Topic: Counting data based on date range
     Posted: 12 Oct 2011 at 9:08pm

Hi all,

I have a tricky one that I've been crunching away at for a while now without result and it's doing my head in!

From a table such as:

ID     Date
1      10/1/2008
1      12/3/2009
1      30/3/2011
2      17/6/2009
2      11/2/2011
3      24/1/2010
3      6/5/2011
4      12/8/2011

I'm trying to find a count of "new", "renewed" and "repeat" records.

New should be ID's that appear for the first time this year (1).
Repeat should be ID's that appear this year and last year (1).
Renewed should be ID's that appear this year and more than 1 year ago (2).

Can anyone please assist?

Regards,
Justin.
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 13 Oct 2011 at 4:54am
I would create groups that are based on dates
zeroth group would be the id (yeah, I added it later)
first group all dates
second group this year and last
third group just this year
 
suppress all sections except third group header or footer (the aggregates work in either)
then create a formula like:
 
local numbervar yrCount;
local stringvar lbl;
 
if distintcount({table.id}, {group 3}) = 1 then( //it is in this year
 yrCount:=count({table.id}, {group 3})) //how many times it occurred this year....just in case
  lbl:="new";
  if count({table.id},{group 2}) > yrCount then(
    lbl:="renewed";
    yrCount = count({table.id},{group 2}) ;
  );
  if count({table.id},{group 1})  > yrCount then
    lbl:="repeat";
);
lbl
 
 
hopefully that works, or at least points to a solution that does
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 13 Oct 2011 at 4:55am
one way...
group on ID
make 3 flag formulas
//thisyear
if table.date in yeartodate then 1
//lastyear
if year(table.date) = dateadd('yyyy',-1,currentdate) then 1
//otheryear
if year(table.date) < dateadd('yyyy',-1,currentdate) then 1
sum each of these at the group level
create 3 running totals that use formula evaluations to get your counts
example
name=cat1
field to summarize=ID
type=distinctcount
evaluation=use a formula
SUM(@thisyear, ID)>0 and
SUM(@lastyear, ID)=0 and
SUM(@otheryear, ID)=0
reset=never
place in repoirt footer
IP IP Logged
JustinB
Newbie
Newbie


Joined: 12 Oct 2011
Location: Australia
Online Status: Offline
Posts: 3
Quote JustinB Replybullet Posted: 13 Oct 2011 at 1:38pm
Fantastic.  Thank you both so much for your help, I'll try out the solutions over the weekend and post up how I went.

Regards,
Justin.
IP IP Logged
JustinB
Newbie
Newbie


Joined: 12 Oct 2011
Location: Australia
Online Status: Offline
Posts: 3
Quote JustinB Replybullet Posted: 23 Oct 2011 at 6:02pm
Took a little longer to get to this than I had hoped for.

DBlank, I used your solution as the basis for my work but I couldn't get a few parts of it to work (mainly the running totals).  I ended up using global variables to track the counters in the group and details sections and then outputting them in the footer.

It is now working, so thank you very much for the help!  You definitely put me on the right track.

Regards,
Justin.
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