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 > MySQL > sum union tables into one

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-14-08, 04:19
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
sum union tables into one

hi to all,
is there a way on how to add the sum of each query results of each tables?

my code is:
Code:
select
coding_log.last_name,

format(time_to_sec(SUM(CASE WHEN coding_log.time_end <= '16:00' && coding_log.adjustment = 'meeting'
        THEN coding_log.adjustment_time ELSE 0 END))/3600,2)
        as 'Total Reg Meeting',

format(time_to_sec(SUM(CASE WHEN coding_log.time_end <= '16:00' && coding_log.adjustment = 'orientation'
        THEN coding_log.adjustment_time ELSE 0 END))/3600,2)
        as 'Total Reg Orientation'

from
coding_log

Group By
coding_log.Last_Name

union

select
key_entry_log.last_name,

time_to_sec(SUM(CASE WHEN key_entry_log.time_end <= '16:00' && key_entry_log.adjustment = 'meeting'
        THEN key_entry_log.adjustment_time ELSE 0 END))/3600
        as 'Total Reg Meeting',

format(time_to_sec(SUM(CASE WHEN key_entry_log.time_end <= '16:00' && key_entry_log.adjustment = 'orientation'
        THEN key_entry_log.adjustment_time ELSE 0 END))/3600,2)
        as 'Total Reg Orientation'

from
key_entry_log

Group By
key_entry_log.Last_Name

order by
last_name
please check
i want to add (coding_log.'Total Reg Meeting', coding_log.'Total Reg Orientation') to (key_entry_log.'Total Reg Meeting', key_entry_log.'Total Reg Orientation')
any advice, solutions?

thanks in advance....
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #2 (permalink)  
Old 06-14-08, 08:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
the query you have posted gives a result set consisting of 3 columns:

- last_name
- 'Total Reg Meeting'
- 'Total Reg Orientation'

remove the ORDER BY clause from your query, and then insert it here, as shown --
Code:
SELECT last_name
     , SUM(`Total Reg Meeting`) AS Sum_Reg_meeting
     , SUM(`Total Reg Orientation`) AS Sum_reg_Orientation
  FROM (
       your query goes here
       ) AS q
GROUP
    BY last_name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-16-08, 00:11
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
thanks for the reply...
it works, can i insert a inner join after the FROM?

Code:
SELECT last_name
     , SUM(`Total Reg Meeting`) AS Sum_Reg_meeting
     , SUM(`Total Reg Orientation`) AS Sum_reg_Orientation
  FROM (
       your query goes here
       ) AS q -- can i make inner join here? --
GROUP
    BY last_name
thanks!
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #4 (permalink)  
Old 06-16-08, 00:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
yes, you can
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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