7

I have a table called "Inventory" with the fields Item (varchar), Amount (int), Type (varchar)

What I would like to extract is rows with the following fields:

Shortest entry in the Item field of all Items of type Type

Sum of all Amounts of all Items of type Type

I have the following:

SELECT Item, sum( Amount ) FROM Inventory GROUP BY Type

which gives what I want except it doesn't return the shortest Item, instead it has one of the other Items (the last one it finds I think). By shortest I mean minimum string length.

Thanks for any help.

2
  • In MySQL, the behavior of selecting a column which is not in your GROUP BY list is undefined. MySQL can return any matched row's value for that column. Commented Feb 4, 2011 at 5:31
  • A more specific question with test data and expected output would greatly help. Commented Feb 4, 2011 at 6:05

3 Answers 3

9

You can get it by sub query.

select type, sum(amount), item 
from inventory 
group by type
having length(item) <= (select min(length(item)) from inventory)

User Order By columnName ASC /DESC for sorting and LIMIT 1 for getting one out of that

Sign up to request clarification or add additional context in comments.

2 Comments

With the caveat that if multiple items have the same shortest length, it's undefined which of those items of the same length will be returned.
I don't think the sum will give you the expected value here. The sum will only be of all the items with exactly the same length as the minimum length. Girish Rao's latest answer below will give you the correct sum as well.
1

select type, sum(amount), min(length(item)) from inventory group by type

should do what you want

3 Comments

No, I already thought of that. That query returns the length of the minimum length Item string instead of the Item string itself.
Ah sorry. Give this a shot select e.type, max_b.s, e.item from inventory e inner join (select e2.type, sum(e2.amount) as s, e2.item, min(length(e2.item)) as bb from inventory e2 group by e2.type) max_b on (length(e.item)=max_b.bb) group by e.type;
My answer is in the comment above. Is that the right place for it or should I create a new Answer and put it there?
1

Not sure where this should go, so I'm creating a new answer:

select e.type, min_item.s, e.item from inventory e 
  inner join 
    (select e2.type, sum(e2.amount) as s, e2.item, min(length(e2.item)) as bb 
     from inventory e2 group by e2.type) min_item 
  on (length(e.item)=min_item.bb) 
group by e.type;

I tested this and it worked.

1 Comment

You should have edited the first answer instead. Try to remove your first answer since that one is not correct. Otherwise you will get down-votes for that. This is the correct answer to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.