Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: DistinctCount Based on a Field Value Post Reply Post New Topic
Author Message
srklg1
Newbie
Newbie
Avatar

Joined: 10 Sep 2007
Location: United States
Online Status: Offline
Posts: 22
Quote srklg1 Replybullet Topic: DistinctCount Based on a Field Value
    Posted: 01 Nov 2007 at 8:24am
Question for y'all!
 
I have the following query:
 
SELECT
  "tblLoads"."BillToID",
  "tblLoads"."BillToName",
  AVG("tblLoads"."TotalRevenue") as REV,
  YEAR("tblLoads"."InvoiceDate") as YEAR,
  "tblLoads"."InvoiceDate",
  "tblLoads"."PickupDate",
  "tblLoads"."DeliveryDate",
  "tblLoads"."LoadID",
  Count("tblLoads"."LoadID") as LdID,
  SUM("tblLoadDispatch"."DriverPay") as Charges,
  "tblLoads"."LoadClass",
  "tblLoads"."OfficeCode",
  "tblLoads"."Status",
  "tblLoads"."Salesperson"
FROM   tblLoads
LEFT OUTER JOIN
tblLoadDispatch 
ON (tblLoads.LoadID = tblLoadDispatch.LoadID)
WHERE
tblLoads.LoadID = tblLoadDispatch.LoadID and
tblLoads.Status <> 'CANCELLED' And tblLoads.Status <> 'INACTIVE' AND tblLoads.LoadClass <> 'TLQUOTE'  And tblLoads.LoadClass <> 'LTLQUOTE'
GROUP BY
  "tblLoads"."BillToID",
  "tblLoads"."BillToName",
  "tblLoads"."InvoiceDate",
  "tblLoads"."PickupDate",
  "tblLoads"."DeliveryDate",
  "tblLoads"."LoadID",
  "tblLoads"."TotalRevenue",
  "tblLoads"."LoadClass",
  "tblLoads"."OfficeCode",
  "tblLoads"."Status",
  "tblLoads"."Salesperson"
And I run it with a customized record selection formula for dates:
 
{Command.InvoiceDate}  IN ((YearToDate)) or
{Command.InvoiceDate} IN ((LastYearYTD))
 
This enables me to do a year-over-year comparison using one query and have data at the BillToName grouping in 2006 and 2007,
 
My difficulty lies in couting the LoadID values by the year.  For example:
 
BillToName 1              2007 LoadID Cnt                  2006 LoadID Cnt
Customer A                            27                                     42
 
As it sits, the DistinctCount function adds up all the LoadID's regardless of year.  I have tried the following:
 
IF statement with DistinctCount
 
Custom Field Counting the LoadID for the single record
 
And a number of others . . .
 
Any ideas?
 
Thanks so much!!
 
SRK
srklg1
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 01 Nov 2007 at 9:00am
yes, this is tough. The only thing that comes to mind is to do a manual running total and track it yourself. Another idea is to use a command object with multiple SQL statements in it and do two distinct count statements based on the year. You would have to make sure you can still link them back to the main SQL for the join to work.
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
srklg1
Newbie
Newbie
Avatar

Joined: 10 Sep 2007
Location: United States
Online Status: Offline
Posts: 22
Quote srklg1 Replybullet Posted: 01 Nov 2007 at 9:20am

How would I join that back?  The BillToName would be the common field between the two statements.  I'm thinking the custom object would look something like this:

SELECT tblLoads.BillToName, DistinctCount(tblLoads.LoadID)

FROM tblLoads

WHERE tblLoads.InvoiceDate in MonthToDate or

            tblLoads.InvoiceDate in LastYearMTD

Is that what you had in mind?
srklg1
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 01 Nov 2007 at 9:26am
Yep, that's what I'm thinking. Actually, you could create a separate command object for each one and then join in CR using BillToName field.
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
srklg1
Newbie
Newbie
Avatar

Joined: 10 Sep 2007
Location: United States
Online Status: Offline
Posts: 22
Quote srklg1 Replybullet Posted: 01 Nov 2007 at 9:36am

That is a fabulous idea!!

Let me play with it and I'll keep you posted on the results!!
srklg1
IP IP Logged
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.031 seconds.