473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Problem using/naming subquery

Hi,

I'm trying to write an SQL statement that provides a list of insurance/
product groups and then lists a total and percentage on the same line
e.g.

XX 5 4 80%
XY 10 5 50%

I can get the first 3 columns done ok but can't do the percentage.
The totals are calculated based on subqueries and I've named the
subqueries and would expect to be able to use the value returned in
the percentage calculation except I get the error "Column ONCOVER not
in specified tables."

Can anybody help please? I've attached the SQL below

Thanks

Steve

select mrnpty "Scheme", mrncru "User", mrnsts "Status", mrnpap
"Paperless",
(select count(*) from elinew.mrnquote b
where mrncrd = curdate()
and b.mrnsts = 'G'
and a.mrnpol = b.mrnpol
and a.mrnpty = b.mrnpty
and a.mrncru = b.mrncru
group by mrncru, mrnpty, mrnsts, mrnpap) AS Enquiry ,

(select count(*) OncoverTotal from elinew.mrnquote b
where mrncrd = curdate()
and b.mrnsts = 'O'
and a.mrnpol = b.mrnpol
and a.mrnpty = b.mrnpty
and a.mrncru = b.mrncru
group by mrncru, mrnpty, mrnsts, mrnpap) AS OnCover,

((OnCover / Enquiry)*100)

from elinew.mrnquote a, sysibm.sysdummy1
where mrncrd = curdate()

union

select 'Total', (select count(*) from elinew.mrnquote where mrncrd =
curdate() and mrnsts IN ('O', 'G')),
'Paperless', (select count(*) from elinew.mrnquote where mrncrd =
curdate() and mrnsts IN ('O', 'G') and mrnpap = 'Y'), ' ', ' ', ' '
from sysibm.sysdummy1
Sep 18 '08 #1
2 2304
1) You can use column names of subqueries in an expresion, like this:
SELECT ...
, (OnCover / Enquiry)*100
/* it would be better to use "OnCover * 100. / Enquiry" to keep the
digits in the fractional part. */
FROM (SELECT ...
, (SELECT ...
...
) AS Enquiry
, (SELECT ...
...
) AS OnCover
FROM ...
...
) Sub

2) It wouldn't need to join sysibm.sysdummy1 in the clause
"from elinew.mrnquote a, sysibm.sysdummy1".

3) You might receive incompatible data type error by your UNION.
If so, replace ' ' with CAST(NULL AS INTEGER) or CAST(NULL AS
DECIMAL).

4) I wonder that your subselect(s) including "group by" might return
multiple rows and you might get error.
Sep 18 '08 #2
I think that last half of your UNION can be replaced by this.
select 'Total'
, count(*)
, 'Paperless'
, count(CASE WHEN mrnpap = 'Y' THEN 0 END)
, CAST(NULL AS INTEGER)
, CAST(NULL AS INTEGER)
, CAST(NULL AS DECIMAL)
from elinew.mrnquote
where mrncrd = curdate()
and mrnsts IN ('O', 'G')
Sep 18 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: André Gasser | last post by:
hello newsgroup, I just discovered a weird effect in my php code. here is the flow of my code: 1. upload jepg file to server 2. create new (empty) jpeg file using imagecreatefromjpeg()...
4
by: bmiras | last post by:
I've got a problem using urllib2 to get a web page. I'm going through a proxy using user/password authentification and i'm trying to get a page asking for a HTTP authentification. And I'm using...
2
by: Stephen Last | last post by:
Hi all, I have a problem using FSO with a mapped drive. I have mapped x: on our Intranet server to a folder on one of our file servers. The folder contains user home directories. I want to be...
4
by: Derek Timothy | last post by:
Hi folks I have strange problem using cmd.exe from asp code I am trying to save the results of an FTP command into a text file. When I run this command from the command line of the web server it...
3
by: Jacky Zhu | last post by:
Hi all, I am having a problem trying to consume a webservice that is developed on ..Net. I can access it without any problem using a .net client, but when I use a java client (based on Axis...
4
by: Dani | last post by:
Hi everyone Description of the problem: Using a PreparedStatement to write down an integer (int) plus a timestamp for testing purposes. When read out again the integer looks very different. We...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
3
by: Franky | last post by:
Been having a problem using a treeview. Work great the first time the form containing it is entered but not the second time. Took a while to create a small sample that exhibits the problem, but...
3
by: presencia | last post by:
Hi all, I have a problem using gdb. I am using Eclipse-CDT as a frontend, but since this problem is obviously related to the gcc compiler or the gdb itself, I have reproduced it without Eclipse. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.