Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Help with group by

$
0
0
I have this example that is relative to something I am working on and I don't know how to over come this. I nee the custID and orderID for the order with the highest amount. I have tried several things but failed. So here i have the basis of getting the orders with total anount. I just don't know how to get what I want from there.below is where I am at this time with the sample:[code]declare @cust table(custID int, name varchar(15))declare @order table(orderid int, custid int, orderNum varchar(15),orderdate datetime)declare @orderdetail table(orderDetid int, orderid int, itemnum varchar(10),Itemcnt int,amt int)insert into @cust(custID,name)values (1,'CustOne'),(2,'CustTwo')insert into @order (orderid,custid,orderNum,orderdate)values (1,1,'100','2014-01-01'),(2,1,'101','2014-02-02'),(3,1,'103','2014-03-03'),(4,2,'201','2014-03-03')insert into @orderdetail(orderDetid,orderid,itemnum,Itemcnt,amt)values (1,1,'itm#3',1,2),(2,1,'itme#4',2,3),(3,2,'item5',2,6),(4,4,'401',3,5),(5,4,'402',2,10)-- get custID, order, tot with highest amount for each cust select o.custid,od.orderid as IorderID, sum(amt * itemcnt) as tot from @orderDetail od join @order o on o.orderid = od.orderid group by o.custid,od.orderid[/code]

Viewing all articles
Browse latest Browse all 3145

Trending Articles