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 > Grouping, mins, and sums???

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-08-04, 04:29
steve_o steve_o is offline
Registered User
 
Join Date: Apr 2004
Posts: 43
Grouping, mins, and sums???

I have a table that looks like this:
artno | row | abt | pp
------+-----+-----+-----
507 | 2 | C60 | 1.23
507 | 4 | E45 | 0.59
509 | 1 | F00 | 2.89
509 | 2 | E45 | 0.00
509 | 3 | C60 | 0.59
510 | 2 | C60 | 1.55
510 | 3 | F00 | 0.05


I am trying to get the total production time (pp) for all article numbers (artno) where the department is C60, but only when the C60 department occupies the lowest row number for all records of that article number...

That is:

artno | row | abt | pp
------+-----+-----+-----
507 | 2 | C60 | 1.23
510 | 2 | C60 | 1.55


And then the total pp for these results: 2.78.

I hope it this makes some sense, and I would greatly appreciate any help i suggestions.
Reply With Quote
  #2 (permalink)  
Old 12-08-04, 05:49
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Perhaps something like this?
Code:
SELECT SUM(my_value) 
FROM (SELECT SUM(pp) my_value 
	  FROM mytab m
	  WHERE m.abt = 'C60'
	  GROUP BY m.artno, m.row_num
	  HAVING m.row_num = (SELECT MIN(m1.row_num) 
						  FROM mytab m1
						  WHERE m1.artno = m.artno
						 )
	 );
Reply With Quote
  #3 (permalink)  
Old 12-08-04, 07:45
steve_o steve_o is offline
Registered User
 
Join Date: Apr 2004
Posts: 43
Thanks! But i get a parse error at or near "my_value", referring to the "...(SELECT SUM(pp) my_value...". My syntax is the same as yours...

Any ideas where this comes from??
Reply With Quote
  #4 (permalink)  
Old 12-08-04, 08:06
steve_o steve_o is offline
Registered User
 
Join Date: Apr 2004
Posts: 43
Thanks! But i get a parse error at or near "my_value", referring to the "...(SELECT SUM(pp) my_value...". My syntax is the same as yours...

Any ideas where this comes from??
Reply With Quote
  #5 (permalink)  
Old 12-08-04, 09:35
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
OK, try this ...
Code:
SELECT SUM(m.pp) 
FROM mytab m
WHERE m.abt = 'C60'
AND m.row_num = (SELECT MIN(m1.row_num)
				 FROM mytab m1
				 WHERE m1.artno = m.artno
				 GROUP BY m1.artno
				 );
(both were written on Oracle 8.1.7; perhaps you SQL doesn't support the way first query was made).
Reply With Quote
  #6 (permalink)  
Old 12-08-04, 10:27
steve_o steve_o is offline
Registered User
 
Join Date: Apr 2004
Posts: 43
Thanks very much for your help. I am using a Postgres DB - but the new syntax did the trick!
Cheers,
Stephen.
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