Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Filter records by exact part of string Post Reply Post New Topic
Author Message
hummeri7582
Newbie
Newbie


Joined: 22 Aug 2008
Online Status: Offline
Posts: 3
Quote hummeri7582 Replybullet Topic: Filter records by exact part of string
     Posted: 01 Apr 2009 at 7:57am
Hi there,
 
Excuse me if this seems rather elementary, but I'm just not seeing the best way to do this.
 
I've been asked to write a report for all inmates in a prison that have NOT been convicted of certain offenses.  The problem is that offense are actually statute numbers that are very complex. 
 
Pulling the data is not the problem, as it's readily available.  Basically, I grouped by the inmate number, and put everything he's ever been convicted of in his "career" in the detail line.  What I have looks like this:
 
INMATE#:  123456    INMATE_NAME:  JOSEPH D. JAILBIRD 
 
      123.12
      234.12(1)
      234.125(2)
      234.127
      301.01
      301.01(1)
      301.01(1B)(2)
 
 
So you can see, the inmate number and name are grouped as Group 1, and the statutes he is convicted of are details. 
 
Now, they have given me a list of statues that should exclude inmates from being on this report.  The problem I have is the way the statute numbers are.  For instance, assume that I DO NOT want to include anyone with a 301.01 conviction on the report. 
 
I used a formula that said:
 
"if (InStr({offender_offenses.stat},"301.01") > 0) then 1
 
The Idea being that if the formula returned a "1", I could do a group level sum on them and omit any offender where the sum was greater than 0. 
 
But, my formula will also assign a "1" to 301.01(1) and 301.01(1B)(2), since they contain "301.01" in the string.   I do not necessarily want that. 
 
Any ideas on how to get around this?
 
Thanks! 
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 01 Apr 2009 at 8:08am
Sounds like you are going to have to do multiple "if then" or an long "in list" statement to get your 1 or 0 per record for your Sum field to suppress on. The instring will not work due to the overlaps and that each starts in the same part of the string.
Try something like:
if {offender_offenses.stat} in ["301.01", "311.00", "123456","whatever", etc.] then 1 else 0
IP IP Logged
Chrissy
Newbie
Newbie


Joined: 01 Apr 2009
Online Status: Offline
Posts: 9
Quote Chrissy Replybullet Posted: 01 Apr 2009 at 8:35am
What about creating a group in a specified order and just say Where..
"not equal to" 301.01...  Then it would automatically exclude those from the report.
 
Or... what about just excluding those in your SQL statment so they don't even come into the report.
 
WHERE offense NOT IN ("301.01", etc)


Edited by Chrissy - 01 Apr 2009 at 8:41am
IP IP Logged
hummeri7582
Newbie
Newbie


Joined: 22 Aug 2008
Online Status: Offline
Posts: 3
Quote hummeri7582 Replybullet Posted: 01 Apr 2009 at 9:01am
Originally posted by DBlank

Sounds like you are going to have to do multiple "if then" or an long "in list" statement to get your 1 or 0 per record for your Sum field to suppress on. The instring will not work due to the overlaps and that each starts in the same part of the string.
Try something like:
if {offender_offenses.stat} in ["301.01", "311.00", "123456","whatever", etc.] then 1 else 0
 
Thanks, I think that will work.   I don't know why I didn't think of it...  Doh!
 
 
IP IP Logged
hummeri7582
Newbie
Newbie


Joined: 22 Aug 2008
Online Status: Offline
Posts: 3
Quote hummeri7582 Replybullet Posted: 01 Apr 2009 at 9:27am
Originally posted by Chrissy

What about creating a group in a specified order and just say Where..
"not equal to" 301.01...  Then it would automatically exclude those from the report.
 
Or... what about just excluding those in your SQL statment so they don't even come into the report.
 
WHERE offense NOT IN ("301.01", etc)
 
I'm not sure I'm following.   I'm not trying to suppress the statute of "301.01" or whatever, I'm trying to suppress the offender who has been convicted of it, ever.  So to do that, I think I need to bring it into the report and then suppress the inmate who has one of the statutes in his history. 
 
It's a one to many relationship -- there's only one offender number per offender, but some of these guys have been convicted of 20 or 30 things over the years.   (Yeah, some people just don't learn, it would seem.)
 
Anyway, the idea is that in order to suppress an offender who has been convicted of 301.01, I need to bring 301.01 into the report, because if I don't, I won't know who was convicted of it.   :) 
 
I also avoid 'NOT IN" queries at all costs.   This database was migrated over from an old mainframe system, which itself was brought in from an even older mainframe system...   Thus, there are millions of convictions going back to the mid 1960s, and the DBAs around here will hunt you down if you use "not in" on an un-indexed field on such a large table.   One guy did this -- he started his query on Friday afternoon, locked his workstation and went home for the weekend.  It was Sunday afternoon when some Maintenance scripts kicked off that the weekend duty DBA noticed the thread was still open...  The DBAs were not impressed.   LOL    
 
   
 
 


Edited by hummeri7582 - 01 Apr 2009 at 9:33am
IP IP Logged
Chrissy
Newbie
Newbie


Joined: 01 Apr 2009
Online Status: Offline
Posts: 9
Quote Chrissy Replybullet Posted: 01 Apr 2009 at 10:01am
Sorry, I misunderstood wanting to suppress the whole offender instead of just the offense... 
 
However, I still think you can achieve this better through your SQL query.  Using "NOT IN" would only cause a problem if you were using it on a sub query that pulled all the information from an entire table in.  A sub query is executed for every instance of the main query.  Using NOT IN as (offense NOT IN ("1", "2", "3")) has the same effect as saying ((offense <> "1" AND offense <> "2" AND offense <> "3").  It's just a cleaner way of writing it.
 
I have very large tables at my company too, and the more I cut the records down in the SQL query, the faster the reports run.
 
You could try something like this...
 
SELECT offender_Num, offender_name, of.offense
FROM offender_table o, offense_table of, (SELECT DISTINCT(offender_num) FROM offense_table WHERE offense NOT IN ("301.01", "etc")) as ODB
WHERE o.offender_num = odb.offender_num
AND of.offender_num = o.offender_num 
 
You don't need to bring the offender in the report to suppress it because it won't be in the report to begin with.  :)
        


Edited by Chrissy - 01 Apr 2009 at 12:45pm
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