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

Using sum in nested select

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.

Expand|Select|Wrap|Line Numbers
  1. select a, b, c, d, e,
  2.   ( select
  3.       name
  4.     from
  5.       table1
  6.     where
  7.       minv <= (select sum(num) from table2 where a != b)
  8.     and
  9.       maxv >= (select sum(num) from table2 where a != b)  )
  10. from
  11.   table2, table3, table4
  12. where
  13.   ............... a = b .........
  14.  
Is there some way to do this efficiently?
Jan 24 '08 #1
2 4629
docdiesel
297 Expert 100+
Hi,

first, I added some code tags to your sql statement for better readability. Second, you could store the sum in another table:

Expand|Select|Wrap|Line Numbers
  1. delete from mytables.storedsum;
  2. insert into mytables.storedsum
  3.   select sum(col1)
  4.   from mytables.table2
  5.   where ... ;
  6.  
  7. select a, b,
  8.   ( select  name
  9.     from  table2
  10.     where a<=(select mysum from mytables.storedsum)
  11.     and   b<=(select mysum from mytables.storedsum)
  12.   )
  13. 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
Jan 25 '08 #2
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.
Jan 25 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
2
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...
3
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...
7
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...
9
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...
6
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...
4
by: Efi Merdler | last post by:
Hi, I have an xml file with the following structure: <Dictionary> .... <Nested> <Entry> .... </Entry> <Entry> ....
3
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...
1
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
0
debasisdas
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...
0
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,...
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:
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...
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
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,...

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.