Solved

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

Posted on 2015-01-09
6
105 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 34

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 34

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Acces SQL Insert Problem 6 31
SQL Quer 4 21
Error when creating a table from a function 6 17
HTML <font style="color:red"> 9 30
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

809 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