nest if statement in crystal report - crystal-reports

I am new to crystal report. I need add a nest if statement in recrod selection formula
The original one like this
if {?Company}<>0 then {HQCO.HQCo}={?Company} else 1=1 and
if {?Job}<>'zzzzz' then {udJobEmp.Job}={?Job} else 1=1 and
if {?Employee}<>0 then {udJobEmp.Employee}={?Employee} else 1=1
but i need use nest if statement, i try to do something like this but it is not right.
if ({?Company}<>0 then if
({?Job}<>'zzzzz' then if
( {?Employee}<>0 then {udJobEmp.Employee}={?Employee} then {udJobEmp.Job}={?Job} then {HQCO.HQCo}={?Company} else 1=1)else 1=1)else1=1)
thanks

I try and avoid using if's in selection formula. Try something more like:
({?Company}=0 or {HQCO.HQCo}={?Company}) and
({?Job}='zzzzz' or {udJobEmp.Job}={?Job}) and
({?Employee}=0 or {udJobEmp.Employee}={?Employee})

I'm not sure if I got your logic correct, but this is how you would approach this whole thing:
if ({?Company}<>0) AND ({?Job}<>'zzzzz') AND ({?Employee}<>0) THEN
({udJobEmp.Employee}={?Employee}) AND ({HQCO.HQCo}={?Company}) ELSE
1 = 1
Hope this helps,
Chris

Related

Crystal formula case statement with like

I'm trying to convert sql case statement to crystal formula.
The select in sql had this in it:
...
,pf.Status_category AS Category
,CASE WHEN p.degree LIKE '%P%' AND
pf.department_name LIKE 'Occ%' THEN isnull(pf.department2, '') ELSE isnull(pf.department_name, '') END AS Department,
CASE WHEN p.degree LIKE '%P%' AND pf.department_name LIKE 'Occ%' THEN isnull(pf.Section2, '') ELSE isnull(pf.Section_name, '') END AS Section,
CASE WHEN p.degree LIKE '%P%' AND pf.department_name LIKE 'Occ%' THEN isnull(pf.department3, '') ELSE isnull(pf.department2, '') END AS [Department 2],
CASE WHEN p.degree LIKE '%P%' AND pf.department_name LIKE 'Occ%' THEN isnull(pf.Section3, '') ELSE isnull(pf.Section2, '') END AS [Section 2],
pdd.DepartmentName AS DP
,pdv.PrivilegeDetailText AS Privilages
...
FROM dbo.Person_PrivDtl_V AS pdv INNER JOIN
dbo.Person_Privs_Facs_V ON pdv.M_ID = dbo.Person_Privs_Facs_V.Person_Priv_M_ID INNER JOIN
dbo.PrivDefDepartments_PDF AS pdd ON pdd.PDDept_ID = pdv.PDDept_ID INNER JOIN
dbo.Person AS p ON pdv.Person_ID = p.Person_ID INNER JOIN
dbo.Person_Facilities AS pf ON pf.FacCode = dbo.Person_Privs_Facs_V.FacCode AND pdv.Person_ID = pf.Person_ID
When I convert this to crystal, I can't put this in the columns of the report design. So my idea is to use a formula for each column selected. I have this so far for the first formula, but it won't let me save it:
Formula name= Department:
select {Person_Facilities.Department_name}
case is like "Occ%" : {Person_Facilities.Department2}
The error seems to be the like. I looked up crystal like and it seems ok except for they use "is" for the accepted answer, but when I add "is" the error seems to be the "is" when I try to save it.
What is wrong with this formula so I can use like and the case?
Is there a better way to do this? I suppose I can use a view, but my boss doesn't want views cluttering the DB. Is using a formula the way to do this in crystal? The sql also handled null, which I'm not doing, but I'm not sure how to incorporate that at this point. I'm pretty new to crystal and my team doesn't like questions. We have Crystal Reports 2008 and SQL server 2008 R2.
Not sure if this is what you are looking for but try this;
select {Person_Facilities.Department_name}
case "Occ%" : {Person_Facilities.Department2}
default : 'Unknown';
The default is optional.
To use the LIKE operator, it is as follows;
If {Command_Main.Name} LIKE '*Manager*' then Do Something
To handle NULLS in CR, there is an option to do so in the formula editor. See below. Change it from Exception for Nulls to Default Values for Nulls.

SELECT with substring in ON clause?

