Question

Say I have the following:

  PART     SUBPART  QUANTITY
  -------- -------- -----------
  01       02                 2
  01       03                 3
  01       04                 4
  01       06                 3
  02       05                 7
  02       06                 6
  03       07                 6
  04       08                10
  04       09                11
...

For each part I need to identify the subpart with the max quantity.

My real example is a little bit more complex, I mean that there are not one, but 3 subpart columns (like a composite key). So I need to identify part each part the couples subpart1, subpart2 and subpart3...

As database I use db2 for as400, but any examples are welcome.

I tried to do the following, but this does not work:

with T (PART, SUBPART1, SUBPART2, SUBPART3, SQ) AS 
       (SELECT PART, SUBPART1, SUBPART2, SUBPART3, SUM(QUANTITY)
        FROM MYTABLE 
        GROUP BY PART, SUBPART1, SUBPART2, SUBPART3)    
select PART, SUBPART1, SUBPART2, SUBPART3 
WHERE SQ = max(SQ)
from T
group by PART
Was it helpful?

Solution

"For each part I need the subpart that has the maximum quantity". How about this?

select t.*
from mytable t join
     (select part, max(quantity) as maxq
      from t
      group by part
     ) m
     on m.part = t.part and m.maxq = t.quantity;

OTHER TIPS

"For each part I need to identify the subpart having the max quantity."

SELECT part, subpart 
FROM table t1
RIGHT JOIN(
SELECT part, subpart, MAX(quantity)
FROM table 
GROUP BY part, subpart) t2 ON t1.part=t2.part AND t1.subpart=t2.subpart

In your example data subpart seems distinct, but you didn't said anything about it, so I'm guessing that subpart isn't unique for each part (it is possible that subpart takes the value 01 for many parts)

I assume DB2 for AS400 doesn't support MAX OVER, i.e. Windowed Aggregate Functions?

Then it would be easy, otherwise you need a correlated subquery:

with T (PART, SUBPART1, SUBPART2, SUBPART3, SQ) AS 
       (SELECT PART, SUBPART1, SUBPART2, SUBPART3, SUM(QUANTITY)
        FROM MYTABLE 
        GROUP BY PART, SUBPART1, SUBPART2, SUBPART3)    
select PART, SUBPART1, SUBPART2, SUBPART3 
from T as t1
WHERE SQ = 
   (select max(SQ) from T as t2
    where t1.PART = t2.PART)

Edit:

This is a version using Windowed Aggregate Functions

with T (PART, SUBPART1, SUBPART2, SUBPART3, SQ, MAXSQ) AS 
       (SELECT PART, SUBPART1, SUBPART2, SUBPART3, SUM(QUANTITY),
            SUM(SUM(QUANTITY)) OVER (PARTITION BY PART)
        FROM MYTABLE 
        GROUP BY PART, SUBPART1, SUBPART2, SUBPART3)    
select PART, SUBPART1, SUBPART2, SUBPART3 
WHERE SQ = MAXSQ
from T

But this version of DB2 seems to support only rankings:

with T (PART, SUBPART1, SUBPART2, SUBPART3, SQ, rnk) AS 
       (SELECT PART, SUBPART1, SUBPART2, SUBPART3, SUM(QUANTITY),
            RANK() OVER (PARTITION BY PART ORDER BY SUM(QUANTITY) DESC)
        FROM MYTABLE 
        GROUP BY PART, SUBPART1, SUBPART2, SUBPART3)    
select PART, SUBPART1, SUBPART2, SUBPART3 
WHERE rnk = 1
from T
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top