Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:40:57+00:00 2026-05-27T11:40:57+00:00

I really don’t know what I’m doing with SQL, but I have two tables.

  • 0

I really don’t know what I’m doing with SQL, but I have two tables. I know it’s possible that you can pull data from one table and add it to another table if columns are equivalent.
So I wanted something like:

SELECT Sum(OrderDetails.Vendor_Price * OrderDetails.Quantity)
WHERE Orders.OrderID=OrderDetails.OrderID
AS COGS

Basically, the orders and orderdetails table are two separate tables but the orderdetails has the OrderID element which associates it to an order in the orders table. So my function is where the Orders.OrderID=OrderDetails.OrderID, that is where to implement the sum, I want it for each order.
I was able to find a sample code that does this task with a lot of excess code:

SELECT orders.orderid,
       orders.cogs
FROM   (SELECT orders.orderid,
               orderdetails.cogs
        FROM   (SELECT orders.orderid AS orderid
                FROM   (((orders WITH(nolock)
                          LEFT JOIN paymentmethods WITH(nolock)
                            ON orders.paymentmethodid =
                               paymentmethods.paymentmethodid)
                         LEFT JOIN shippingmethods WITH(nolock)
                           ON orders.shippingmethodid =
                              shippingmethods.shippingmethodid)
                        LEFT JOIN customers WITH(nolock)
                          ON orders.customerid = customers.customerid)
                GROUP  BY orders.orderid) orders
               INNER JOIN (SELECT
                          orders.orderid
                          AS orderid,
                                  COUNT(orderdetails.orderdetailid)
                          AS
                                                            orderdetails_count
                                                            ,
                                  SUM(orderdetails.quantity)
                                                            AS quantity,
                                  SUM(orderdetails.vendor_price *
                          orderdetails.quantity) AS
                                                            cogs,
                                  CASE
                                    WHEN SUM(vendor_price) IS NULL THEN NULL
                                    ELSE SUM(( CASE
                                                 WHEN orderdetails.productcode
                                                      LIKE
                                                      'DSC-%'
                                               THEN
                                                 orderdetails.productprice
                                                 - Isnull(
                                                 orderdetails.vendor_price, 0)
                                                 ELSE orderdetails.productprice
                                                      -
                                                      orderdetails.vendor_price
                                               END ) * orderdetails.quantity)
                                  END
                          AS
                                                            profit,
                                  CASE SUM(orderdetails.productprice *
                          orderdetails.quantity)
                                    WHEN 0 THEN 0
                                    ELSE Round(( ( SUM(orderdetails.productprice
                                                       *
                                                       orderdetails.quantity)
                                                   - SUM(
                                                     orderdetails.vendor_price *
                                                     orderdetails.quantity) ) /
SUM(
             orderdetails.productprice
             *
             orderdetails.quantity) )
* 100,
1)
END
AS
           profitmargin
FROM   (((orders WITH(nolock)
LEFT JOIN paymentmethods WITH(nolock)
ON orders.paymentmethodid =
paymentmethods.paymentmethodid)
LEFT JOIN shippingmethods WITH(nolock)
ON orders.shippingmethodid =
shippingmethods.shippingmethodid)
LEFT JOIN customers WITH(nolock)
ON orders.customerid = customers.customerid)
LEFT JOIN orderdetails WITH(nolock)
ON orders.orderid = orderdetails.orderid
WHERE  orders.orderstatus <> 'Cancelled'
GROUP  BY orders.orderid) orderdetails
ON orders.orderid = orderdetails.orderid) orders
ORDER  BY orders.orderid DESC  

This basically delivers the orders and their COGS for each order into the table. But every time I try to delete a line of excess code I get an error. Things like ShippingMethodID are unnecessary. Please help.

EDIT:

SELECT Orders.OrderID, Orders.SalesRep_CustomerID, Orders.Total_Payment_Received, Orders.SalesTax1, SumDetails.COGS, ISNULL(Total_Shipping_Cost.Shipping_Cost,0) as Shipping_Cost

FROM Orders

JOIN

(SELECT OrderID, Sum(OrderDetails.Vendor_Price * OrderDetails.Quantity)

AS COGS

FROM OrderDetails

GROUP BY OrderID)

AS SumDetails

ON SumDetails.OrderID=Orders.OrderID LEFT

JOIN

(SELECT OrderID, SUM(Shipment_Cost)

AS Shipping_Cost

FROM Trackingnumbers

GROUP BY OrderID)

AS Total_Shipping_Cost

(SELECT CASE

WHEN Orders.ShippingMethodID

in (19, 20, 21, 25, 26, 27, 28, 30, 31, 502)

THEN 5

ELSE 0

END)

AS ServiceCharge

ON Total_Shipping_Cost.OrderID = Orders.OrderID

WHERE Orders.OrderStatus = ‘Shipped’

AND Orders.ShipDate > (GETDATE()-6)

AND Orders.PaymentAmount = Orders.Total_Payment_Received

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:40:57+00:00Added an answer on May 27, 2026 at 11:40 am

    This should get you started:

    SELECT Orders.OrderID,
    SumDetails.COGS
    FROM Orders
    JOIN 
        (SELECT OrderID,
        Sum(OrderDetails.Vendor_Price * OrderDetails.Quantity) AS COGS
        FROM OrderDetails
        GROUP BY OrderID) AS SumDetails
    ON SumDetails.OrderID=Orders.OrderID
    

    EDIT: To add in the other columns from Orders (and this is why I like the subquery approach – they don’t have to be in a GROUP BY):

    SELECT Orders.OrderID,
    Orders.SalesRep_CustomerID,
    Orders.Total_Payment_Received,
    Orders.S‌​alesTax1,
    SumDetails.COGS,
    ISNULL(Total_Shipping_Cost.Shipping_Cost,0) as Shipping_Cost
    FROM Orders
    JOIN 
        (SELECT OrderID,
        Sum(OrderDetails.Vendor_Price * OrderDetails.Quantity) AS COGS
        FROM OrderDetails
        GROUP BY OrderID) AS SumDetails
    ON SumDetails.OrderID=Orders.OrderID
    LEFT JOIN
        (SELECT OrderID,
        SUM(Shipment_Cost) AS Shipping_Cost 
        FROM Trackingnumbers
        GROUP BY OrderID) AS Total_Shipping_Cost 
    ON Total_Shipping_Cost.OrderID = Orders.OrderID  
    WHERE Orders.OrderStatus = 'Shipped' 
    AND Orders.ShipDate > (GETDATE()-6)
    AND Orders.PaymentAmount = Orders.Total_Payment_Received
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That can be silly question but I really don't know the reason. // i
I have a homework problem that I really don't know how to start. Here
I know that the naming conventions really don't say anything about that, but I
A really boring question, sorry, but I really don't know that yet ;) I've
I really don't know how to describe it, but if you understood it and
As a developer I really don't like writing documentation but when I have to
I really don't know/have the answer, knowledge to find a resource value using a
I really don't understand what I'm doing wrong, but according to Adobe, this is
I really don't know how to title this question, but I need some help
I have this json object (which i don't really know what is inside) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.