Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"if then" in select formula??

Status
Not open for further replies.

pelajhia

Programmer
May 19, 1999
592
US
I have a report which passes a selected value from a table to a subreport.
Based on the value passed (wonlinkkey),
I would like to change the select statment in the subreport.
I have 'successfully' used the following formula in the select expert, but it runs really slowly:
if
{?Pm-HISTORY.WOLINKKEY}=1
then
ltrim({HISTORY.WONO}) = {?Pm-@InputToAllCaps}
else
{?Pm-HISTORY.WOLINKKEY} = {HISTORY.WOLINKKEY}

If I just have the second line ({?Pm-HISTORY.WOLINKKEY} = {HISTORY.WOLINKKEY}) in the select statement, it runs really fast, but doesn't pull back the right records for wonlinkkeys = 1.

I have attempted to edit the sql directly, too, but I don't know much about the variable declaration syntax and I'm getting various errors - is this a viable route to spend some time on?
Or, more generally, is there a GOOD way to do this?
Thanks in advance.
 
I'd guess that the SQL isn't getting passed because of the ltrim().

Try creating a SQL Expression to trim the field for you and then use the trimmed field in the comparison.

I would have provided an example were you to have shared the database type, version, and version of Crystal.

-k
 
The use of a function (ltrim()) in your record selection condition causes the filtering process to occur on the PC rather than in the Database server.

Instead of
ltrim({HISTORY.WONO})
create and use an equivalent "SQL Expression Field" (the node below "Formula Fields").

Cheers,
- Ido CUT, Visual CUT, and DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
S.V., next time you get ready to answer a question, give me a call and let me know you're "on the case"... :eek:) CUT, Visual CUT, and DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
You take every post number that's divisable by a prime, I'll take the rest... ;)

-k
 
Thanks for the responses. I have tried this same formula using the standard trimleft function instead of ltrim, but it did not run any better or worse. :|
I just now tried IdoMillet's idea, as I really feel this is the problem, but I have not found that switching to a sql expression has helped, either.
Here is how I tried to use the sql expression:
if
{?Pm-HISTORY.WOLINKKEY}>1
then
{HISTORY.WOLINKKEY} = {?Pm-HISTORY.WOLINKKEY}
else
{%TrimWONO}= {?Pm-@InputToAllCaps}

TrimWONO has the following inside it:
ltrim(HISTORY."WONO")

Am I using the sql expression idea incorrectly?
THanks for all the help.

 
Try:
-------------------------------------------
({?Pm-HISTORY.WOLINKKEY}>1 AND
{HISTORY.WOLINKKEY} = {?Pm-HISTORY.WOLINKKEY})
OR
({?Pm-HISTORY.WOLINKKEY}<=1 AND
{%TrimWONO}= {?Pm-@InputToAllCaps})
-------------------------------------------

Cheers,
- Ido CUT, Visual CUT, and DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
I have tried the suggested formula, which looks a lot nicer, but doesn't seem to run any faster. It does return the correct records after 3 minutes or so, but I'm back to my original problem of pretty darn slow processing.

For comparison, if I just use the first part of the select, ({?Pm-HISTORY.WOLINKKEY}>1 AND
{HISTORY.WOLINKKEY} = {?Pm-HISTORY.WOLINKKEY})
it takes about 5 seconds to run. (Tried this alternately with the 2nd part of the 'or' by itself, and it runs very quick, too). It just seems so unfair!

Also, I realized I don't need the lefttrim/allcaps functions afterall, and am still experience sloooooow processing.

Should I be doing this in a stored procedure, or is there still hope within crystal...? I kind of wondered if splitting this into 2 subreports would help, but I think it would not easily work around the problem.
Thanks for all the help!

 
There's hope in Crystal, but you should post what is being generated in the SQL (database->Show SQL Query, and the current record selection formula in it's entirety.

I can almost always get the SQL to pass, but then again, if your database isn't properly indexed, there's little help.

Stored Procedures are always faster, though the difference is dependent upon the number of rows, indexes, statistics, etc.

-k
 
If I run this with a work order whose wolinkkey = 1,
I get:

