sql中每天的总转换率

wqsoz72f  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(352)

根据下表
用户信息:
用户id
日期
用户访问:
用户id
交易日
采购金额
转换率:在当天到达的用户总数中,有多少用户在当天进行了交易。
这张table应该有

------------------------------
 Date    |   converstion_rate
------------------------------
2020-7-9     30%

我尝试过这个,但它只返回一行:

select
  date(A.date) as date 
  , count(distinct B.user_id) / 
      count(distinct A.user_id) as conversion
from dau A join rev B
  on A.user_id = B.user_id
group by 1
nbewdwxp

nbewdwxp1#

保存日期和时间始终作为正确的日期,所有其他都必须转换。
下面的查询只在有事务发生时显示行,当您还需要显示日期时,如果没有事务,您必须切换到left join

CREATE TABLE User_info (
 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
 Date VARCHAR(255),
 User_ID VARCHAR(255)
);
INSERT INTO User_info
 (Date,User_ID)
VALUES
 ('7/9/2020','1060'),
 ('7/9/2020','1069'),
 ('7/9/2020','1089'),
 ('7/9/2020','1125'),
 ('7/9/2020','1232'),
 ('7/9/2020','1264'),
 ('7/9/2020','1269'),
 ('7/9/2020','1279'),
 ('7/9/2020','1292'),
 ('7/9/2020','2008');

CREATE TABLE User_trascations(
 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
 Date_Time VARCHAR(255),
 User_ID INT,
 Revenue FLOAT
);
INSERT INTO User_trascations
 (Date_Time,User_ID,Revenue)
VALUES
 ('2020-7-9 9.20.56.281000000 ',1269,13.993),
 ('2020-7-9 8.16.52.214000000 ',1279,1.393),
 ('2020-7-9 7.17.49.895000000 ',1232,1.393),
 ('2020-7-9 6.54.6.846000000 ',1264,1.393),
 ('2020-7-9 6.46.51.135000000 ',1232,6.993),
 ('2020-7-9 6.41.32.654000000 ',1125,1.393),
 ('2020-7-9 5.58.37.464000000 ',1279,6.993),
 ('2020-7-9 4.35.20.509000000 ',1279,7.2779448),
 ('2020-7-9 4.19.26.254000000 ',1232,3.493),
 ('2020-7-9 23.34.20.47000000 ',1232,3.493),
 ('2020-7-9 22.53.19.828000000 ',1232,6.993),
 ('2020-7-9 21.38.14.723000000 ',1264,6.993),
 ('2020-7-9 20.38.50.284000000 ',1264,6.993),
 ('2020-7-9 20.34.34.235000000 ',1279,6.993),
 ('2020-7-9 18.57.47.459000000 ',1264,3.493),
 ('2020-7-9 18.57.2.383000000 ',1279,1.393),
 ('2020-7-9 18.38.20.297000000 ',1279,1.393),
 ('2020-7-9 18.29.26.471000000 ',1089,7.214527773),
 ('2020-7-9 18.27.53.275000000 ',1232,1.393),
 ('2020-7-9 18.26.6.499000000 ',1264,6.993),
 ('2020-7-9 18.22.0.159000000 ',1089,1.393),
 ('2020-7-9 17.0.1.171000000 ',1232,13.993),
 ('2020-7-9 16.29.3.477000000 ',1232,1.393),
 ('2020-7-9 15.7.23.893000000 ',1264,3.493),
 ('2020-7-9 15.51.53.701000000 ',1279,3.486),
 ('2020-7-9 13.48.47.687000000 ',1264,3.493);
SELECT t1.date_t1, t2.count_t2/t1.count_t1
FROM (SELECT COUNT(DISTINCT User_ID) count_t1 ,STR_TO_DATE(`Date`,'%m/%d/%y') date_t1 FROm User_info GROUP BY STR_TO_DATE(`Date`,'%m/%d/%y')) t1
 INNER JOIN (SELECT COUNT(DISTINCT User_ID) count_t2,DATE(`Date_Time`) date_t2 FROm User_trascations GROUP BY DATE(`Date_Time`)) t2
 ON t1.date_t1 = t2.date_t2
date_t1    | t2.count_t2/t1.count_t1
:--------- | ----------------------:
2020-07-09 |                  0.6000
SELECT COUNT(DISTINCT User_ID) count_t1 ,STR_TO_DATE(`Date`,'%m/%d/%y') date_t1 FROm User_info GROUP BY STR_TO_DATE(`Date`,'%m/%d/%y')
count_t1 | date_t1   
-------: | :---------
      10 | 2020-07-09
SELECT COUNT(DISTINCT User_ID) count_t2,DATE(`Date_Time`) date_t2 FROm User_trascations GROUP BY DATE(`Date_Time`)
count_t2 | date_t2   
-------: | :---------
       6 | 2020-07-09
SELECT STR_TO_DATE(`Date`,'%m/%d/%y') FROM User_info;
| STR_TO_DATE(`Date`,'%m/%d/%y') |
| :----------------------------- |
| 2020-07-09                     |
| 2020-07-09                     |
| 2020-07-09                     |
| 2020-07-09                     |
| 2020-07-09                     |
| 2020-07-09                     |
| 2020-07-09                     |
| 2020-07-09                     |
| 2020-07-09                     |
| 2020-07-09                     |

db<>在这里摆弄
按月和按年

SELECT 
    t1.date_t1 AS Date,
    total_users_on_a_day,
    Users_who_made_pruchase
FROM
    (SELECT 
        COUNT(DISTINCT User_ID) total_users_on_a_day,
            DATE_FORMAT(STR_TO_DATE(Date, '%m/%d/%y'),'%Y-%m') date_t1
    FROM
        User_info
    GROUP BY DATE_FORMAT(STR_TO_DATE(Date, '%m/%d/%y'),'%Y-%m')) t1
        INNER JOIN
    (SELECT 
        COUNT(DISTINCT User_ID) Users_who_made_pruchase,
            DATE_FORMAT(Date_Time,'%Y-%m') date_t2
    FROM
        User_trascations
    GROUP BY DATE_FORMAT(Date_Time,'%Y-%m') WITH ROLLUP) t2 ON t1.date_t1 = t2.date_t2

相关问题