views:

91

answers:

2

Hi, I am trying to group my data based on age. I use the following database select:

select * from (
select 0 range_start, 11 range_end, '0-10 days' date_description from dual union
select 11, 21, '11-20 days' from dual union  
select 21, 31, '21-30 days' from dual union  
select 31, 99999, '31+ days' from dual) date_helper
left outer join table
on table.date <= date_helper.range_start*-1 + sysdate 
and table.date > date_helper.range_end*-1 + sysdate 

I then make a group based on the date_description column. I am trying to make it display all groups, even when there are no records, that fall within that group. If there are no records, I want it to have a value of 0, and still print the group.

Thanks!!

+2  A: 

(+1 for completeness of your question. Welcome to SO!)

If there are no records for a group, then obviously Crystal can't report it. I recommend creating a "helper" table in your datasource. Here is what I would do using some form of SQL:

  1. Make a 'helper' table. It will have 1 column and will contain all the groups you want displayed. If the names of the groups are dynamic, you may want to use a select query or make-table query.

  2. Right join from your helper table to your data-table. Send the combined data to Crystal.

  3. In Crystal, use the helper table's column in your groupings and agebucket calculations.

Also, in your calculation, you should add a line: Else "No age";

PowerUser
Hi PowerUser, Thank you for your response. Is there any way to achieve what I want without making changes on the server side? Thanks again!
ntsue
@ntsue, if you're using a version of Crystal that allows you to enter your own SQL (instead of having to use Crystal's Database Expert), you can set up a subquery that acts as a helper table - something like ...` from (select 0 start, 10 end, '0-10 days' date_description union select 11, 20, '11-20 days' union select 21, 30, '21-30 days' union select 31, 99999, '31+ days') date_helper,`...
Mark Bannister
@Mark: Good idea. Can you work that into a complete answer? I'm trying to do that myself and I keep getting SQL compilation errors.
PowerUser
@ntsue, can you store the helper table in an MS Access db or comma-delimited text file, pull it into Crystal, and then do the right-join in Crystal? This way, you won't have to make any changes to your 'real' datasource. If you have control over the machine generating the report, this shouldn't be a problem.
PowerUser
@PowerUser, it's probably because in my comment I used `end` (which is a reserved word in SQL) as a column name.
Mark Bannister
@Mark,@PowerUser, thanks for the suggestions!
ntsue
@PowerUser, I would like to avoid having to export an extra file with that report.. I talked to my supervisor about it and he did not particularly like that solution.
ntsue
@Mark, For some reason when I try to use the SQL Expression field I get the following error for a very simple select statement (select * from table):Error in compiling SQL Expression :Database Connector Error: 'HY000:[Oracle][ODBC][Ora]ORA-00936:missing expression[Database Vender Code: 936]'.
ntsue
I tried googling that error, and it seems that it may be a oracle driver issue. We would like to fix this problem without updating our clients drivers. Does this still seem feasible?
ntsue
@ntsue, was the select statement `select * from table` or was it `(select * from table)` ? If it was the former, try specifying the fields you want to see (with aliases as required) instead of `*`; if it was the latter, try removing the parentheses.
Mark Bannister
@Mark, I tried that. Same Error. I dont know if this helps or not, but I don't even get any syntax highlighting or anything.
ntsue
@ntsue, which did you try - both?
Mark Bannister
ntsue, your goals of not updating the client's drivers and not exporting a 2nd file seem reasonable, but why are you trying to send them the entire .rpt? I always just send my end-users a premade pdf. (I'm asking this mainly for my own knowledge).
PowerUser
@PowerUser, we wrap the rpt in an executable so that they may run the report for different parameters rather than sending a static pdf.@Mark, I tried specifying the fields that I wanted to select. I did not have it written with the parentheses before.
ntsue
@ntsue, in that case I'm stumped. Have you been using Crystal's Database Expert exclusively in reports for Oracle, until now?
Mark Bannister
@Mark, yeah.. I am pretty new to working on these reports, and have never had to use the sql expression fields before.
ntsue
@ntsue, the SQL expression fields weren't what I had in mind; they don't define the dataset - they are like Crystal formulas, but are written in Structured Query Language (SQL), not in the Crystal Reports formula language. Which version of Crystal are you using?
Mark Bannister
@ntsue, I believe is Mark referring to Database->Database Expert->Data->Current Connections->Add Command
PowerUser
Ohh, sorry I am such a crystal noob. I did what you guys suggested and I got the inner join to work. So I am pulling everything from that Command now. However, I am still not sure how to get the group with no data in it to disply.
ntsue
@ntsue, if you got a join to a helper table to work, the answer is to make it a left outer join from the helper table to the data table (or a right outer join from the data table to the helper table).
Mark Bannister
@Mark, I am using a left outer join as you suggested from the helper table to data table, and I am grouping my data based on date_description. However, other than taking much longer for the report to run, I get the same results and problems. Any ideas?
ntsue
@ntsue, please can you amend your question to include the query being run by your report?
Mark Bannister
@Mark, edited the question!
ntsue
@ntsue, have you tried running that query in a query tool, such as TOAD, SQLDeveloper or SQLPlus? I have amended the query in my supplemental answer to include a couple of rows of dummy data - when I try running it in TOAD, it includes rows both for ranges with data on the data table, and for ranges without data on the data table. If you're not seeing helpertable rows for ranges without data on the data table in your query results, can you try running the amended sample and let us know the results?
Mark Bannister
@Mark, @PowerUser Thanks guys!! MUCH appreciated!
ntsue
+1, now that I can vote and this was very helpful earlier!
ntsue
A: 

Expanding on a comment on PowerUser's answer, if you're using a version of Crystal that allows you to enter your own SQL (instead of having to use Crystal's Database Expert), you can set up a subquery that acts as a helper table - something like:

select * from (
select 0 range_start, 11 range_end, '0-10 days' date_description from dual union
select 11, 21, '11-20 days' from dual union  
select 21, 31, '21-30 days' from dual union  
select 31, 99999, '31+ days' from dual) date_helper
left outer join 
(select sysdate-5 mydate from dual union all 
 select sysdate - 25 from dual) mytable
on mytable.mydate <= date_helper.range_start*-1 + sysdate 
and mytable.mydate > date_helper.range_end*-1 + sysdate 

(Oracle syntax - the precise syntax of the query will vary depending on which dialect of SQL you are using.)

EDIT: Changed from SQLServer to Oracle syntax.

FURTHER EDIT: Added some simple sample data.

Mark Bannister
This worked!, I didnt realise it because I had to tweak my select statement etc.. Thank you so much!!!
ntsue