How to find out the maximum sales of an item for each sales man.The table consists sales_man, item, sales columns, each sales man will have the different sales values for different items.here I have to find out for every sales man, which item he sold maximum

Showing Answers 1 - 3 of 3 Answers

Mak

  • Aug 29th, 2007
 


First, you have to ask two questions -

Assuming the given table is (sp, item, Q)   ---  sp is sales person id, Q is quantity sold for that item.

a) are there duplicate rows, which means we will need to sum the quantity first.
b) what happens if a sales person sold more than one item with the SAME maximum quantity ? ie. John's top sale was 500, however, he sold 500 computers and also sold 500 laptops. Does the report show only one item, or, both items, and, if only one, then which one should be shown (ie. what is the criteria). I am assuming that both items should be shown.  

Here is the query

select  sp, item, sum(Q) as TQ
from <table>
into #t
group by sp, item

select sp, item, TQ
from #t a
where a.TQ = (select max(TQ) from #t b where b.sp = a.sp)

========

if you want everything in one query, then here is the one --



select a.sp, a.item, a.TQ
from  (select  sp, item, sum(Q) as TQ
from <table>
group by sp, item ) a
where a.TQ = (select max(TQ) from #t b where b.sp = a.sp)

========

  Was this answer useful?  Yes

nagaraju_p

  • Sep 9th, 2009
 

select a.sales_person, a.item, a.TotalQuantity
FROM (select b.sales_person,b.item,SUM(b.InvAmount ) as TotalQuantity from sales_data b
Group by b.sales_person,b.item)as a
GROUP BY a.sales_person
HAVING a.TotalQuantity  = MAX(a.TotalQuantity )

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions