Solved

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

Posted on 2015-01-09
6
104 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
ID: 40541339
remove distinct from line 27

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

Accepted Solution

by:
Scott Pletcher earned 125 total points
ID: 40541375
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
ID: 40541399

1.

Remove distinct  when calculating Product_Count

2.

Order_Weight calculation looks correct, but the having-clause may be filtering out data
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40541649
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
ID: 40541765
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
ID: 40573768
Thanks guys! I got it worked out
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

813 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

11 Experts available now in Live!

Get 1:1 Help Now