比较 table 两个多月内客户的演变

Compare evolution of clients between more than two month on a table

我有一个数据库,每月都会在其中输入新记录,我可以在其中查看每个提供商拥有的客户数量和客户名称。大约有 1500 家客户和 17 家供应商。可以禁用老客户,也可以每月注册新客户。每个客户只能有一个供应商,但每个月他们都可以更换供应商。我正在尝试执行一个查询 returns 谁每个月都更换了提供者(客户的姓名、与他们在一起的人以及与他们在一起的人)。

我查询中的所有内容都运行良好,但我希望它比较所有月份,而不仅仅是我在 table 1 和 table 2 上写的两个月。

我开发了以下代码:

WITH
-- CREATE TABLE 1 FROM A GENERAL DATABASE WITH CURRENT MONTH INFO
t1 AS (
  SELECT 
        Transaction_date,
        Provider,
        Customer_ID,
        Customer_Name,
        CONCAT(Customer_ID, Provider) AS ID1,
        Demand
    FROM MyDataBase
    WHERE YEAR(Transaction_date)=2020
    AND MONTH(Transaction_date)=10
), 

-- CREATE TABLE 2 FROM A GENERAL DATABASE WITH LAST MONTH INFO
t2 AS (
  SELECT 
        Transaction_date AS Transaction_date2,
        Provider AS Provider2,
        Customer_Code AS Customer_ID2,
        Customer_Name AS Customer_Name2,
        CONCAT(Customer_ID, Provider) AS ID2,
        Demand AS Demand2
    FROM MyDataBase
    WHERE YEAR(Transaction_date)=2020
    AND MONTH(Transaction_date)=09
    
),


-- t3: IDENTIFY WHO CHANGED

t3 AS (
    SELECT 
        Transaction_date2,
        Provider2,
        Customer_ID2,
        Customer_Name2,
        Demand2
    FROM t1
    RIGHT JOIN t2
    ON t2.ID2 = t1.ID1
    WHERE demand IS null
),

-- t4: RETURNS THE CUSTOMERS THAT CHANGED OF PRIVIDER IN AN ORGANIZED TABLE

T4 AS (
    SELECT 
        Transacion_date,
        Provider2 AS 'Last_Provider',   
        Provider AS 'Actual_Privder',
        Customer_ID,
        Customer_Name,
        Demand
    FROM t3
        LEFT JOIN t1
        ON t1.Customer_ID = t5.Customer_ID2
        WHERE Demand IS NOT NULL 
        AND Transacion_date IS NOT NULL
    )
    

SELECT * FROM t4

数据库样本

 Month        & Provider & Client & Demand \
 2020-01-01   & A        & 1      & 50     \
 2020-01-01   & A        & 2      & 40     \
 2020-01-01   & B        & 3      & 55     \
 2020-01-01   & B        & 4      & 70     \
 2020-02-01   & A        & 1      & 55     \
 2020-02-01   & B        & 2      & 34     \
 2020-02-01   & B        & 3      & 67     \
 2020-02-01   & B        & 4      & 89     \
 2020-03-01   & A        & 1      & 78     \
 2020-03-01   & A        & 2      & 89     \
 2020-03-01   & A        & 3      & 76     \
 2020-03-01   & B        & 4      & 45    

如您在示例中所见

1 月:有 4 个客户。 2 月:客户 #2 转投供应商 B。 3 月:客户 #2 和 #3 被转移到提供商 A。

I am trying to perform a query that returns Who changed provider each month (client's name, who they were with, and with whom they're).

如果你是运行 MySQL 8.0,你可以使用lag()来获取每个客户的“以前”的提供者。然后剩下要做的就是过滤提供商更改的月份:

select *
from (
    select t.*,
        lag(provider) over(partition by customer_code order by transaction_date) as previous_provider
    from mytable t
) t
where provider <> previous_provider

在早期版本中,您可以使用子查询模拟 window 函数:

select *
from (
    select t.*,
        (
            select t1.provider 
            from mytable t1 
            where t1.customer_code = t.customer_code and t1.transaction_date < t.transaction_date 
            order by t1.transaction_date desc limit 1
        ) as previous_provider
    from mytable t
) t
where provider <> previous_provider