Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Sorting in Groups Post Reply Post New Topic
Page  of 2 Next >>
Author Message
GreyDog
Newbie
Newbie
Avatar

Joined: 13 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 11
Quote GreyDog Replybullet Topic: Sorting in Groups
    Posted: 13 Jun 2007 at 8:49am
Hi,
 
i'm having a problem sorting data in groups.
 
The report looks like
 
Account           Turnover
                  Last Month
AAAA              £300
BBBB              £0
CCCC              £2500
DDDD              £0
 
I've got two groups set up, Group 1 is Rep Code, Group 2 is Turnover Last month.
 
I need 2 groups so I can use distinctcount() on the fields and display that in the group header. I use these to work out how many accounts ordered/didn't order last month.
 
However for some reason I can't work out, but is hopefully obvious to someone here, the distinctcount() only works if group 2 is set to sort in ascending order.
 
this means the details are listed A-Z of accounts that didn't order last month, then A-Z of the accounts that did order last month.
 
If i set group 2 to sort 'in original order' it looks ok on the report (sort wise) but the distinctcount() is wrong. It doesn't even work if i sort by Z-A, which I would have thought would be the same as A-Z but upside down.
 
I have two distinctcounts(), the first one counts the number of accounts for each rep. The second counts the number of accounts with a turnover last month=0.
 
So is it possible to sort all the accounts A-Z and to correctly distinctcount the number of accounts where TOLM = 0?
 
I hope I explained that ok.
 
Thanks.
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 13 Jun 2007 at 3:21pm
How are you getting your "Turnover last month" figure?  Also, what is your table structure for this information?  Knowing that will help me figure out how to get you your information.
 
-Dell
IP IP Logged
GreyDog
Newbie
Newbie
Avatar

Joined: 13 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 11
Quote GreyDog Replybullet Posted: 14 Jun 2007 at 3:13am
Hi,
 
Thanks for replying. Here is some more info.
 
The database has 24 turnover fields, ThisYear1 - ThisYear12 and LastYear1 to LastYear12. Period 1 is April and so on.
 
TurnOverLastMonth is a formula field where I do a select case on the current month and return the value of the previous months turnover.
 
The report uses 3 tables all linked on the Account field. I've only listed the fields I use for the report.
 
Accounts
 
Account (code)
Sales_Type  (used in the selection query)
House_Account (used in the selection query)
 
Account_Ledger
 
Account (code)
Ledger (used in the selection query)
TurnOver_LY_1
.
.
TurnOver_LY_12
TurnOver_TY_1
.
.
TurnOver_TY_12
 
Addresses
 
Name (of account)
Rep
 
Unfortunately I can't change the database structure (it's MS SQL) at all as it is run by a software company and I only have read privileges.
Although I could get them to set up a view for me if that would help.
 
I don't really know a lot of SQL otherwise some of this could probably be done with that.
 
The report layout is as follows -
 
Group Header #1
Grouped by addresses.rep
 
@Acc Count  
DistinctCount ({ACCOUNT_LEDGER.ACCOUNT}, {ADDRESSES.REP})
 
@AccNotOrdered     
if {@TurnOverLastmonth}=0 then
distinctCount ({ACCOUNT_LEDGER.ACCOUNT},{@TurnOverLastMonth} )
 