SELECT
HISTORY.&quot;ADDDTTM&quot;, HISTORY.&quot;HISTKEY&quot;, HISTORY.&quot;WOLINKKEY&quot;, HISTORY.&quot;WONO&quot;,
COSTXTRA.&quot;COSTKEY&quot;, COSTXTRA.&quot;HISTKEY&quot;, COSTXTRA.&quot;TOTCOST&quot;,
COSTLABR.&quot;COSTKEY&quot;, COSTLABR.&quot;HISTKEY&quot;, COSTLABR.&quot;TOTCOST&quot;,
COSTVEH.&quot;COSTKEY&quot;, COSTVEH.&quot;HISTKEY&quot;, COSTVEH.&quot;TOTCOST&quot;,
COSTPART.&quot;COSTKEY&quot;, COSTPART.&quot;HISTKEY&quot;, COSTPART.&quot;TOTCOST&quot;,
COSTTOOL.&quot;COSTKEY&quot;, COSTTOOL.&quot;HISTKEY&quot;, COSTTOOL.&quot;TOTCOST&quot;,
COSTEQ.&quot;COSTKEY&quot;, COSTEQ.&quot;HISTKEY&quot;, COSTEQ.&quot;TOTCOST&quot;,
ltrim(HISTORY.&quot;WONO&quot;)
FROM
&quot;IMSV7&quot;.&quot;HISTORY&quot; HISTORY,
&quot;IMSV7&quot;.&quot;COSTXTRA&quot; COSTXTRA,
&quot;IMSV7&quot;.&quot;COSTLABR&quot; COSTLABR,
&quot;IMSV7&quot;.&quot;COSTVEH&quot; COSTVEH,
&quot;IMSV7&quot;.&quot;COSTPART&quot; COSTPART,
&quot;IMSV7&quot;.&quot;COSTTOOL&quot; COSTTOOL,
&quot;IMSV7&quot;.&quot;COSTEQ&quot; COSTEQ
WHERE
HISTORY.&quot;HISTKEY&quot; = COSTXTRA.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTLABR.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTVEH.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTPART.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTTOOL.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTEQ.&quot;HISTKEY&quot; (+) AND
(HISTORY.&quot;WOLINKKEY&quot; = 1 OR
HISTORY.&quot;WONO&quot; = ' 1015')
ORDER BY
HISTORY.&quot;WOLINKKEY&quot; ASC,
HISTORY.&quot;WONO&quot; ASC,
HISTORY.&quot;HISTKEY&quot; ASC

If I run it with a wolinkkey > 1, I get:

SELECT
HISTORY.&quot;ADDDTTM&quot;, HISTORY.&quot;HISTKEY&quot;, HISTORY.&quot;WOLINKKEY&quot;, HISTORY.&quot;WONO&quot;,
COSTXTRA.&quot;COSTKEY&quot;, COSTXTRA.&quot;HISTKEY&quot;, COSTXTRA.&quot;TOTCOST&quot;,
COSTLABR.&quot;COSTKEY&quot;, COSTLABR.&quot;HISTKEY&quot;, COSTLABR.&quot;TOTCOST&quot;,
COSTVEH.&quot;COSTKEY&quot;, COSTVEH.&quot;HISTKEY&quot;, COSTVEH.&quot;TOTCOST&quot;,
COSTPART.&quot;COSTKEY&quot;, COSTPART.&quot;HISTKEY&quot;, COSTPART.&quot;TOTCOST&quot;,
COSTTOOL.&quot;COSTKEY&quot;, COSTTOOL.&quot;HISTKEY&quot;, COSTTOOL.&quot;TOTCOST&quot;,
COSTEQ.&quot;COSTKEY&quot;, COSTEQ.&quot;HISTKEY&quot;, COSTEQ.&quot;TOTCOST&quot;,
ltrim(HISTORY.&quot;WONO&quot;)
FROM
&quot;IMSV7&quot;.&quot;HISTORY&quot; HISTORY,
&quot;IMSV7&quot;.&quot;COSTXTRA&quot; COSTXTRA,
&quot;IMSV7&quot;.&quot;COSTLABR&quot; COSTLABR,
&quot;IMSV7&quot;.&quot;COSTVEH&quot; COSTVEH,
&quot;IMSV7&quot;.&quot;COSTPART&quot; COSTPART,
&quot;IMSV7&quot;.&quot;COSTTOOL&quot; COSTTOOL,
&quot;IMSV7&quot;.&quot;COSTEQ&quot; COSTEQ
WHERE
HISTORY.&quot;HISTKEY&quot; = COSTXTRA.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTLABR.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTVEH.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTPART.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTTOOL.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTEQ.&quot;HISTKEY&quot; (+) AND
(HISTORY.&quot;WOLINKKEY&quot; = 6205 OR
HISTORY.&quot;WONO&quot; = 'FY2003F')
ORDER BY
HISTORY.&quot;WOLINKKEY&quot; ASC,
HISTORY.&quot;WONO&quot; ASC,
HISTORY.&quot;HISTKEY&quot; ASC

I just noticed also, that the 2nd run through did go pretty quickly - where the wolinkkey = 6205.
To more fully describe the data, the work orders with wolinkkey= 1, are not linked, and those with wolinkkey >1 are linked, via the wolinkkey. So, if the wolinkkey is 1, I want to only pull based on wono, but if >1, I want to pull any wono's with matching wolinkkeys. I guess maybe there are just a TON of wolinkkeys = 1...?
Please let me know if you can make some sense of this! Thanks.
 
Note that the first pass had:

HISTORY.&quot;WONO&quot; = ' 1015'

where the column wasn't trimmed, which may prove incorrect.

Also, you still haven't shared your record selection formula, just fragments of it, please post it.

-k
 
oops - here it is:
({?Pm-HISTORY.WOLINKKEY}>1 and
{HISTORY.WOLINKKEY}= {?Pm-HISTORY.WOLINKKEY})
or
({?Pm-HISTORY.WOLINKKEY}=1 and
{HISTORY.WONO}={?Pm-HISTORY.WONO})
 
