Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: CR 2011 sql query formula Post Reply Post New Topic
Author Message
bullsfan03
Newbie
Newbie


Joined: 12 Jul 2012
Online Status: Offline
Posts: 5
Quote bullsfan03 Replybullet Topic: CR 2011 sql query formula
     Posted: 12 Jul 2012 at 4:03pm
I'm working on a report for health benefit deductions in Crystal Reports 2011, grabbing fields from SQL views. The view I'm stuck on, is called emp_groups.group_code. Here's a screenshot of the fields in the view.


What I want to do, is create a formula that grabs all the groups the employee is in from this view(they can be enrolled in 1 up to all of the groups. And then put it in my report. I'm a Crystal Syntax newbie and thought a select statement would work, but it only grabs the first group someone is enrolled in. (ie: a person may be enrolled in LUNLRN,PHYEX,&WGHTLOSS but only LUNLRN shows up for me after this select statement)

select {emp_groups.group_code}
   Case "COACHEDU":
      "Coach"
   Case "HRA":
      "HRA2012"
   Case "LUNLRN":
      "Lunch&Learn"
   Case "PHYSICAL":
      "Phyiscal"
   Case "PHYEX":
      "Exercise"
   Case "WGHTLOSS":
      "WeightLoss"
   Default :
      "";

any help is appreciated, thanks! :)
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 16 Jul 2012 at 10:35am
shared stringvar gps:="";
if {emp_groups.group_code} = "COACHEDU" Then gps := gps + ", " + "Coach";
if {emp_groups.group_code} = "HRA" Then gps := gps + ", " + 
      "HRA2012";
if {emp_groups.group_code} = "LUNLRN" Then gps := gps + ", " + 
      "Lunch&Learn";
if {emp_groups.group_code} = "PHYSICAL" Then gps := gps + ", " + 
      "Phyiscal";
if {emp_groups.group_code} = "PHYEX" Then gps := gps + ", " + 
      "Exercise";
if {emp_groups.group_code} = "WGHTLOSS" Then gps := gps + ", " + 
      "WeightLoss";
if gps <> "" then
  mid(gps, 3) //trim first comma
else
  gps  
 
this will return a string of groups the person belongs to.
 
is this what you are looking for?
IP IP Logged
bullsfan03
Newbie
Newbie


Joined: 12 Jul 2012
Online Status: Offline
Posts: 5
Quote bullsfan03 Replybullet Posted: 25 Jul 2012 at 2:03pm
I tried the code lockwelle provided, but it still just pulls the very first item from the table that the person is enrolled in.

Appreciate the help

Edited by bullsfan03 - 25 Jul 2012 at 2:03pm
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 26 Jul 2012 at 4:24am
What type of section are you using the formula in?  If it's in a group header, you'll only get the first value.  You would have to place it in a group footer in order to get all of the values.  (You might be able to get it to work in a header if you use "WhileReadingRecords;" at the beginning of the formula.)
I also don't see where the formula is getting reset at the beginning of each employee.  Assuming you have a field called {emp.emp_id} as an identifier for each employee, I would replace the first line of what lockwelle posted with something like this:
 
stringvar gps;
if OnFirstRecord or {emp.emp_id} <> previous({emp.emp_id}) then gps := "";
 
This will initialize gps to an empty string if the report is on the first record or at the start of each employee.
 
-Dell
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 26 Jul 2012 at 9:06am
I was thinking that this would be in the Details section and that all of the groups would be available.  I didn't reset as the group string just because primarily I forgot...I was thinking to just provide syntax for the immediate question.
 
I also did not provide a display formula, which I thought would go in the group footer, nor is there code to prevent duplications.
 
Hope this clarifies my thoughts on the code.
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