oracle 如何查看客户的购买动向

xzabzqsa  于 2023-11-17  发布在  Oracle
关注(0)|答案(2)|浏览(109)

我想监视客户的购买行为。每个客户可以购买一个或多个产品,每个产品的价格范围不同。如何确定客户是持续购买、切换到较小的捆绑包还是升级到较大的捆绑包?
到目前为止,我有下面的SQL脚本来查看移动,但是我如何修改它以使其前进并获得我需要的更多细节?

SELECT 
A.Month PRE_MONTH,A.ProductID PRE_PRODUCTID,
A.Product_name PRE_PRODUCT_NAME,
B.Month POST_MONTH,B.ProductID POST_PRODUCTID,
B.Product_name POST_PRODUCT_NAME,
SUM(B.sales)sales, COUNT(DISTINCT B.CustomerID)USER
FROM ( SELECT * FROM TABLE_X WHERE MONTH = 'JAN' ) A 
LEFT OUTER JOIN ( SELECT * FROM TABLE_X WHERE MONTH = 'FEB')B ON A.CustomerID = B.CustomerID

字符串
应该对SQL脚本进行哪些调整,以确定客户1的销售额与前一个月相比是高还是低?此外,我们如何确定他们从哪些产品过渡到哪些产品?
原始数据看起来像这样:


的数据

g52tjvyc

g52tjvyc1#

您可以自连接不同月份的表,然后简单地使用case语句来确定它是更高还是更低的销售额。

SELECT jan_data.CustomerId,
       PRE_PRODUCTID first_product_id,
       PRE_PRODUCT_NAME first_product_name,
       PRE_SALES first_sales,
       POST_PRODUCTID new_product_id,
       POST_PRODUCT_NAME new_product_name,
       POST_SALES new_sales,
       CASE WHEN first_sales > new_sales THEN 'Lower Sales'
            WHEN first_sales < new_sales THEN 'Higher Sales'
            ELSE 'Equal'
       END sales_scale
  FROM (SELECT j.ProductID PRE_PRODUCTID,
               j.Product_name PRE_PRODUCT_NAME,
               j.CustomerId,
               SUM(J.Sales) PRE_SALES
          FROM TABLE_X j WHERE MONTH = 'JAN'
       ) jan_data
  FULL JOIN (SELECT f.month POST_MONTH,
               f.ProductID POST_PRODUCTID,
               f.Product_name POST_PRODUCT_NAME,
               f.CustomerId,
               SUM(J.Sales) POST_SALES
          FROM TABLE_X f WHERE MONTH = 'FEB'
       ) feb_data ON jan_data.CustomerId = feb_data.CustomerId

字符串

pn9klfpd

pn9klfpd2#

其中一个选项是使用LAG()分析函数将销售额与以前的销售额进行比较。

WITH    --  S a m p l e    D a t a :
    tbl ( A_MONTH, CUSTOMER_ID, PRODUCT_ID, PRODUCT_NAME, SALES ) AS
        (   Select '2023-01', 1, 444, 'aaa', 40 From Dual Union All
            Select '2023-01', 2, 333, 'bbb', 30 From Dual Union All
            Select '2023-01', 3, 222, 'ccc', 20 From Dual Union All
            Select '2023-01', 4, 444, 'aaa', 40 From Dual Union All
            Select '2023-01', 5, 1111, 'ddd', 10 From Dual Union All    
            --
            Select '2023-02', 1, 333, 'bbb', 30 From Dual Union All
            Select '2023-02', 2, 444, 'aaa', 40 From Dual Union All
            Select '2023-02', 3, 222, 'ccc', 20 From Dual Union All
            Select '2023-02', 4, 1111, 'ddd', 10 From Dual Union All
            Select '2023-02', 5, 333, 'bbb', 30 From Dual Union All
            --
            Select '2023-03', 1, 444, 'aaa', 40 From Dual Union All
            Select '2023-03', 2, 333, 'bbb', 30 From Dual Union All
            Select '2023-03', 3, 222, 'ccc', 20 From Dual Union All
            Select '2023-03', 4, 444, 'aaa', 40 From Dual Union All
            Select '2023-03', 5, 1111, 'ddd', 10 From Dual 
        )

字符串
主SQL

Select  A_MONTH, CUSTOMER_ID, SALES,
        LAG(SALES) Over(Partition By CUSTOMER_ID Order By CUSTOMER_ID, A_MONTH) "PREV_MONTH_SALES",
        Case  When LAG(SALES) Over(Partition By CUSTOMER_ID Order By CUSTOMER_ID, A_MONTH) > SALES
              Then 'DOWN'
              When LAG(SALES) Over(Partition By CUSTOMER_ID Order By CUSTOMER_ID, A_MONTH) < SALES
              Then 'UP'
              When LAG(SALES) Over(Partition By CUSTOMER_ID Order By CUSTOMER_ID, A_MONTH) = SALES
              Then 'SAME'
        End "MOVEMENT"
From      tbl
Order By CUSTOMER_ID, A_MONTH
--  
--    R e s u l t :
--  A_MONTH CUSTOMER_ID      SALES PREV_MONTH_SALES MOVE
--  ------- ----------- ---------- ---------------- ----
--  2023-01           1         40                      
--  2023-02           1         30               40 DOWN
--  2023-03           1         40               30 UP  
--  2023-01           2         30                      
--  2023-02           2         40               30 UP  
--  2023-03           2         30               40 DOWN
--  2023-01           3         20                      
--  2023-02           3         20               20 SAME
--  2023-03           3         20               20 SAME
--  2023-01           4         40                      
--  2023-02           4         10               40 DOWN
--  2023-03           4         40               10 UP  
--  2023-01           5         10                      
--  2023-02           5         30               10 UP  
--  2023-03           5         10               30 DOWN


LAG()分析函数也可以让你得到产品的移动。只需在选择列表中添加另一列:

Nvl(LAG(PRODUCT_NAME) Over(Partition By CUSTOMER_ID Order By CUSTOMER_ID, A_MONTH), 'n/a') || ' --> ' || PRODUCT_NAME "PRODUCT_MOVE"
-- Result fror CUSTOMER_ID = 1
--  A_MONTH CUSTOMER_ID      SALES PREV_MONTH_SALES MOVE PRODUCT_MOVE
--  ------- ----------- ---------- ---------------- ---- ------------
--  2023-01           1         40                       n/a --> aaa
--  2023-02           1         30               40 DOWN aaa --> bbb
--  2023-03           1         40               30 UP   bbb --> aaa


如果原始数据应首先求和,那么您可以创建CTE(将其命名为sales)并从CTE而不是tbl中选择。如果产品移动列在选择列表中,则应注意产品移动列-如果不在选择列表中,则可以使用以下代码:

sales AS
    ( Select A_MONTH, CUSTOMER_ID, Sum(SALES) "SALES"
      From   tbl
      Group By  A_MONTH, CUSTOMER_ID
    )

相关问题