Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2006-09-29 : 13:52:21
|
| THIS IS THE SAMPLE DATEI want to see supplierID, sum of amount shipped and sum of amount not shippedand group them by suppier, total no of orders accpeted by the vendor, total no of orders declinedCREATE TABLE #OrdersShipped (OrderID int, [Shipped Date] datetime)INSERT #OrdersShippedSELECT 1, '1/2/05' union allSELECT 2, '2/3/05'CREATE TABLE #OrderStatus(supplierIDint, OrderID int, status varchar(10), amount decimal(9,2))INSERT #OrderStatusSELECT 111, 1 , 'accepted', 1000.00 UNION ALLSELECT 111, 2, 'accepted', 2500.00 UNION ALLSELECT 222, 3, 'pending', 2000 UNION ALLSELECT 222, 4, 'accepted', 3000.00 UNION ALLSelect 333, 5, 'declined', 200 UNION ALLSelect 333, 6, 'accepted', 500Ashley Rhodes |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-09-29 : 14:08:10
|
quote: Is this an RFW?
Yeah, I was wondering that too!Ashley, here's the query you need - in future though, you need to start posting what you have tried and we'll help you get it right, but if you just continually ask us to do your work for you - you'll start finding no-one will reply.SELECT #OrderStatus.supplierID , sum(CASE WHEN [Shipped Date] IS NOT NULL THEN amount ELSE 0 END) AS amtshipped , sum(CASE WHEN [Shipped Date] IS NULL THEN amount ELSE 0 END) AS amtnotshipped , sum(CASE WHEN status ='accepted' THEN 1 ELSE 0 END) AS accepted , sum(CASE WHEN status ='declined' THEN 1 ELSE 0 END) AS declinedFROM #OrderStatusLEFT OUTER JOIN #OrdersShipped ON #OrderStatus.OrderID = #OrdersShipped.OrderIDGROUP BY supplierID |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2006-10-02 : 16:02:26
|
| Its not that I did not triedTHIS IS WHAT I HAD. I had a problem with grouping select supplierID,sum(case when o.status = 'accepted' then 1 else 0 end) as [ORDERS ACCEPTED],sum(case when o.status = 'declined' then 1 else 0 end) as [ORDERS DECLINED],sum(case when o.status = 'accepted' and o.orderid in (select os1.orderid from #ordersshipped os1 where os1.orderid = o.orderid) then 1 else 0 end ) as [ORDERS SHIPPED] , sum(case when o.status = 'accepted' and o.orderid not in (select os1.orderid from #ordersshipped os1 where os1.orderid = o.orderid) then 1 else 0 end ) as [ORDERS PENDING SHIPPING] , sum(case when o.status = 'accepted' and o.orderid in (select os1.orderid from #ordersshipped os1 where os1.orderid = o.orderid) then o.amount else 0 end ) as [AMOUNT OF ORDERS SHIPPED] , sum(case when o.status = 'accepted' and o.orderid not in (select os1.orderid from #ordersshipped os1 where os1.orderid = o.orderid) then o.amount else 0 end ) as [AMOUNT OF PENDING SHIPPING] , sum(case when o.status = 'declined' then o.amount else 0 end ) as [AMOUNT OF ORDERS DECLINED] into #t1From #ORDERS OGROUP BY o.supplierid, O.status, O.OrderID, o.amountGOselect * from #t1 group by supplierID, [ORDERS ACCEPTED], [ORDERS DECLINED],[ORDERS SHIPPED], [ORDERS PENDING SHIPPING], [AMOUNT OF ORDERS SHIPPED], [AMOUNT OF PENDING SHIPPING], [AMOUNT OF ORDERS DECLINED]godrop table #OrdersShipped , #orders , #t1Ashley Rhodes |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2006-10-02 : 16:02:53
|
| WHAT IS RFW??????????????Ashley Rhodes |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-10-02 : 22:38:19
|
| Request for work. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-03 : 09:22:02
|
| RTDH - Request to do homeworkCODO ERGO SUM |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-03 : 10:03:50
|
| >> I had a problem with grouping What is the problem?MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|
|