Solved

Please help with a MS SQL Select query (calculate total order weight)

Posted on 2015-01-09
6
103 Views
Last Modified: 2015-01-27
I have the following query:

SELECT O.OrderID
     , O.ShipFirstName
     , O.ShipLastName
     , O.ShipCompanyName
     , O.ShipAddress1
     , O.ShipAddress2
     , O.ShipCity
     , O.ShipState
     , O.ShipPostalCode
     , CT.Name AS Country
     , O.ShipPhoneNumber
     , O.ShippingMethodID
     , O.TotalShippingCost
     , (O.PaymentAmount - O.TotalShippingCost) AS FOB
     , O.OrderStatus
     , O.BatchNumber
     , O.Custom_Field_eBayID
     , C.EmailAddress
     , od_totals.Product_Count
     , od_totals.Order_Weight

FROM Orders o
JOIN Customers C ON C.CustomerID = O.CustomerID
LEFT JOIN Countries CT ON CT.Name = O.ShipCountry
INNER JOIN (
    SELECT od.OrderID
         , COUNT(DISTINCT od.ProductCode) AS Product_Count
         , SUM(od.Quantity * p.ProductWeight) AS Order_Weight
    FROM OrderDetails od
    INNER JOIN Products_Joined p ON p.ProductCode = od.ProductCode
    GROUP BY od.OrderID
    HAVING MAX(CASE WHEN od.QtyOnBackOrder > 0 THEN 1 ELSE 0 END) = 0
) AS od_totals ON od_totals.OrderID = o.OrderID

WHERE o.ShipDate IS NULL
  AND o.BatchNumber IS NULL 
  AND (
      o.OrderStatus = 'PROCESSING'
      OR (
      ShippingMethodID = 6761
      AND BatchNumber IS NULL   
      AND OrderStatus <> 'CANCELLED'))
ORDER BY O.OrderID ASC
;

Open in new window


The problem seems to be in lines 27 and 28. I've done something wrong here.

You'll notice that there are some calculated fields, included these two:
     od_totals.Product_Count
     od_totals.Order_Weight

The query is NOT returning the proper results.

For example, say the order consists of 3 Widgets:

3 x WidgetA (weighing 0.5 kg each)
2 x WidgetB (weighing 0.25 kg each)
1 x WidgetC (weight 0.25 kg)

What I would LIKE to see in my query result is
     od_totals.Product_Count = 6 (i.e. 3 + 2 + 1)
     od_totals.Order_Weight = 2.25 kg (i.e. (3x0.5kg) + (2x0.25kg) + (1x0.25kg)

I hope you're "with me" so far.

However, this is what the query is returning:
     od_totals.Product_Count = 3
     od_totals.Order_Weight = 1.0 kg

So what have I done wrong. Can somebody please help me redo the query to return the correct results?

Thanks guys!  :D
0
Comment
Question by:gregholl
6 Comments
 
LVL 33

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 125 total points
Comment Utility
remove distinct from line 27

, COUNT(od.ProductCode) AS Product_Count
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 125 total points
Comment Utility
You need to sum the item qty's to get 6:

   SELECT od.OrderID
         , SUM(od.Quantity) AS Product_Count

But as long as the ProductWeight already has decimal places in it, I don't see why you're getting a wrong answer there.  You could explicitly CAST a value just to be sure:

         , SUM(CAST(od.Quantity AS decimal(9, 2)) * p.ProductWeight) AS Order_Weight
0
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 125 total points
Comment Utility

1.

Remove distinct  when calculating Product_Count

2.

Order_Weight calculation looks correct, but the having-clause may be filtering out data
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
Hi John,

How did you make these nice bullet points with numbers in it?
0
 
LVL 15

Assisted Solution

by:gplana
gplana earned 125 total points
Comment Utility
Can you execute this to see what is happening?:

    SELECT od.OrderID
         , od.ProductCode
         , od.Quantity * p.ProductWeight
    FROM OrderDetails od
    INNER JOIN Products_Joined p ON p.ProductCode = od.ProductCode

and then let us know the result of this SELECT ?
0
 
LVL 1

Author Closing Comment

by:gregholl
Comment Utility
Thanks guys! I got it worked out
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now