473,386 Members | 1,796 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,386 software developers and data experts.

Use of having with additional select

Hello guys, i have this query:

select c8.cerveh, sum(c8.monto1) monto1,
(select prima from arysauto a where a.cerveh=c8.cerveh)
priari,
(sum(c8.monto1)-(select prima from arysauto a
where a.cerveh=c8.cerveh)) dif
from clpf08 c8
where c8.ramo=31 and c8.poliza=6100265 and
c8.stcdcb=' ' and c8.cerveh in (select cerveh from arysauto)
and
exists (select * from clpf07 where ramo=31 and poliza=6100265
and
cerveh=c8.cerveh and actret<>'R')
group by c8.cerveh
That query prints the sum of a value and compares it with a
standalone
value in another table, and then outputs the difference between those
2 values.
Now i want the same query, but to only show the values that have a
difference in absolute value over 1 units. So i tried putting at the
end of the group by the following:
having (sum(c8.monto1) - (select prima from arysauto a where
a.cerveh = c8.cerveh) ) 0
But the query doesn't run with some error that i have another
function
inside a funtion. If i remove the select inside the having and just
put a number it runs, so i am guessing it is the additional select.
There must be a way to do this, but i am stuck. Can someone help?
Jun 27 '08 #1
4 1599
On Wed, 23 Apr 2008 12:10:34 -0700 (PDT), Seguros Catatumbo wrote:
>Hello guys, i have this query:

select c8.cerveh, sum(c8.monto1) monto1,
(select prima from arysauto a where a.cerveh=c8.cerveh)
priari,
(sum(c8.monto1)-(select prima from arysauto a
where a.cerveh=c8.cerveh)) dif
from clpf08 c8
where c8.ramo=31 and c8.poliza=6100265 and
c8.stcdcb=' ' and c8.cerveh in (select cerveh from arysauto)
and
exists (select * from clpf07 where ramo=31 and poliza=6100265
and
cerveh=c8.cerveh and actret<>'R')
group by c8.cerveh
That query prints the sum of a value and compares it with a
standalone
value in another table, and then outputs the difference between those
2 values.
Now i want the same query, but to only show the values that have a
difference in absolute value over 1 units. So i tried putting at the
end of the group by the following:
having (sum(c8.monto1) - (select prima from arysauto a where
a.cerveh = c8.cerveh) ) 0
But the query doesn't run with some error that i have another
function
inside a funtion. If i remove the select inside the having and just
put a number it runs, so i am guessing it is the additional select.
There must be a way to do this, but i am stuck. Can someone help?
Hi Seguros,

I'm not sure why you got that error - I would have expected it to work.
If you could post the table structure (CREATE TABLE statements) and some
sample data (INSERT statements), I could try to reproduce. However, I
think you might be better off rewriting your query to eliminate the
repetition of the subquery.

I can't test because I have no access to your tables and test data, but
try if the followiing does what you need:

SELECT c8.cerveh,
SUM(c8.monto1) AS monto1,
SUM(a.prima) AS priari,
SUM(c8.monto1) - SUM(a.prima) AS dif
FROM clpf08 AS c8
INNER JOIN arysauto AS a
ON a.cerveh = c8.cerveh
WHERE c8.ramo = 31
AND c8.poliza = 6100265
AND c8.stcdcb = ' '
AND EXISTS
(SELECT *
FROM clpf07 AS c7
WHERE c7.ramo = 31
AND c7.poliza = 6100265
AND c7.cerveh = c8.cerveh
AND c7.actret <'R')
GROUP BY c8.cerveh;
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Jun 27 '08 #2
I can't test because I have no access to your tables and test data, but
try if the followiing does what you need:
Table CLPF08 sample data and relevant field information:

RAMO POLIZA CERVEH CODCOB MONTO1
31 6100265 2 1 20.15
31 6100265 2 2 30.10
31 6100265 2 3 15.02

Table ARYSAUTO sample data and relevant field information:

CERVEH PRIMA
2 65.26

My query shows a 0.01 monetary units of difference, since
20.15+30.10+15.02 is not exactly 65.26

