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-28 : 17:57:18
|
| I have 2 tables OrdersOrderID, Shipped Date1, 1/2/05and another tableOrderID, status1 , accepted2, accepted3, pending4, acceptednow I want to find out the sum of orders where status is accepted and they have not been shipped this means there is no record of them in the first table.Ashley Rhodes |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-09-28 : 18:07:22
|
You said the sum or the orders but didn't specify that one of the tables has an amount column, so I assumed that it is in the second table. I'm not sure why you have the status and the shipping date for an order in two different tables, so you may have a design problem, but all that aside, this should give you what you want:CREATE TABLE #Orders (OrderID int, [Shipped Date] datetime)INSERT #OrdersSELECT 1, '1/2/05'CREATE TABLE #OrderStatus(OrderID int, status varchar(10), amount decimal(9,2))INSERT #OrderStatusSELECT 1 , 'accepted', 1000.00 UNION ALLSELECT 2, 'accepted', 2500.79 UNION ALLSELECT 3, 'pending', 1999.45 UNION ALLSELECT 4, 'accepted', 3000.00 SELECT sum(amount)FROM #OrderStatusWHERE status = 'accepted' AND NOT EXISTS (SELECT * FROM #Orders WHERE OrderID = #OrderStatus.OrderId) |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2006-09-29 : 09:25:12
|
| What is I want to see sum of orders which have been shipped as wellI would need a seperate column for that.Ashley Rhodes |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-29 : 09:31:12
|
| Do you have some sample data for the two tables?Peter LarssonHelsingborg, Sweden |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2006-09-29 : 10:21:09
|
| 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 |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2006-10-02 : 15:33:30
|
| I know the SQL for this simple query very well.I WANTED TO GROUP EVERTHING TOGETHER IN 1 QUERY.LOOK AT THE RESULT OF THIS. HOW DO I GET the SUMS of each fieldmeans GROUP by SUPPLIER ID in one query without using derived or temp tablesCREATE TABLE #OrdersShipped (OrderID int, [Shipped Date] datetime)INSERT #OrdersShippedSELECT 1, '1/2/05' union allSELECT 2, '2/3/05'CREATE TABLE #Orders(supplierID int, OrderID int, status varchar(10), amount int)INSERT #OrdersSELECT 111, 1, 'accepted', 1000 UNION ALLSELECT 111, 2, 'accepted', 2500 UNION ALLSELECT 111, 8, 'declined', 200 UNION ALLSELECT 222, 3, 'accepted', 2000 UNION ALLSELECT 222, 4, 'accepted', 3000 UNION ALLSELECT 333, 5, 'declined', 2000 UNION ALLSELECT 333, 6, 'accepted', 500 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],(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] , (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 = 'declined' then 1 else 0 end ) as [ORDERS DECLINED] , (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] , (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] From #ORDERS OGROUP BY o.supplierid, O.status, O.OrderID, o.amountGOdrop table #OrdersShipped , #ordersAshley Rhodes |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-02 : 15:45:20
|
| There are no derived tables in the query you posted. Does it produce the results you want?CODO ERGO SUM |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-02 : 16:45:29
|
| There are no aggregations for1) [ORDERS SHIPPED]2) [ORDERS PENDING SHIPPING]3) [AMOUNT OF ORDERS SHIPPED]4) [AMOUNT OF PENDING SHIPPING]Also, there are too many groupings1) o.supplierid2) O.status3) O.OrderID4) o.amountPeter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-02 : 16:52:26
|
Play around with this query for a while and se if the result is what you expect.SELECT o.SupplierID, SUM(CASE WHEN o.Status = 'Accepted' THEN 1 ELSE 0 END) [ORDERS ACCEPTED], SUM(CASE WHEN o.Status = 'Declined' THEN 1 ELSE 0 END) [ORDERS DECLINED], SUM(CASE WHEN o.Status = 'Accepted' AND os.OrderID IS NOT NULL THEN 1 ELSE 0 END) [ORDERS SHIPPED], SUM(CASE WHEN o.Status = 'Accepted' AND os.OrderID IS NULL THEN 1 ELSE 0 END) [ORDERS PENDING SHIPPING], SUM(CASE WHEN o.Status = 'Declined' THEN 1 ELSE 0 END) [ORDERS DECLINED], SUM(CASE WHEN o.Status = 'Accepted' AND os.OrderID IS NOT NULL THEN o.Amount ELSE 0 END) [AMOUNT OF ORDERS SHIPPED], SUM(CASE WHEN O.Status = 'Accepted' AND os.OrderID IS NULL THEN o.Amount ELSE 0 END) [AMOUNT OF PENDING SHIPPING], SUM(CASE WHEN o.Status = 'Declined' THEN o.Amount ELSE 0 END) [AMOUNT OF ORDERS DECLINED]FROM Orders oLEFT JOIN OrdersShipped os ON os.OrderID = o.OrderID GROUP BY o.SupplierID Peter LarssonHelsingborg, Sweden |
 |
|
|
sa
Yak Posting Veteran
77 Posts |
Posted - 2006-10-02 : 19:35:39
|
| SELECT orderid,status,SUM(amount)--assuming)FROM anotherTableWHERE orderid NOT IN(SELECT orderid FROM orders)GROUP BY orderidThis is how i understood your need. Wish it will work for you! |
 |
|
|
|
|
|
|
|