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 > Other > Sum one row from joined table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-04-10, 08:11
tosa tosa is offline
Registered User
 
Join Date: Feb 2010
Posts: 35
Sum one row from joined table

Hi!

I have this SQL so far:
SELECT (SUM(TotalQty) - SUM(ProdQty)) AS 'openProd' FROM PUB.ProdOper INNER JOIN PUB.ProdOrder ON (ProdOper.PrOrderNum=ProdOrder.PrOrderNum) WHERE ProdOrder.ItemCode = '1234' AND ProdOrder.Completed = 0

The problem is I want to sum only the last row from table ProdOper (where ProdOper.PosNum is the highest). How can I do that?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 11-05-10, 02:59
tosa tosa is offline
Registered User
 
Join Date: Feb 2010
Posts: 35
I thought I'd explain it a little better:

There are tables ProdOrder and ProdOper.

ProdOrder
=======
OrderNum
1001
1002

ProdOper
=======
OrderNum, PosNum
1001, 500
1001, 510
1002, 600
1002, 610

How do I select, from ProdOper, only the rows with the highest PosNum? It's a Progress database.

Last edited by tosa; 11-05-10 at 03:04.
Reply With Quote
  #3 (permalink)  
Old 11-05-10, 13:39
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
then add in a subselect that you a.Posnum = (select max(Posnum) from table b where a.Ordernum = b.Ordernum)
Dave
Reply With Quote
  #4 (permalink)  
Old 11-08-10, 03:17
tosa tosa is offline
Registered User
 
Join Date: Feb 2010
Posts: 35
Thanks for your reply.

I didn't get it to work though.

I don't get it, this query

"SELECT * FROM PUB.ProdOper AS l INNER JOIN PUB.ProdOrder AS o ON l.PrOrderNum=o.PrOrderNum WHERE o.ItemCode = '2073' AND l.Completed = 0 AND o.Completed = 0 AND l.OperPosNum = (SELECT MAX(OperPosNum) FROM PUB.ProdOper)"

returns no rows at all, but this

"SELECT * FROM PUB.ProdOper AS l INNER JOIN PUB.ProdOrder AS o ON l.PrOrderNum=o.PrOrderNum WHERE o.ItemCode = '2073' AND l.Completed = 0 AND o.Completed = 0 AND l.OperPosNum = '515'"

returns two rows.

Last edited by tosa; 11-08-10 at 03:47.
Reply With Quote
  #5 (permalink)  
Old 11-08-10, 04:58
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,540
Code:
SELECT something
     , anything
     , just_not_the_dreaded_evil_select_star
  FROM PUB.ProdOrder AS o
INNER 
  JOIN ( SELECT PrOrderNum
              , MAX(OperPosNum) AS last_posnum
           FROM PUB.ProdOper
          WHERE Completed = 0
         GROUP
             BY PrOrderNum ) AS m
    ON m.PrOrderNum = o.PrOrderNum 
INNER
  JOIN PUB.ProdOper AS l 
    ON l.PrOrderNum = o.PrOrderNum 
   AND l.OperPosNum = m.last_posnum
 WHERE o.ItemCode = '2073' 
   AND o.Completed = 0
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 11-08-10, 08:44
tosa tosa is offline
Registered User
 
Join Date: Feb 2010
Posts: 35
Thanks Rudy! This looks quite close.

I'd like to select the rows from table ProdOper with highest OperPosNum, but I'm not sure how to modify your query to do it...
Reply With Quote
  #7 (permalink)  
Old 11-08-10, 08:59
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,540
what makes you think my query needs to be modified?

did you test it?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 11-08-10, 09:19
tosa tosa is offline
Registered User
 
Join Date: Feb 2010
Posts: 35
I'm terribly sorry!

Eeh...I used the "evil" star again and didn't notice the stuff I actually wanted was added at the end.

Thanks again! You're a pro!
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