销售百分比

时间:2017-07-14 19:13:27

标签: sql

我试图计算每件商品销售给特定客户的数量x每件商品的总销售额。我想看看这个客户在我们的总销售额中是否有很大份额的任何产品。

我想按日期,项目编号和仓库代码对其进行分组。

由于某种原因,这些值不对。有人可以看一看,看看下面的查询是否有任何问题?感谢您的帮助!

    SELECT 
DISTINCT 
tb1."ItemCode"
,tb1."ItemName"
, tb2."Quantity" as "Quantity"
, tb2."LineTotal" as "Revenue"
, tb2."AvgPrice" as "Average Sale Price"
, tb3."Quantity" as "Specific customer Quantity"
, tb3."LineTotal" as "Specific customer Revenue"
, tb3."AvgPrice" as "Specific customer Avg Sale Price"
,tb3."Date" as "Date"
,tb2."Warehouse" as "Warehouse"
FROM
(
SELECT DISTINCT T0."ItemCode", "ItemName" FROM "xx" T0
) tb1
LEFT OUTER JOIN
(
SELECT
    DISTINCT T1."ItemCode" as "ItemCode"
    , SUM(T1."Quantity") as "Quantity"
    , SUM(T1."LineTotal") as "LineTotal"
    , AVG(T1."PriceBefDi") as "AvgPrice
,T1."DocDate" as "Date"
,T1."WhsCode" as "Warehouse"
FROM "xx2"T1 INNER JOIN "xx3"T0 ON T0."DocEntry"=T1."DocEntry"
        WHERE T0."CANCELED" = 'N'
GROUP BY T1."ItemCode", T1."DocDate", T1."WhsCode"
    ) tb2
ON tb1."ItemCode" = tb2."ItemCode"
LEFT OUTER JOIN
(
SELECT
    DISTINCT T1."ItemCode" as "ItemCode"
    , SUM(T1."Quantity") as "Quantity"
    , SUM(T1."LineTotal") as "LineTotal"
    , AVG(T1."PriceBefDi") as "AvgPrice"
 ,T1."DocDate" as "Date"
,T1."WhsCode"
FROM "xx2"T1 INNER JOIN "xx3"T0 ON T0."DocEntry"=T1."DocEntry"
        WHERE T0."CANCELED" = 'N'
    AND T0."CardCode" = 'Specific Customer'
        GROUP BY T1."ItemCode", T1."DocDate", T1."WhsCode") tb3 ON tb1."ItemCode" = tb3."ItemCode"

0 个答案:

没有答案