Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I create a minimum SQL expression?

Posted on 2011-10-18
11
Medium Priority
?
362 Views
Last Modified: 2012-05-12
The following query works perfectly in TOAD for Oracle:
(
select  a.spr_code, min( b.paw_rank)
from sipr.cam_paw b, sipr.cam_saw a
where a.prg_code = b.prg_code
and a.AWD_CODE = b.AWD_CODE
and a.AWS_CODE in ('A','CONF')
group by a.spr_code
);

I am trying to replicate this in Crystal :
(
select  a.cam_saw.spr_code, min(b.CAM_PAW.PAW_RANK)
from sipr.cam_paw b, sipr.cam_saw a
where a.cam_saw.prg_code = b.cam_paw.prg_code
and a.CAM_SAW.AWS_CODE = b.CAM_PAW.AWD_CODE
and a.CAM_SAW.AWS_CODE in ('A','CONF')
group by a.cam_saw.spr_code
);
however, the following error is returned.

Error in compiling SQL Expression :
Failed to retrieve data from the database.
Details: ADO Error Code: 0x80040e14
Source: Microsoft OLE DB Provider for Oracle
Description: ORA-00911: invalid character

Native Error:911 [Database Vendor Code:911].

If I don't include the reference to the spr_code in the select statement, the query runs, but returns incorrect values

Are you able to advise?

Many thanks,
Katy
0
Comment
Question by:Katy_Holmes
  • 4
  • 2
  • 2
  • +2
11 Comments
 

Author Comment

by:Katy_Holmes
ID: 36985498
Sorry, I provided the wrong error message. It should read:

Error in compiling SQL Expression :
Failed to retrieve data from the database.
Details: ADO Error Code: 0x80040e14
Source: Microsoft OLE DB Provider for Oracle
Description: ORA-00913: too many values

Native Error:913 [Database Vendor Code:913].

0
 
LVL 101

Expert Comment

by:mlmcc
ID: 36985975
Why are you using a.cam_saw.prg_code instead of a.prg_code
(
select  a.spr_code, min(b.PAW_RANK)
from sipr.cam_paw b, sipr.cam_saw a
where a.prg_code = b.prg_code
and a.AWS_CODE = b.AWD_CODE
and a.AWS_CODE in ('A','CONF')
group by a.spr_code
);

mlmcc
0
 
LVL 77

Expert Comment

by:peter57r
ID: 36986192
Doesn't a SQL Expression have to return a single value?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Katy_Holmes
ID: 36986333
So are you saying that it is not possible to use the SQL expression in this way? The suggestion by MLMCC still returns the same error of too many values.
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 36986673
That is correct.  I thought (missed the SQL expression) you were using this as the command for the report.

A SQL expression is a formula and thus can only have 1 value.

Where are you using the expression?

This should work to get the A value

(
select  min(b.CAM_PAW.PAW_RANK)
from sipr.cam_paw b, sipr.cam_saw a
where a.cam_saw.prg_code = b.cam_paw.prg_code
and a.CAM_SAW.AWS_CODE = b.CAM_PAW.AWD_CODE
and a.CAM_SAW.AWS_CODE ='A'
);

mlmcc
0
 
LVL 77

Expert Comment

by:peter57r
ID: 36986674
A SQL Expression field is just an alternative to a Crystal Formula field - it produces a result which is a single value.

If you are producing a value for each record being fetched from the database then you don't have to include the 'linking' field, just the result.  But I can't tell from your Q whether this is possible in your situation.
0
 

Author Comment

by:Katy_Holmes
ID: 36986769
The value that I actually want is the minimum paw_rank for a particular student. If I don't include the reference to the student (SPR_CODE) in the expression I get the minimum paw_rank for the programme (PRG_CODE) which is not necessarily the same as the minimum rank for the individual student. The cam_paw table does not include a reference to the student so I can't include it in the where clause.
The PAW table would be:
BA-HONS     Rank 1     Programme Code 1
DIP-HE          Rank 2     Programme Code 1
CERT-HE      Rank 3     Programme Code 1
There are only these 3 fields in this table.

An individual student may thus have two records in the SAW table:
Student number 11111111  AWD CODE CERT-HE   PROGRAMME CODE 1
Student number 11111111  AWD CODE DIP-HE  PROGRAMME CODE 1

I only want to return one record for the student and I want the one with the lowest rank - ie the DIP-HE record which would have a rank of 2.
If I don't include the reference to the student it finds the lowest rank for the programme which would be rank 1 BA HONS which this student has not achieved.

Does this make sense? Is there another way to achieve this result?
0
 
LVL 35

Expert Comment

by:James0628
ID: 37005458
As mlmcc mentioned earlier, your CR code looks strange.  Your FROM creates aliases b and a for sipr.cam_paw and sipr.cam_saw, but then you use things like a.cam_saw.spr_code and b.CAM_PAW.PAW_RANK, which would theoretically equate to sipr.cam_saw.cam_saw.spr_code and sipr.cam_paw.CAM_PAW.PAW_RANK (with the table names twice), which doesn't look right.  But I haven't used Oracle, so maybe there's some Oracle thing at work here.  Putting that aside for the moment ...

 What you need to do is try to "link" the SQL Expression to the main data in your report.  I assume that you have spr_code or some other student ID field in your report.  Using your original CR code, try this:

select min(b.CAM_PAW.PAW_RANK)
from sipr.cam_paw b, sipr.cam_saw a
where
<student field from report> = a.cam_saw.spr_code
and a.cam_saw.prg_code = b.cam_paw.prg_code
and a.CAM_SAW.AWS_CODE = b.CAM_PAW.AWD_CODE
and a.CAM_SAW.AWS_CODE in ('A','CONF')

 Or, without the "extra" table names:

select min(b.PAW_RANK)
from sipr.cam_paw b, sipr.cam_saw a
where
<student field from report> = a.spr_code
and a.prg_code = b.prg_code
and a.AWS_CODE = b.AWD_CODE
and a.AWS_CODE in ('A','CONF')


 Where I have <student field from main report>, you would go to the CR Field Explorer, find that field and double-click on it to add it to the SQL Expression.  I assume that you could just type it in, but if you don't get it exactly right, CR won't like it.  The safest thing is to just let CR enter it for you.  And it needs to be first, before the field in the expression that you're comparing it with.  For some reason, using something like a.spr_code = <field from report> doesn't seem to work in a SQL Expression.

 James
0
 
LVL 26

Accepted Solution

by:
Kurt Reinhardt earned 2000 total points
ID: 37037172
FYI, you cannot correlate data from the report in a full SELECT statement SQL Expression against Oracle.  You can with DB2, SQL Server, etc..., but not Oracle.

As such, there's no real way to use a SQL Expression in this manner for your report.  You'd need to either use SQL for the entire report's recordset (a view, command or stored proc) or use a subreport to get the min value you're looking for. Please note, depending where you place the subreport, you may run into performance issues.
0
 

Author Closing Comment

by:Katy_Holmes
ID: 37037289
That is the conclusion that I came to. I ended up writing a couple of formulas in the report and doing the sorting of the records that I wanted in Crystal rather than on the database prior to bringing the required records into the report.
0
 
LVL 26

Expert Comment

by:Kurt Reinhardt
ID: 37038130
Sorry to have been the bearer of bad news.  As far as I know, it's an Oracle problem, not necessarily a Crystal Reports problem.  There are generally more idiosyncrasies when reporting against Oracle (especially in regards to the type of connection to Oracle) than other databases.  As a consultant, it's frustrating when I need to do something I know I can against another DB (and have, many times), but can't against the current DB.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question