(it works wether i have the if then in there or not, i don't think it makes sense to have it but i thought i'd need something to count only if the turnover was 0. in fact i'm surprised the count actually gives the corect figure at all. )
 
Group Header #2
Grouped by @TurnOverLastMonth
This section is suppressed.
 
Details
Account             TurnOverLastMonth
 
 
I only added the second group as it was the only way i could get the second distinctcount to work.
 
I hope that explains it a bit better.
 
Thanks
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 14 Jun 2007 at 7:09am
Instead of using a group and distinct count, try creating a running total.
 
Field to summarize is {ACCOUNT_LEDGER.ACCOUNT}.
Type of summary is Distinct Count.
Under Evaluate, select Use Formula.  Your formula would probably be "{@TurnOverLastmonth}=0"
Reset on change of field using the field that is your first group.
 
This way you can take out the group on {@TurnOverLastmonth} and your data should appear like you want it to.
 
-Dell
 
IP IP Logged
GreyDog
Newbie
Newbie
Avatar

Joined: 13 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 11
Quote GreyDog Replybullet Posted: 14 Jun 2007 at 7:28am
Hi Dell,
 
thanks for your help so far Smile.
 
Unfortunately I need the information in the group header and it always returns 1.
It works if it is in Details or the footers.
 
I tried modifying the formula to evaluateafter etc, but it still returns 1.
 
Is there a way of using a running total in a header? i know it calculates it before the details, which i guess it why the number is incorrect.
 
that's one of the reasons i ended up with a distinctcount formula as that seems to work even in a header field.
 
Any ideas?
 
Thanks
 


Edited by GreyDog - 14 Jun 2007 at 7:29am
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 14 Jun 2007 at 7:48am

Are you sorting (but not grouping) by account number?  If so, something like this might work.

Create a formula:
if  ({Account_Ledger.Account} <> previous({Account_Ledger.Account}) and ({@TurnedOverLastmonth} = 0) then 1 else 0
 
Then do a sum on this formula.
 
The formula will only work if the account numbers are in order.  It will look to see if it's a duplicate account number and, if it isn't, then look to see if it had 0 turnover.  If both conditions are met, then you have a 1.  By summing the formula you should get the distinct count you're looking for.
 
-Dell
IP IP Logged
GreyDog
Newbie
Newbie
Avatar

Joined: 13 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 11
Quote GreyDog Replybullet Posted: 14 Jun 2007 at 8:35am
Hi Dell,
 
Thanks for your help again Smile.
 
Can I just check with you what i'm doing wrong?
 
I've created a formula called TestFormula:
 
if {ACCOUNT_LEDGER.ACCOUNT}<>previous({ACCOUNT_LEDGER.ACCOUNT}) and
({@TurnOverLastMonth}=0) then 1 else 0
 
I then put that in the detail section and it does show as either 1 or 0 for each account.
 
However I then tried to create a running total field, but it didn't list  TestFormula in the report fields so i couldn't select it.
 
So i then tried creating another formula field and tried using the sum(fld) function. But it when i 'check' the formula it says 'This field cannot be summarized'.
 
Any ideas?
 
Thanks.
 
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 14 Jun 2007 at 8:44am

You don't need to actually put the formula on the report - just because you've defined a formula, it doesn't mean that it has to be on the report in order to use it. 

Also, don't use a running total.  Just put a summary on the report by clicking on the Summary button (has a "sigma" on it that looks like a big "E") or going to the Insert menu and selecting Summary.  You then select your formula and "Sum".
 
Because of the "previous" you may actually have to break down the formula into two formulas - one that uses the other.  It would look something like this:
 
{ACCOUNT_LEDGER.ACCOUNT}<>previous({ACCOUNT_LEDGER.ACCOUNT})
 
 
Then create your sum on {@TestFormula}.
 
I'm not entirely sure this will work, but I think it will.  If it doesn't, I have one more option...
-Dell
IP IP Logged
GreyDog
Newbie
Newbie
Avatar

Joined: 13 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 11
Quote GreyDog Replybullet Posted: 14 Jun 2007 at 9:03am
Hi Dell,
 
thanks again Smile.
 
Same problem though. it won't let me select the TestFormula field in the insert summary box. It is something to do with the @isnotduplicate as if i remove that reference i can select it.
 
I think i'll play around with the formulas and try and combine them some way that will allow me to sum it.
 
If you do have one more option though i'd love to hear it!
 
It's 5pm here so i'll get back on this again tomorrow.
 
Thanks again for all your help.
 
James
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 14 Jun 2007 at 9:51am
Yes, working with the previous and next functions can cause problems when you need them for summaries. Cry
 
The other option I have is to use a subreport that only contains the count that you're looking for.  You would include only the Account Ledger table and whatever table you need to link that to the to the Rep code. 
 
- On the main report, put the subreport in the header where you want your count to appear.
- Link on the Rep code. 
- Remove the borders and make the subreport the approximate size of the data that you'll be displaying.
- In the subreport, suppress everything except one report footer section.
- Add your  {@TurnOverLastMonth} formula (hint:  if you copy the formula object from where it's located ON the main report, paste it into the subreport, the formula will be added to the formula list for the subreport.  You can then delete the object from the subreport since you're not going to be displaying it on the subreport, but it will still be available in the list.) 
- Add a select condition on {@TurnOverLastMonth}= 0. 
- Add a distinct count of the account number and put it in the non-suppressed report footer.
 
It's not the most elegant solution, but this should get you what you need.
 
-Dell
IP IP Logged
Page  of 2 Next >>
Post Reply Post New Topic
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



This page was generated in 0.012 seconds.