Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Adding a if statement to an existing command Post Reply Post New Topic
Author Message
nsk74n
Newbie
Newbie


Joined: 08 Dec 2009
Online Status: Offline
Posts: 11
Quote nsk74n Replybullet Topic: Adding a if statement to an existing command
    Posted: 14 Jan 2010 at 12:07pm
Good afternoon,

I am trying to add an if statement to a command in a subreport I have.

If statement to be added
if reg =2 then 1.4
else if reg =3 then 1.5
else if reg =4 then 1.2
else

--------------
-Current Command Statment in my Subreport-
SELECT
     sum(MRDSPD.Proj*PPL.Yld* can I add the if statement here) [ProjR],
     0 [Coll],
     0 [PerEff]
FROM DrMaster DM
JOIN Detail CD ON (DM.CeID=CD.CeID)
JOIN DShDetail DSD ON (DM.DrID=DSD.DrID)
JOIN DrShPrDetail MRDSPD ON (DSD.ShID=MRDSPD.ShID)
JOIN DrShProDetail DSPD ON ((MRDSPD.ShID=DSPD.ShID) AND (MRDSPD.ProjID=DSPD.ProjID))
JOIN ProPrLink PPL ON ((DSPD.ProcID=PPL.ProcID) AND (DSPD.ProdID=PPL.ProdID))
JOIN ProcDef PD ON (DSPD.ProcID=PD.ProcID)
JOIN ProDef PrD ON (DSPD.ProID=PrD.ProID)
WHERE (DM.FromDateTime>{?Pm-EndDate} AND DM.FromDateTime<{?Pm-NextMonthStartDate})
AND (DM.StID<>4 and DM.StID<>5) AND CD.Reg={?Pm-Reg} AND PrD.ICode LIKE '%PLAT%'

UNION

SELECT
     0 [ProjR],
     sum(DSPD.Coll) [Coll],
     case
          when sum(MRDSPD.Proj*PPL.Yld) = 0 then 0
          else cast(cast(sum(DSPD.Collected)as numeric(9,2))/cast(sum(MRDSPD.Proj*PPL.Yld)as numeric(9,2))as numeric(9,2))
     end [PerEff]     
FROM DrMaster DM
JOIN Detail CD ON (DM.CeID=CD.CeID)
JOIN DShDetail DSD ON (DM.DrID=DSD.DrID)
JOIN DrShPrDetail MRDSPD ON (DSD.ShID=MRDSPD.ShID)
JOIN DrShProDetail DSPD ON ((MRDSPD.ShID=DSPD.ShID) AND (MRDSPD.ProjID=DSPD.ProjID))
JOIN ProPrLink PPL ON ((DSPD.ProcID=PPL.ProcID) AND (DSPD.ProdID=PPL.ProdID))
JOIN ProcDef PD ON (DSPD.ProcID=PD.ProcID)
JOIN ProDef PrD ON (DSPD.ProID=PrD.ProID)
WHERE (DM.FromDateTime>={?Pm-StartDate} AND DM.FromDateTime<={?Pm-EndDate})
AND (DM.StID<>4 and DM.StID<>5) AND CD.Reg={?Pm-Reg} AND PrD.ICode LIKE '%PLAT%'

Any help would be great.
[IMG]smileys/smiley19.gif" align="middle" />
Thanks,
NK

Edited by nsk74n - 14 Jan 2010 at 12:12pm
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 14 Jan 2010 at 1:28pm

You can only use an IF statement if your database will process it.  The command that you enter is parsed by the database, not by Crystal so you can't use Crystal syntax.

You can do this two ways:

1.  Create a formula and have Crystal process the logic.  Use this logic on the report.

2.  Use a Case statement in whatever format your database syntax requires - if your database type will parse Case statements.  For Oracle and MS SQL Server, it looks something like this:

Case reg
  when 2 then 1.4 
  when 3 then 1.5 
  when 4 then 1.2
end
 
or like this:
Case 
  when reg=2 then 1.4 
  when reg=3 then 1.5 
  when reg=4 then 1.2
end
 
For both of these you can include an "else" statement after the last when to set a specific default value.  If you don't use the else, the default value is null.
 
-Dell
IP IP Logged
nsk74n
Newbie
Newbie


Joined: 08 Dec 2009
Online Status: Offline
Posts: 11
Quote nsk74n Replybullet Posted: 14 Jan 2010 at 1:35pm
Thanks. I'll try it. Do I put the case statement in the red part of my command statement.
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 14 Jan 2010 at 1:38pm
Yes.
 
-Dell
IP IP Logged
nsk74n
Newbie
Newbie


Joined: 08 Dec 2009
Online Status: Offline
Posts: 11
Quote nsk74n Replybullet Posted: 15 Jan 2010 at 8:57am
Looks good. Thank you very much.
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.016 seconds.