Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Select SQL Results Based on Min Value Post Reply Post New Topic
Author Message
putnamm
Newbie
Newbie


Joined: 20 May 2008
Location: United States
Online Status: Offline
Posts: 2
Quote putnamm Replybullet Topic: Select SQL Results Based on Min Value
     Posted: 20 May 2008 at 12:55pm
All,
  Haven't yet had an opportunity to look deeply at this forum but -- I'll fire away with a newbie question....
 
I'm using Crystal XI to create a summary report.  Presently, too many detail records are retrieved.  The query that returns these records follows, it was generated using the
I have what is to me a complex query that returns appropriate detail records -- matter of fact, too many.... here is the automatically generated using the report->record selection editor.
 

 SELECT DISTINCT CallLog.CallID, CallLog.CallType, CallLog.RecvdDate, CallLog.CustID, Asgnmnt.Assignee, CallLog.Priority, CallLog.Category, Asgnmnt.Resolution, CallLog.RecvdTime, Asgnmnt.DateAcknow, Asgnmnt.TimeAcknow, Asgnmnt.WhoAcknow, Subset.RHC
 FROM   (heat.heat.CallLog CallLog LEFT OUTER JOIN heat.heat.Asgnmnt Asgnmnt ON CallLog.CallID=Asgnmnt.CallID) LEFT OUTER JOIN heat.heat.Subset Subset ON CallLog.CallID=Subset.CallID
 WHERE  CallLog.Category LIKE 'break%' AND Subset.RHC LIKE 'LMC%' AND CallLog.Priority='1' AND Asgnmnt.DateAcknow NOT  LIKE '' AND (Asgnmnt.Resolution='Completed' OR Asgnmnt.Resolution='Reassigned')
 ORDER BY CallLog.CallID
 
What I want to do is from the result set, is further reduce it by selecting just the minimum value for the combined elements of Asgnmnt.DateAcknow and Asgnmnt.TimeAcknow.  
 
In this case, there are multiple rows returned for a given CallLog.CallID.  Each row contains the values for asgnmnt.dateacknow and asgnmnt.timeacknow.   I'm only after the rows where the combined values previously mentioned are the minimum or min for the CallLog.CallID. I'm sure this is an easy one to solve but for the life of me, I can't figure it out.  THe data base is SQL 2000 or above...
Can anyone HELP!!!!
Thank You very much
Mark
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 20 May 2008 at 4:08pm
I think you'll have to create a command to do this - there's no way to use a min calculated in Crystal in your selection criteria.
 
I would use something like this:
SELECT DISTINCT
CallLog.CallID, CallLog.CallType, CallLog.RecvdDate, CallLog.CustID, Asgnmnt.Assignee, CallLog.Priority, CallLog.Category, Asgnmnt.Resolution, CallLog.RecvdTime, Asgnmnt.DateAcknow, Asgnmnt.TimeAcknow, Asgnmnt.WhoAcknow, Subset.RHC
FROM heat.heat.CallLog CallLog
LEFT OUTER JOIN heat.heat.Asgnmnt Asgnmnt ON CallLog.CallID=Asgnmnt.CallID
LEFT OUTER JOIN heat.heat.Subset Subset ON CallLog.CallID=Subset.CallID
WHERE  CallLog.Category LIKE 'break%'
  AND Subset.RHC LIKE 'LMC%' AND CallLog.Priority='1'
  AND Asgnmnt.DateAcknow is not null
  AND (Asgnmnt.Resolution='Completed'
    OR Asgnmnt.Resolution='Reassigned')
  AND Asgnmnt.DateAcknow + Asgnmnt.TimeAcknow =
    (Select min(Asgnmnt1.DateAcknow + Asgnmnt.TimeAcknow)
     from heat.heat.Asgnmnt Asgnmnt1
     where Asgnmnt1.CallID = CallLog.CallID)
 ORDER BY CallLog.CallID
 
-Dell
IP IP Logged
putnamm
Newbie
Newbie


Joined: 20 May 2008
Location: United States
Online Status: Offline
Posts: 2
Quote putnamm Replybullet Posted: 20 May 2008 at 4:36pm
I'll give this a try --- Thank you Hilfy!!!--- I'll post the results... & as you pointed out, create a command.   Didn't look like anyway to do it otherwise. Oh... One other thing... I failed to mention!! -- the two elements are Text not Date and Time elements..... anything different based on that?
THanks again...
Mark
IP IP Logged
Lugh
Senior Member
Senior Member
Avatar

Joined: 14 Nov 2007
Online Status: Offline
Posts: 377
Quote Lugh Replybullet Posted: 21 May 2008 at 5:26am
The main thing to watch for is that text values sort alphabetically, which is not the same as sorting date values.  So, while the report will function, it may not agree with you as to what the "minimum" value is.  One possible workaround is to use the various date conversion functions to turn the text value into a datetime value.


IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 21 May 2008 at 7:08am
I thought I remembered that dates in HEAT are stored as text (we use it too, but I don't write the reports for it...)  It shouldn't make any difference in the SQL, though - although that may depend on what format they're in in the text field.
 
-Dell
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