Hi,
I'm trying to use use a sum value from table2 in my nested select, but I can't seem to figure a way to not have to calculate it twice. - select a, b, c, d, e,
-
( select
-
name
-
from
-
table1
-
where
-
minv <= (select sum(num) from table2 where a != b)
-
and
-
maxv >= (select sum(num) from table2 where a != b) )
-
from
-
table2, table3, table4
-
where
-
............... a = b .........
-
Is there some way to do this efficiently?
2 4629
Hi,
first, I added some code tags to your sql statement for better readability. Second, you could store the sum in another table: - delete from mytables.storedsum;
-
insert into mytables.storedsum
-
select sum(col1)
-
from mytables.table2
-
where ... ;
-
-
select a, b,
-
( select name
-
from table2
-
where a<=(select mysum from mytables.storedsum)
-
and b<=(select mysum from mytables.storedsum)
-
)
-
from ...
This way you could give DB2 a short query. To use a variable or something similar instead, I see no way. Writing a stored procedure could give you more possibilities, on the other hand.
Regards,
Bernd
I wish it was as simple as a single sum value.
If I'm properly interpreting your approach, you're suggesting that I use a temp table to store the calculations and pull each sum from that table instead. I hope that'll reduce my timings, because table2 is over a million records and the temp table is going to have to store a few hundred sums.
Thanks Bernd. I'll give it a try.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dw |
last post by:
Hello all. We're doing a site with teams and their members. We've got a page
where we need to display people according to who belongs to a which team.
I've heard that nested loops are bad, but...
|
by: Forgone Conclusion |
last post by:
Hi,
I have a View that groups and sums up totals.
This View is then nested within in another View and used (it needs to
be done like this). What i need to do is to be able to vary the
records...
|
by: WGW |
last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost!
everything I need. Maybe not efficiently, but usefully. However, I have
a problem -- a complex query problem...
I can...
|
by: Anthony Robinson |
last post by:
Have been encountering an odd issue. Every now and again, certain
packages of stored procedures just become invalid. I'm aware that
dropping or altering an underlying table would render a package...
|
by: Colin McGuire |
last post by:
Hi, I have an report in Microsoft Access and it displays everything in
the table. One column called "DECISION" in the table has either 1,2,
or 3 in it. On my report it displays 1, 2, or 3. I want...
|
by: duke442games |
last post by:
Hi Gang
I am working on fixing a corrupted database (A yearly proc did not handel the conversion from year to year and now I have to fix the resulting lines in the DB). Most of my fixes I have...
|
by: Efi Merdler |
last post by:
Hi,
I have an xml file with the following structure:
<Dictionary>
....
<Nested>
<Entry>
....
</Entry>
<Entry>
....
|
by: Frank Swarbrick |
last post by:
I was just messing around trying to learn things and attempted the
following:
select brch_nbr
, sum(case when post_flag != 'P' then amount else 0 end) as sum_amount
from film.film_transactions...
|
by: nanoman |
last post by:
Hello,
I've got a Nested Set structure in MySQL 4 here with
- id
- lft
- rgt
- parent_id
- root_id
I wrote some test scripts and i discovered that the Nested Set (the
|
by: debasisdas |
last post by:
Using Subqueries
==================
The sub query is often referred to as a nested SELECT, Sub - SELECT, or inner SELECT statement.
The sub query executes once before the main query.
The...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |