[prog] newbie SQL query assistance needed

Monique Y. Mudama monique at bounceswoosh.org
Tue Apr 27 20:28:32 UTC 2010


Depends on the SQL variant, but for MS SQL (sorry, that's what I use
most), here's how I'd get the itemid with the highest aggregate
quantity:

drop table #foo
create table #foo
(
	orderid varchar(10),
	itemid int,
	quantity int
)

insert into #foo 
select 'a', 1, 42 union
select 'b', 2, 100 union
select 'c', 1, 3

select top 1 itemid 
from #foo
group by itemid
order by sum(quantity) desc


On Tue, Apr 27 at 14:54, Tina penned:
> I have an orders table. In it are all the orders (orderid) with each
> item ordered (itemid) and quantity (quantity).
> 
> I need to figure out which item was ordered the most times. I know
> it involves a multi-row query, but I just can't seem to figure it
> out.
> 
> Thanks in advance,
> 
> Tina _______________________________________________ Programming
> mailing list Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
> 

-- 
monique


More information about the Programming mailing list