If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > I need select: total sum from two tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-06-03, 09:18
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
I need select: total sum from two tables

Hi,

I have two tables with I have to make summary select statement.

Table1:
Sales
1
2

Table2:
Sales
3
4

I would like to write one SQL withc would return the sum from bouth tables that is: 10.

How to write such SQL?

Thanks,
Grofaty
Reply With Quote
  #2 (permalink)  
Old 11-06-03, 09:29
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Grofaty,

How about something like:

with temp1 (sales) as
(select sales from table1 union all select sales from table2)
select sum(sales) from temp1

Andy
Reply With Quote
  #3 (permalink)  
Old 11-06-03, 11:20
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Or

with temp1(sumsales) as
(
select sum(sales) from tabl1
union all
select sum(sales) from table2
)
select sumsales from temp1

I think this will make a difference if the table is large ... Probably it may not require to create a large temp table ...

(This is just my thought ... Untested one)

Cheers
Sathyaram



Quote:
Originally posted by ARWinner
Grofaty,

How about something like:

with temp1 (sales) as
(select sales from table1 union all select sales from table2)
select sum(sales) from temp1

Andy
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #4 (permalink)  
Old 11-06-03, 11:25
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You will still need to sum the sums:

with temp1(sumsales) as
(
select sum(sales) from tabl1
union all
select sum(sales) from table2
)
select sum(sumsales) from temp1

Andy


Quote:
Originally posted by sathyaram_s
Or

with temp1(sumsales) as
(
select sum(sales) from tabl1
union all
select sum(sales) from table2
)
select sumsales from temp1

I think this will make a difference if the table is large ... Probably it may not require to create a large temp table ...

(This is just my thought ... Untested one)

Cheers
Sathyaram
Reply With Quote
  #5 (permalink)  
Old 11-06-03, 12:20
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Sorry, missed it ..

Thansk for pointing out

Sathyaram

Quote:
Originally posted by ARWinner
You will still need to sum the sums:

with temp1(sumsales) as
(
select sum(sales) from tabl1
union all
select sum(sales) from table2
)
select sum(sumsales) from temp1

Andy
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 11-10-03, 08:15
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,

Thanks to you all.
I have one more sample. When I wrote my post I was just forgeting to write "as whatever" as temporaly name.

Sample - this works
select sum(Sales) from
(select Sales from table1 union all select Sales from table2)
as whatever;

Sample - this does't works
select sum(Sales) from
(select Sales from table1 union all select Sales from table2)
;

Thanks,
Grofaty
Reply With Quote
  #7 (permalink)  
Old 11-10-03, 08:23
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,

I have also created access plan for all 3 SQL variants (ARWinner, sathyaram_s, grofaty).

The access plan was the same for all 3 SQL variants! Total cost for all variants were 50.1767 so there is no difference in performances.

I think that DB2 optimizer rewrites SQL...

Grofaty
Reply With Quote
  #8 (permalink)  
Old 11-10-03, 09:34
sundialsvcs sundialsvcs is offline
Registered User
 
Join Date: Oct 2003
Posts: 706
Quote:

The access plan was the same for all 3 SQL variants! Total cost for all variants were 50.1767 so there is no difference in performances.

I think that DB2 optimizer rewrites SQL...
That is precisely what the optimizer does. The SQL command is a description of what the result should be, and the optimizer's job is to decide .. on the fly, as it were .. how to get it.
__________________
ChimneySweep(R): fast, automatic
table repair at a click of the
mouse! http://www.sundialservices.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On