1

我有一个为 Volusion 构建的查询,它的 SQL 能力非常有限,例如我不能使用临时表,甚至不能使用变量……这就是我通常会尝试执行此任务的方式。

但是,我有这个查询,它为我提供了我需要的信息,但我必须在运行后在 Excel 中使用它......我想获得查询以获得我需要的结果。

我最终需要的是 google_gender 和 rep... 的成本/利润。有没有一种方法可以在没有变量或临时表的情况下完成此操作?

这是我当前的查询:

SELECT
    Products_Joined.Google_Gender,
    REPLACE(REPLACE(REPLACE(REPLACE(Customers.SalesRep_CustomerID,379646,'Dan'),658024,'Owen'),643352,'Mary'),584310,'Pete') as SalesRep,
    SUM(OrderDetails.ProductPrice)*OrderDetails.Quantity as TotalSold,
    SUM(OrderDetails.Vendor_Price)*OrderDetails.Quantity as VendorCost,
    SUM(OrderDetails.ProductPrice)*OrderDetails.Quantity-(OrderDetails.Vendor_Price)*OrderDetails.Quantity as Profit
FROM
    Products_Joined, OrderDetails, Orders, Customers
WHERE
    Products_Joined.ProductCode=OrderDetails.ProductCode 
    AND OrderDetails.OrderID=Orders.OrderID 
    AND Orders.CustomerID=Customers.CustomerID
    AND Orders.OrderDate BETWEEN '05/01/16 00:00' and '05/31/16 23:59'
    AND Customers.SalesRep_CustomerID in ('379646' , '658024' , '643352' , '584310')
    AND Orders.OrderStatus <> 'Cancelled'
GROUP BY    
    Products_Joined.Google_Gender,
    OrderDetails.Quantity,
    OrderDetails.Vendor_Price,
    OrderDetails.ProductPrice,
    Customers.SalesRep_CustomerID

因为查询必须从 OrderDetails 中提取商品信息,以获取“售价”,它在结果文件中列出每个订单#和 sku,我不需要,我只需要每个 google_gender 的总金额,由代表. 并且只有 4 个代表 :)

4

1 回答 1

2

要获得每个性别/代表的 SUM,您需要摆脱其他分组级别。没有任何架构或数据,我只是在猜测,但这可能会给你你想要的;

SELECT
    Products_Joined.Google_Gender,
    REPLACE(REPLACE(REPLACE(REPLACE(Customers.SalesRep_CustomerID,379646,'Dan'),658024,'Owen'),643352,'Mary'),584310,'Pete') as SalesRep,
    SUM(OrderDetails.ProductPrice*OrderDetails.Quantity) as TotalSold,
    SUM(OrderDetails.Vendor_Price*OrderDetails.Quantity) as VendorCost,
    SUM(OrderDetails.ProductPrice*OrderDetails.Quantity)-SUM(OrderDetails.Vendor_Price*OrderDetails.Quantity) as Profit
FROM
    Products_Joined, OrderDetails, Orders, Customers
WHERE
    Products_Joined.ProductCode=OrderDetails.ProductCode 
    AND OrderDetails.OrderID=Orders.OrderID 
    AND Orders.CustomerID=Customers.CustomerID
    AND Orders.OrderDate BETWEEN '05/01/16 00:00' and '05/31/16 23:59'
    AND Customers.SalesRep_CustomerID in ('379646' , '658024' , '643352' , '584310')
    AND Orders.OrderStatus <> 'Cancelled'
GROUP BY    
    Products_Joined.Google_Gender,
    Customers.SalesRep_CustomerID

请注意,所有 OrderDetails 都在 SUM 函数中。

于 2016-05-23T14:31:51.530 回答