将一个矩阵除以另一个过日期

Posted

技术标签:

【中文标题】将一个矩阵除以另一个过日期【英文标题】:Divide one matrix by the other over dates 【发布时间】:2021-10-04 22:43:32 【问题描述】:

我有一个每月支付给平台的数据库,该平台已初始化 orders.engagement = 'M' 或已经每月 orders.engagement = 'R',(您可以在后面的问题中看到 mysql Fiddle)。目前,大约 74% 的启动已转换为每月付款。我想知道这个月费率是多少,因为在过去的几个月里我只得到 45%:

然后,我要计算计算两者关系的SQL:

Number of initiations resulting in at least one payment in the following months
                          ---- divided ----
                        Number of initiations

我想为每个月做这个比率。但在以下测试中,我认为当我按月进行 GROUP BY 时,我会失去“接下来的几个月中至少一笔付款”。

这是它返回在接下来的几个月中导致至少一次付款的启动数量的操作:

SELECT orders_1.date, COUNT(DISTINCT orders_1.`gxo_vads_subscription`) count_first_recurrent
        FROM `orders` AS orders_1
        INNER JOIN `orders` AS orders_2 ON orders_1.`gxo_vads_subscription` = orders_2.`gxo_vads_subscription`
        # Donde la transacion a logrado
        WHERE `orders_1`.`status` = 2
            AND `orders_1`.`engagement` = 'R' 
            AND orders_2.`engagement` = 'M'
    GROUP BY YEAR(orders_1.date), MONTH(orders_1.date)

返回:

...
25/5/2021, 1:10    2 651
7/6/2021, 1:10     2 443

here is the Fiddle related to an extract of the data base

这是每个 mez 初始化的所有交易:

SELECT orders_3.date, COUNT(DISTINCT orders_3.`gxo_vads_subscription`) count_initialization
        FROM `orders` AS orders_3
        -- INNER JOIN `orders` AS orders_4 ON orders_3.`gxo_vads_subscription` = orders_4.`gxo_vads_subscription`
        # Où la transaction a réussi
        WHERE orders_3.`status` = 2
            # qui soit après la campagne de pâques
            AND orders_3.`engagement` = 'M' 
            AND `orders_3`.`gxo_means` = 'CB'
        GROUP BY YEAR(orders_3.date), MONTH(orders_3.date)

然后它返回:

...
1/5/2021, 8:04    86
1/6/2021, 4:54    36

Aqui esta el resultado y el Fiddle relacionado

有一个问题:count_initialization/count_first_recurrent除法返回的结果与平均值相比似乎太高了:

...
1/4/2021, 1:10    664,25
11/5/2021, 1:10   662,75
7/6/2021, 1:10    610,75

这意味着导致至少一次付款的启动次数是启动次数的 6 倍,这是不可能的。

这里是获取0,74平均值的脚本:

SELECT (count_first_recurrent/count_initialization) FROM
    (
        (SELECT count(distinct `orders`.`gxo_vads_subscription`) count_first_recurrent
        FROM `orders`
        WHERE `orders`.`engagement` = 'R'
           AND `orders`.`status` = 2 AND `orders`.`gxo_means` = 'CB') as a,
           
        (SELECT count(distinct `orders`.`gxo_vads_subscription`) count_initialization
        FROM `orders`
        WHERE `orders`.`engagement` = 'M'
           AND `orders`.`status` = 2 AND `orders`.`gxo_means` = 'CB') AS b
   )

【问题讨论】:

【参考方案1】:

根据您的描述,一个更简单的整体查询是:

SELECT COUNT(DISTINCT CASE WHEN o.engagement = 'R' THEN o.gxo_vads_subscription END) as count_first_recurrent,
       COUNT(DISTINCT CASE WHEN o.engagement = 'M' THEN o.gxo_vads_subscription END) as count_initialization
FROM `orders` o
WHERE o.status = 2 AND o.gxo_means = 'CB';

您可以进行除法,但这会计算原始计数。

您可以轻松地按月展开:

SELECT YEAR(o.date), MONTH(o.date),
       COUNT(DISTINCT CASE WHEN o.engagement = 'R' THEN o.gxo_vads_subscription END) as count_first_recurrent,
       COUNT(DISTINCT CASE WHEN o.engagement = 'M' THEN o.gxo_vads_subscription END) as count_initialization
FROM `orders` o
WHERE o.status = 2 AND o.gxo_means = 'CB'
GROUP BY YEAR(o.date), MONTH(o.date);

这似乎可以回答您的问题,但我不确定结果是否有用。所有初始化都在同一个月内。

【讨论】:

很高兴简化了我的查询,谢谢!但我很担心:在订单中,我有两种行: - 每月捐赠的初始化交易(o.engagement = 'M') - 以及这些捐赠的实际交易。 (o.engagement = 'R') 因此,根据您的命令,我是否有在本月初始化 (o.commitment = M) 之前开始的每月承诺 (o.commitment = M) 的情况?

以上是关于将一个矩阵除以另一个过日期的主要内容,如果未能解决你的问题,请参考以下文章

如何将矩阵的每一行除以R中的向量元素

将其他日期的数据除以某一特定日期的数据

Eigen - 将每个(稀疏)矩阵行除以其对应的对角线元素

将一个 DateInterval 除以另一个

Oracle SQL - 将一个多行查询列除以另一个

矩阵求逆