我真的不知道我在用SQL做什么,但我有两张桌子。我知道如果列相同,您可以从一个表中提取数据并将其添加到另一个表中。 所以我想要这样的东西:
SELECT Sum(OrderDetails.Vendor_Price * OrderDetails.Quantity)
WHERE Orders.OrderID=OrderDetails.OrderID
AS COGS
基本上,orders和orderdetails表是两个单独的表,但orderdetails具有OrderID元素,该元素将它与orders表中的订单相关联。所以我的函数是Orders.OrderID = OrderDetails.OrderID,即实现总和的地方,我想要每个订单。 我能够找到一个示例代码来执行此任务,其中包含大量多余的代码:
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
这基本上将每个订单的订单及其COGS交付到表中。但每当我尝试删除一行多余的代码时,我都会收到错误。像ShippingMethodID这样的东西是不必要的。请帮忙。
编辑:
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
(选择案例
当Orders.ShippingMethodID
时in(19,20,21,25,26,27,28,30,31,502)
那么5
ELSE 0
END)
AS ServiceCharge
ON Total_Shipping_Cost.OrderID = Orders.OrderID
WHERE Orders.OrderStatus ='发货'
AND Orders.ShipDate&gt; (GETDATE() - 6)
AND Orders.PaymentAmount = Orders.Total_Payment_Received
答案 0 :(得分:2)
这就是JOIN的用途
在你的例子中,这就是你要做的事情
SELECT Orders.OrderId, SUM(OrderDetails.Vendor_Price * OrderDetails.Quantity)
FROM Orders
INNER JOIN OrderDetails
ON Orders.OrderId = OrderDetails.OrderId
GROUP BY Orders.OrderId
网上有大量文章,您可以阅读联接和汇总数据
链接到解释联接http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/
的文章以下是关于GROUP BY的文章:http://www.sqlteam.com/article/how-to-use-group-by-in-sql-server
答案 1 :(得分:1)
回答你的问题
SELECT Orders.OrderID,Sum(OrderDetails.Vendor_Price * OrderDetails.Quantity) AS COGS
FROM Orders
INNER JOIN OrderDetails ON Orders.OrderID=OrderDetails.OrderID
GROUP BY Orders.OrderID
我相信这相当于你所拥有的...我希望这会有所帮助,因为我在想你想要的东西可能没有考虑到目前正在发生的逻辑。
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))* orderdetails.quantity
ELSE (orderdetails.productprice-orderdetails.vendor_price)* orderdetails.quantity
END
)
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
ORDER BY orders.orderid DESC
答案 2 :(得分:1)
这应该让你开始:
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
编辑:要添加Orders中的其他列(这就是我喜欢子查询方法的原因 - 它们不必位于GROUP BY中):
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
ON Total_Shipping_Cost.OrderID = Orders.OrderID
WHERE Orders.OrderStatus = 'Shipped'
AND Orders.ShipDate > (GETDATE()-6)
AND Orders.PaymentAmount = Orders.Total_Payment_Received
答案 3 :(得分:0)
你在寻找这样的东西:
SELECT Orders.OrderID, Sum(OrderDetails.Vendor_Price * OrderDetails.Quantity)
FROM Orders
INNER JOIN OrderDetails on Orders.OrderID = OrderDetails.OrderID
WHERE ....
GROUP BY Orders.OrderID