The idea is to add all of MONTO1 in CLPF08 and compare that sum with
the lone value in table ARYSAUTO.
My query works, but using the HAVING statement to show up only those
records that have certain difference (we only need big differences,
not measly cents) doesn't work.

Your query executed, but it showed bizarre amounts.

Thanks for your help, i hope that this information is enough to fix
this
Jun 27 '08 #3
On Wed, 23 Apr 2008 13:49:49 -0700 (PDT), Seguros Catatumbo wrote:
>
>I can't test because I have no access to your tables and test data, but
try if the followiing does what you need:

Table CLPF08 sample data and relevant field information:

RAMO POLIZA CERVEH CODCOB MONTO1
31 6100265 2 1 20.15
31 6100265 2 2 30.10
31 6100265 2 3 15.02

Table ARYSAUTO sample data and relevant field information:

CERVEH PRIMA
2 65.26

My query shows a 0.01 monetary units of difference, since
20.15+30.10+15.02 is not exactly 65.26
Hi Seguros,

I expect that this is a rounding issue. What data types are the various
columns?
>Thanks for your help, i hope that this information is enough to fix
this
No, it isn't. As I already stated in my previous reply, I need to know
the table structure ===as a CREATE TABLE statement <===. I failed to
include that you also need to include all the constraints, properties,
and indexes (you may omit irrelevant extra columns though).

I also need the sample data ===as INSERT statements <===. And I need
to know the expected results based on the sample data given.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Jun 27 '08 #4
I agree with Hugo that the information you provided is not sufficient to
give you a good answer. Here is just a guess based on your explanation and
expected results:

SELECT C.cerveh, C.monto1, A.prima
FROM Arysauto AS A
JOIN (SELECT ramo, cerveh, poliza, stcdcb,
SUM(monto1) AS monto1
FROM Clpf08
GROUP BY ramo, cerveh, poliza, stcdcb) AS C
ON A.cerveh = C.cerveh
WHERE C.ramo = 31
AND C.poliza = 6100265
AND C.stcdcb = ' '
AND EXISTS (SELECT *
FROM Clpf07 AS B
WHERE B.ramo = C.ramo
AND B.poliza = C.poliza
AND B.cerveh = C.cerveh
AND B.actret <'R')
AND C.monto1 - A.prima 0;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #5

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

Similar topics

6
by: Marek Kotowski | last post by:
I need to put current date into additional column title. I mean something like this SELECT Name_1, ' ' AS CURDATE() FROM Table_1 ... or, better SELECT Name_1, ' ' AS CONCAT('My column ',...
6
by: Sebi | last post by:
Hello all, I'm thinking about overwriting the ListItem, so it can contain x additional values (not only one). Has anybody ever tried this? Has someone got an example (C#)? Can DropDownList...
4
by: Supra | last post by:
I am doing irc chat similar to mirc chat if i connected to irc.webamster.com i had no problem. but if connected to eu.undernet.org. i got error....somthing bug me! O:-) 'February 2004 'An...
3
by: Rich | last post by:
Hello, I have been using OneClick deployment to deploy apps at my workplace. Works great! But now I have to deploy additional files with the app, like chms, rtfs. How do I publish the...
2
by: John | last post by:
Hi I have a query as a rowsource to a list. Is it possible to add a single additional value to the row source to appear in the list in addition to what is coming from the query? Such as by...
3
by: dtvuser | last post by:
Hi, I'm new to PHP and seem to be having soom problems, I'm getting confused with all the different styles of script writing. I've created a PHP script to submit details to my email but the...
4
by: tweeterbot | last post by:
Hi, I am a chemical engineer trying to design a database and I am running into some trouble. My database is going to be 'processing' raw data to get the figures we need to prepare the monthly...
1
by: =?Utf-8?B?RGVicmEgTGFzc21hbg==?= | last post by:
I have installed Office 2007 on my machine and when I open Excel 2007 I still have 256 columns and 65,536 rows. I know in the 2007 version that you have the ability to have more than this. If I add...
2
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.