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 > Data Access, Manipulation & Batch Languages > ANSI SQL > how to join the table?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-14-11, 04:35
manhjk manhjk is offline
Registered User
 
Join Date: May 2011
Posts: 13
how to join the table?

I have two table as follow

TableA
PID, Amount1
1, 10
1, 20
2, 10
2, 10
3, 30

TableB
PID, Amount2
1, 20
1, 50
2, 30
2, 20

SELECT TableA.PID, Sum(TableA.Amount1) AS Amount1OfSum, Sum(TableB.Amount2) AS Amount2OfSum
FROM TableA left JOIN TableB ON TableA.PID = TableB.PID
GROUP BY TableA.PID;

The output is
PID, Amount1, Amount2
1, 90, 200
2, 20, 40
3, 30,


What I want the output is
PID, Amount1, Amount2
1, 30, 70
2, 20, 50
3, 30,

Thank.
Reply With Quote
  #2 (permalink)  
Old 06-14-11, 05:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you are joining and then grouping

you should be grouping and then joining --
Code:
SELECT one.pid
     , one.Amount1OfSum
     , two.Amount2OfSum
  FROM ( SELECT pid
              , SUM(Amount1) AS Amount1OfSum
           FROM TableA 
         GROUP
             BY pid ) AS one
LEFT OUTER
  JOIN ( SELECT pid
              , SUM(Amount2) AS Amount2OfSum
           FROM TableB 
         GROUP
             BY pid ) AS two
    ON two.pid = one.pid
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-14-11, 05:22
manhjk manhjk is offline
Registered User
 
Join Date: May 2011
Posts: 13
hi r937,

Thank for your's reply.

I will try it tmr.
Reply With Quote
  #4 (permalink)  
Old 06-14-11, 05:29
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by manhjk View Post
I will try it tmr.
What's a tmr?
Reply With Quote
  #5 (permalink)  
Old 06-14-11, 06:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by shammat View Post
What's a tmr?
the day after tdy

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 06-14-11, 21:50
manhjk manhjk is offline
Registered User
 
Join Date: May 2011
Posts: 13
Hi r937,

I got it, thank.
Reply With Quote
  #7 (permalink)  
Old 07-28-11, 00:38
amankumar amankumar is offline
Registered User
 
Join Date: Jul 2011
Posts: 1
join the table

the first start make a database and then two file combine with each other.
and then join the table
Reply With Quote
  #8 (permalink)  
Old 08-07-11, 11:15
jason_bourne jason_bourne is offline
Registered User
 
Join Date: Aug 2011
Posts: 11
Sorry I am new to SQL
I executed the following query :


SELECT one.pid
, one.Amount1OfSum
, two.Amount2OfSum
FROM ( SELECT pid
, SUM(Amount1) AS Amount1OfSum
FROM TableA
GROUP
BY pid ) AS one
LEFT OUTER
JOIN ( SELECT pid
, SUM(Amount2) AS Amount2OfSum
FROM TableB
GROUP
BY pid ) AS two
ON two.pid = one.pid


and got the error ORA: 00933 sql command not properly ended....Cud you plz tell me where I went wrong ?
Reply With Quote
  #9 (permalink)  
Old 08-07-11, 14:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
jason, dude, this isn't your thread, perhaps you should re-post in the oracle forum
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 08-08-11, 06:24
jason_bourne jason_bourne is offline
Registered User
 
Join Date: Aug 2011
Posts: 11
Reply

Okay. I will repost the same query in the oracle forum
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