Skip to Content
1

Crystal Reports IF statement in Command window

Jan 11 at 02:02 PM

51

avatar image

Hello.

I'm trying to create a report that depends on what a user enters, a different SELECT statement is to run. For example, if a user enters a calendar year value, then a SELECT statement for the calendar year runs. If a user enters a fiscal year value, then a SELECT statement for the fiscal year runs. I didn't even know that I could do this from the Command windows to begin with. My code looks like:

IF ( ({?BeginCY}) = ' ' )
BEGIN
select fiscal_year,station_response_area_category, count(*) as Incident_count
from dw_prod.dbo.vw_unit_response
where fiscal_year between {?BeginFY} and {?EndFY}
group by fiscal_year,station_response_area_category
END
ELSE 
BEGIN
select year,station_response_area_category, count(*) as Incident_count
from dw_prod.dbo.vw_unit_response
where year between {?BeginCY} and {?EndCY}
group by year, fiscal_year,station_response_area_category
END

Of course the code isn't working right now. I'm trying to figure out how to do the IF section... Please help.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Abhilash Kumar
Jan 16 at 05:58 PM
1

OK. I see why it's doing that.

Add a common 'column' alias for Fiscal_Year and year columns.

IF '{?YearType}' = 'Fiscal'
BEGIN 
SELECT fiscal_year Year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE fiscal_year between '{?BeginYear}' and '{?EndYear}'
GROUP BY fiscal_year,station_response_area_category 
END 
IF '{?YearType}' = 'Calendar' 
BEGIN 
SELECT year Year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE year between '{?BeginYear}' and '{?EndYear}'
GROUP BY year,station_response_area_category 
END

-Abhilash

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thank you! Saved my butt again!

0
Abhilash Kumar
Jan 12 at 08:07 AM
1

Hi Kenshin,

Here's what I would do:

1. Create a parameter in the Command Window called "YearSelection". It would have two static values in its list:

  • Calendar Year
  • Fiscal Year

2. Create two parameters to accept the Start and End years. You can name them "BeginYear" and "EndYear"

3. Modify the SQL to use this code:

DECLARE @YearType varchar 
SET @YearType = {?YearSelection} 

IF @YearType = 'Fiscal Year' 
BEGIN 
SELECT fiscal_year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE fiscal_year between {?BeginYear} and {?EndYear} 
GROUP BY fiscal_year,station_response_area_category 
END 

IF @YearType = 'Calendar Year' 
BEGIN 
SELECT year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE year between {?BeginYear} and {?EndYear} 
GROUP BY year,station_response_area_category 
END

Make sure to add the list of values to the "YearSelection" prompt by editing the parameter from the "Field Explorer".

When this report is run, the user will be prompted for three prompts:

  • Year Type Selection
  • Start Year
  • End Year

When the user selects Year Type = 'Calendar Year', the report returns data for the Calendar year based on the date range in the Start and End prompt.

When the user selects Year Type = 'Fiscal Year', the report returns data for the Fiscal year based on the date range in the Start and End prompt.

I hope this helps.

-Abhilash

Show 14 Share
10 |10000 characters needed characters left characters exceeded

Thanks, Abhilash, as usual!

I followed your instruction, and I'm getting an error right now. When I run the report, I get an error and it says, syntax near 'Year'. I presume the error is due to a parameter and am trying to debug this thing.

In the meantime, any idea about the error?

0

Hi Kenshin,

Try running the query in the database. Replace the parameter fields with static values:

DECLARE @YearType varchar 
SET @YearType = 'Fiscal Year'

IF @YearType = 'Fiscal Year' 
BEGIN 
SELECT fiscal_year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE fiscal_year between 2016 and 2017 
GROUP BY fiscal_year,station_response_area_category 
END 

IF @YearType = 'Calendar Year' 
BEGIN 
SELECT year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE year between 2016 and 2017 
GROUP BY year,station_response_area_category 
END
0

It seems to be fine in the database. I get, "Command(s) completed successfully."

0

Do you see any results?

Could you check if the datatype of the parameters and the fields match?

For e.g. datatype of the parameters {?BeginYear} and {?EndYear} should be same.

You may also try enclosing the parameters in quotes like so:

WHERE year between '{?BeginYear}' and '{?EndYear}'
0

No, sir. It did not give me any result other than the message.

The datatype of the parameters is 'varchar' in the database. So I think the datatype is correct here.

I also tried enclosing the parameters in quotes, but the same syntax error...

0

OK. I tried this and this doesn't give me any error, BUT it doesn't ask to select a year type when I try to refresh...

DECLARE @YearType varchar
--SET @YearType = {?YearType}

IF '{?YearType}' = 'Fiscal'
BEGIN 
SELECT fiscal_year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE fiscal_year between '2016' and '2017' 
GROUP BY fiscal_year,station_response_area_category 
END 

IF '{?YearType}' = 'Calendar' 
BEGIN 
SELECT year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE year between '2016' and '2017' 
GROUP BY year,station_response_area_category 
END

0

Do you still have the YearType prompt in the command window?

Have you trued without the quotes?

-Abhilash

0

Yes, and now it seems to be running all right. A HUGE THANKS TO YOU!!

My next question is, how do I show the result - fiscal year data to the calendar year data, upon user selection? I guess I could modify the code to show both calendar and fiscal data and create a formula to show the one that user select?

0

Expand the Field Explorer > Look for the 'YearType' selection prompt.

Drag and drop this prompt on the Report Header and it should display what is selected.

-Abhilash

1

It's like one mountain after another...The reports does ask me to select the year type, but it's not asking for the begin and end year, even though the parameters are in the code. What could it be now?...

IF '{?YearType}' = 'Fiscal'
BEGIN 
SELECT fiscal_year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE fiscal_year between '{?BeginYear}' and '{?EndYear}'
GROUP BY fiscal_year,station_response_area_category 
END 


IF '{?YearType}' = 'Calendar' 
BEGIN 
SELECT year, 
station_response_area_category, 
count(*) as Incident_count 
FROM dw_prod.dbo.vw_unit_response 
WHERE year between '{?BeginYear}' and '{?EndYear}'
GROUP BY year,station_response_area_category 
END
0

Are the BeginYear and EndYear in the command prompt?

Do you see them to the right in the prompts panel?

-Abhilash

0

They are in the command prompt, but weirdly I wasn't prompted for them until I added them to the report design section....

About the data selection between fiscal and calendar, I did try your suggestion of dragging the 'Year Type" to the report header, but if I refresh with different year type, the report cannot process and throws an error:

One of more fields could not be found in the result set. Use verify database to update the report.

0

OK and what happens after you 'verify database'?

-Abhilash

0

Then it brings the data, but I have to do this each time when I switch from fiscal to calendar and so on.

0