also, the auto generated numbers-only work order numbers do have spaces in front (arg!) - that is why I mistakenly thought I needed the ltrim/trimleft function in the beginning. But then I realized I don't have to match to the user's input, since I get the actual wono out of the database during the main report's select statement. So I'm just directly comparing the wono from the main report with what i want to see in the subreport. Hope this is correct assumption.
 
I have looked through your Selection formula 5 times.. and can't see how you get the SQL that you do if that is indeed your selection formula...

Either I am going batty (which is entirely possible) or you have changed your selection formula since you listed the SQL statements..

Lisa
 
I really have not (unless I blacked out or something ;). I will say that the two sql statments I show are in reverse order of how I have the record selection formula.

Here, I have just run it again:

SELECT
HISTORY.&quot;ADDDTTM&quot;, HISTORY.&quot;HISTKEY&quot;, HISTORY.&quot;WOLINKKEY&quot;, HISTORY.&quot;WONO&quot;,
COSTXTRA.&quot;COSTKEY&quot;, COSTXTRA.&quot;HISTKEY&quot;, COSTXTRA.&quot;TOTCOST&quot;,
COSTLABR.&quot;COSTKEY&quot;, COSTLABR.&quot;HISTKEY&quot;, COSTLABR.&quot;TOTCOST&quot;,
COSTVEH.&quot;COSTKEY&quot;, COSTVEH.&quot;HISTKEY&quot;, COSTVEH.&quot;TOTCOST&quot;,
COSTPART.&quot;COSTKEY&quot;, COSTPART.&quot;HISTKEY&quot;, COSTPART.&quot;TOTCOST&quot;,
COSTTOOL.&quot;COSTKEY&quot;, COSTTOOL.&quot;HISTKEY&quot;, COSTTOOL.&quot;TOTCOST&quot;,
COSTEQ.&quot;COSTKEY&quot;, COSTEQ.&quot;HISTKEY&quot;, COSTEQ.&quot;TOTCOST&quot;,
ltrim(HISTORY.&quot;WONO&quot;)
FROM
&quot;IMSV7&quot;.&quot;HISTORY&quot; HISTORY,
&quot;IMSV7&quot;.&quot;COSTXTRA&quot; COSTXTRA,
&quot;IMSV7&quot;.&quot;COSTLABR&quot; COSTLABR,
&quot;IMSV7&quot;.&quot;COSTVEH&quot; COSTVEH,
&quot;IMSV7&quot;.&quot;COSTPART&quot; COSTPART,
&quot;IMSV7&quot;.&quot;COSTTOOL&quot; COSTTOOL,
&quot;IMSV7&quot;.&quot;COSTEQ&quot; COSTEQ
WHERE
HISTORY.&quot;HISTKEY&quot; = COSTXTRA.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTLABR.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTVEH.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTPART.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTTOOL.&quot;HISTKEY&quot; (+) AND
HISTORY.&quot;HISTKEY&quot; = COSTEQ.&quot;HISTKEY&quot; (+) AND
(HISTORY.&quot;WOLINKKEY&quot; = 6205 OR
HISTORY.&quot;WONO&quot; = 'FY2003F')
ORDER BY
HISTORY.&quot;WOLINKKEY&quot; ASC,
HISTORY.&quot;WONO&quot; ASC,
HISTORY.&quot;HISTKEY&quot; ASC

and again, what is in the select expert:
({?Pm-HISTORY.WOLINKKEY}>1 and
{HISTORY.WOLINKKEY}= {?Pm-HISTORY.WOLINKKEY})
or
({?Pm-HISTORY.WOLINKKEY}=1 and
{HISTORY.WONO}={?Pm-HISTORY.WONO})
 
It appears that everything is being passed, so the performance appears to be on the database side, the server should be returning a subset of data, which is the point of the record selection criteria.

I'd guess that your performance issue is with indexes/statistics.

Copy the above SQL and execute it in a query tool on your database to test this.

-k
 
It appears that everything is being passed to the server, so the performance concern appears to be on the database side, the server is returning a subset of data, which is the point of the record selection criteria.

I'd guess that your performance issue is with indexes/statistics.

Copy the above SQL and execute it in a query tool on your database to test this.

-k
 
Thanks you so much for taking the time to review this with me. I will check this out from the db side with one of our dbas.

 
Well, I was able to wrangle access to a query design tool, and while messing around with the sql I realized how incorrectly crystal is translating what I have put in the select expert. Like lyanch, I am really scratching my head about how crystal has taken the select expert statement and basically created an incorrect hybrid statement.
It should really have an 'AND' where it has placed the 'OR', and I don't know why I didn't notice before. It's just plain wrong.

I have checked and double-checked all my parentheses, etc. (With the 'AND' in the query design tool, it runs great. With the 'OR', it selects 280000 records, of course.)
I feel like I am beating a dead horse, but I guess I am just curious to have this confirmed. Is my horse dead, or does this ring any bells for anyone, now that the problem is a bit more clear?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top