使用子查询计算的复杂查询

时间:2017-06-28 15:55:21

标签: mysql sql

所以,我有以下问题:

    select count(distinct(r.user_id)), p.id_store, u.country, r.price_usd
from receipts as r inner join products as p on p.id=r.product_id inner join customers as c on r.customer_id=c.id inner join users as u on u.id=r.user_id
where month(r.expiration_at)=6
and year(r.expiration_at)=2017
and r.status='trial'
and p.platform not in ('manual')
and c.tester=0
group by 2,3,4

此查询将显示在特定月份到期的收据。为了预测每张收据的价值(请注意我们只收取试用状态的收据),我们应该乘以历史转换率试用到活动状态。 (即如果我们收到100美元的收据,预期的RR为50%,则收据价值50美元)

只有一个查询,我想在上一个查询中添加更多2列:

'RR' ratio, RR= sum(active) / sum(active + unpaid).

请注意,状态是一个可以包含多个值的列(“有效”,“未付款”,“试用”,“已取消”和“待定”)

然后,最后一步是让另一列成为:

count(distinct r.user_id) * price_usd * RR

我需要按比例('RR')按国家/地区细分,以及id_store(即产品名称)。

要照常采取此RR,对于上个月的数据,我的查询将如下所示:

select count(distinct r.user_id), r.status, u.country
from receipts as r inner join users as u on u.id=r.user_id
where month(r.created_at)=5
and year(r.created_ay)=2017
group by 2,3

然后我通常必须将数据带入电子表格并使用数据透视表计算RR。

所以这就是我期望得到的:

Count  id_store          country    price       RR       Expected Revenue
100   trial_subscription  Brazil    10$         50%      500$
200   trial_subscription  France    20$         60%      2.400$ 

这是收据表的示例:

user_id status  price_usd   store_key
1172637 active  89.99   lk3.ios.trial.12m.90
1203314 trial   89.99   lk3.ios.trial.12m.90
1172250 unpaid  89.99   lk3.ios.trial.12m.90

RR将是:count(active)/ sum(active + unpaid)

(实际支付的人除以付费的人加上没有付费的人)。

请记住,我还希望按国家/地区进行细分(不在收据表中)。但是你可以在我发布的第一个查询中看到逻辑。

更新

这是查询的样子。问题在于计算每个国家的一个RR而不是每个国家和产品。感谢@DavidLee为您的伟大贡献。

SELECT
   r.count AS count,
   r.id_store AS id_store,
   u.country AS country,
   r.price_usd AS price_usd,
   a.count / (a.count + u.count) AS RR,
   r.price_usd * r.count * (a.count / (a.count + u.count)) AS ExpectedRevenue

FROM
(
   select count(distinct(r.user_id)) AS count, p.id_store, u.country, r.price_usd
   from receipts as r inner join products as p on p.id=r.product_id inner join customers as c on r.customer_id=c.id inner join users as u on u.id=r.user_id
   where month(r.expiration_at)=6
   and year(r.expiration_at)=2017
   and r.status='trial'
   and p.platform not in ('manual')
   and c.tester=0
   group by 2,3,4
) AS r

LEFT JOIN
(
   select count(distinct r.user_id) AS count, r.status, u.country
   from receipts as r inner join users as u on u.id=r.user_id
   where month(r.created_at)=5 AND r.status = 'active'
   and year(r.created_at)=2017
   group by 2,3
) AS a
ON r.country = a.country

LEFT JOIN
(
   select count(distinct r.user_id) AS count, r.status, u.country
   from receipts as r inner join users as u on u.id=r.user_id
   where month(r.created_at)=5 AND r.status = 'unpaid'
   and year(r.created_at)=2017
   group by 2,3
) AS u
ON r.country = u.country

1 个答案:

答案 0 :(得分:1)

不确定这是否是正确的答案,因为我正在使用有限的信息,但请试一试,让我知道它是如何出来的。

我接受了你的RR查询并创建了两个RR查询,一个用于活动查询,一个用于未付费查询。然后,我将其加入到您的基本查询中,并计算出RRExpectedRevenue

SELECT
    r.count AS count,
    r.id_store AS id_store,
    r.country AS country,
    r.price AS price_usd,
    a.count / (a.count + u.count) AS RR,
    r.count * r.price * (a.count / (a.count + u.count)) AS ExpectedRevenue

FROM
(
    select count(distinct(r.user_id)) AS count, p.id_store, u.country, r.price_usd
    from receipts as r 
        inner join products as p on p.id=r.product_id 
        inner join customers as c on r.customer_id=c.id 
        inner join users as u on u.id=r.user_id
    where month(r.expiration_at)=6
        and year(r.expiration_at)=2017
        and r.status='trial'
        and p.platform not in ('manual')
        and c.tester=0
    group by 2,3,4
) AS r

LEFT JOIN
(
    select count(distinct r.user_id) AS count, u.country, p.id_store
    from receipts as r 
        inner join products as p on p.id=r.product_id
        inner join users as u on u.id=r.user_id
    where month(r.created_at)=5
        and year(r.created_ay)=2017
        AND r.status = "active"
    group by 2,3
) AS a
ON r.country = a.country AND r.id_store = a.id_store

LEFT JOIN
(
    select count(distinct r.user_id) AS count, u.country, p.id_store
    from receipts as r 
        inner join products as p on p.id=r.product_id
        inner join users as u on u.id=r.user_id
    where month(r.created_at)=5 
        and year(r.created_ay)=2017
        AND r.status = "unpaid"
    group by 2,3
) AS u
ON r.country = u.country AND r.id_store = u.id_store

编辑:修复预期收入计算并将产品id_store添加到RR计算。 RR查询现在按国家和id_store加入。

相关问题