I have the following select statement in ABAP:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
INTO corresponding fields of table GT_INSTMUNIC_F
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN EVER AS EV on
MUNIC~POD = EV~VREFER(9).
"where EV~BSTATUS = '14' or EV~BSTATUS = '32'.
My problem with the above statement is that does not recognize the substring/offset operation on the 'ON' clause. If i remove the '(9) then
it recognizes the field, otherwise it gives error:
Field ev~refer is unknown. It is neither in one of the specified tables
nor defined by a "DATA" statement. I have also tried doing something similar in the 'Where' clause, receiving a similar error:
LOOP AT gt_instmunic.
clear wa_gt_instmunic_f.
wa_gt_instmunic_f-mandt = gt_instmunic-mandt.
wa_gt_instmunic_f-bis = gt_instmunic-bis.
wa_gt_instmunic_f-ab = gt_instmunic-ab.
wa_gt_instmunic_f-zzelecdate = gt_instmunic-zzelecdate.
wa_gt_instmunic_f-ZZCERTDATE = gt_instmunic-ZZCERTDATE.
wa_gt_instmunic_f-CONSYEAR = gt_instmunic-CONSYEAR.
wa_gt_instmunic_f-ZDIMO = gt_instmunic-ZDIMO.
wa_gt_instmunic_f-ZZONE_M = gt_instmunic-ZZONE_M.
wa_gt_instmunic_f-ZZONE_T = gt_instmunic-ZZONE_T.
wa_gt_instmunic_f-USAGE_M = gt_instmunic-USAGE_M.
wa_gt_instmunic_f-USAGE_T = gt_instmunic-USAGE_T.
temp_pod = gt_instmunic-pod.
SELECT vrefer
FROM ever
INTO wa_gt_instmunic_f-vrefer
WHERE ( vrefer(9) LIKE temp_pod ). " PROBLEM WITH SUBSTRING
"AND ( BSTATUS = '14' OR BSTATUS = '32' ).
ENDSELECT.
WRITE: / sy-dbcnt.
WRITE: / 'wa is: ', wa_gt_instmunic_f.
WRITE: / 'wa-ever is: ', wa_gt_instmunic_f-vrefer.
APPEND wa_gt_instmunic_f TO gt_instmunic_f.
WRITE: / wa_gt_instmunic_f-vrefer.
ENDLOOP.
itab_size = lines( gt_instmunic_f ).
WRITE: / 'Internal table populated with', itab_size, ' lines'.
The basic task i want to implement is to modify a specific field on one table,
pulling values from another. They have a common field ( pod = vrefer(9) ). Thanks in advance for your time.
If you are on a late enough NetWeaver version, it works on 7.51, you can use the OpenSQL function LEFT or SUBSTRING. Your query would look something like:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN ever AS ev
ON MUNIC~POD EQ LEFT( EV~VREFER, 9 )
INTO corresponding fields of table GT_INSTMUNIC_F.
Note that the INTO clause needs to move to the end of the command as well.
field(9) is a subset operation that is processed by the ABAP environment and can not be translated into a database-level SQL statement (at least not at the moment, but I'd be surprised if it ever will be). Your best bet is either to select the datasets separately and merge them manually (if both are approximately equally large) or pre-select one and use a FAE/IN clause.
They have a common field ( pod = vrefer(9) )
This is a wrong assumption, because they both are not fields, but a field an other thing.
If you really need to do that task through SQL, I'll suggest you to check native SQL sentences like SUBSTRING and check if you can manage to use them within an EXEC_SQL or (better) the CL_SQL* classes.

CASE and NULLS issue

I don't use CASE very often but have a pretty simple query I would like to return a generic statement for when the value is NULL. I have written the below and do not get any errors but it doesn't catch the NULL values either. I have shortened the list of articles as it is several thousand but was hoping someone could point out what I may be doing wrong. Thank you in advance.
**USE Asag_Reporting
GO
SELECT DISTINCT ARTICLE_NUMBER,
CASE PM_NAME
WHEN NULL THEN 'No PM Available'
ELSE PM_NAME
END AS 'PM_NAME'
FROM vw_GIM_Articles
WHERE ARTICLE_NUMBER IN ('15110',
'19228',
'34563',
'36516');**
After more thinking, I would prefer this over what I wrote below.
SELECT DISTINCT ARTICLE_NUMBER,
COALESCE(PM_NAME, 'No PM Available') AS [PM_NAME]
FROM vw_GIM_Articles
WHERE ARTICLE_NUMBER IN ('15110','19228','34563','36516');
Try this.
SELECT DISTINCT ARTICLE_NUMBER,
CASE
WHEN PM_NAME IS NULL THEN 'No PM Available'
ELSE PM_NAME
END AS [PM_NAME]
FROM vw_GIM_Articles
WHERE ARTICLE_NUMBER IN ('15110',
'19228',
'34563',
'36516');

T-SQL where clause case statement

Need some small help with some SQL. I am using a VARCHAR value to determine in a case which logic to use in WHERE clause but I am having some issues writing this case statement.
where
(CASE WHEN #p_flag = 'ATA'
THEN (#p_start_ata IS NULL AND #p_end_ata IS NULL) OR (vso.poa_ata between #p_start_ata and #p_end_ata)
ELSE (#p_start_atd IS NULL AND #p_end_atd IS NULL) OR (vso.pol_atd between #p_start_atd and #p_end_atd)
)
Line 93 Incorrect syntax near the keyword 'IS'.
Its probably a minor error but its frustrating me. Maybe there is a better way of writing this? Thanks!
Like said before you can not use a case statement like you are doing it. So this might be a suggestion:
WHERE
(
#p_flag = 'ATA'
AND
(
(#p_start_ata IS NULL AND #p_end_ata)
OR (vso.poa_ata between #p_start_ata and #p_end_ata)
)
)
OR
(
NOT #p_flag = 'ATA'
AND
(
(#p_start_atd IS NULL AND #p_end_atd IS NULL)
OR (vso.pol_atd between #p_start_atd and #p_end_atd)
)
)
You can't use the CASE like that!
It's use to get values and not restrictions. Check this to view how to properly use CASE.
In your case you've to change your logic probably using IF's and making the wanted SELECT's.

Is DECLARE what I should be using in order to reference a created field within the same program

I'm trying to create a new field and then later on in my program reference that new field within a case statement, but I can't seem to get the syntax right - my error message says there's an error near the '='.
Here's my code:
declare #source_sys_obligation_id varchar(40);
if facility_utilization in ('F')
set source_sys_obligation_id = source_sys_facility_id
else set source_sys_obligation_id = source_sys_utilization_num;
select
source_sys_utilization_num
,source_sys_id
,facility_utilization
,case when source_sys_id in ('AFSEAST','AFSLSAL','DFBDOM','ACBS')
then right('000000000000000' + substring(source_sys_obligation_id,6,10),16)
when source_sys_id in ('MLSTLEND')
then right('000000000000000' + left(source_sys_obligation_id,15),16)
else '' end as No
from BridgeUnderwrite.dbo.t_sag_pimsc1
where source_sys_id in ('AFSEAST','AFSLSAL','DFBDOM','ACBS','MLSTLEND')
order by source_sys_id
;
The error is in reference to the set statements. They should look like:
if facility_utilization in ('F')
set #source_sys_obligation_id = source_sys_facility_id
else
set #source_sys_obligation_id = source_sys_utilization_num;
That ought to do it :) . . . however, source_sys_facility_id and source_sys_utilization_num are most likely going to be your next issues . . . are they variables (or perhaps parameters passed in) as well?
The '#' is part of the name. All T-SQL variable names or procedure parameters have to begin with this character (I assume the reason is so they are easy to discern from table and column names). So you probably need to say set #source_sys_obligation_id ... instead of set source_sys_obligation_id ....
What you want and what you can have are not the same! You cannot create a field in a select stament and then reference it in the same select statment in a case. Your code makes no sense at all and indcates a severe lack of understanding of how variables work.
declare #source_sys_obligation_id varchar(40);
if facility_utilization in ('F')
set source_sys_obligation_id = source_sys_facility_id
else set source_sys_obligation_id = source_sys_utilization_num;
This does not add columns to a select nor would it even populate anything even adding the #sign as some others have suggested because you do not have a select here. Further a variable can only have one value, not a different value per record. So scrap this whole approach. What you really need is a derived table or a CTE. You could also simply embed the first case in the second case. Something like this might get waht you are asking for:
SELECT a.source_sys_utilization_num
,a.source_sys_id
,a.facility_utilization
,CASE WHEN a.source_sys_id in ('AFSEAST','AFSLSAL','DFBDOM','ACBS')
THEN RIGHT('000000000000000' + SUBSTRING(a.source_sys_obligation_id,6,10),16)
WHEN a.source_sys_id in ('MLSTLEND')
THEN RIGHT('000000000000000' + LEFT(a.source_sys_obligation_id,15),16)
ELSE '' END AS [No]
FROM
(SELECT
source_sys_utilization_num
,source_sys_id
,facility_utilization
,CASE WHEN facility_utilization = 'F' THEN source_sys_facility_id
ELSE source_sys_utilization_num END AS source_sys_obligation_id
FROM BridgeUnderwrite.dbo.t_sag_pimsc1
WHERE source_sys_id in ('AFSEAST','AFSLSAL','DFBDOM','ACBS','MLSTLEND')) a
ORDER BY source_sys_id
Too busy to write the other versions. Maybe someone else will supply.