中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

給老婆的一篇文章

發布時間:2020-06-06 07:01:39 來源:網絡 閱讀:605 作者:caiyongji 欄目:MySQL數據庫

老婆公司有個這樣的需求:

查詢出某游戲的用戶回流信息,當用戶連續兩天登陸,則判定為2日回流,如果間隔一天登陸,則判定為3日回流,如果間隔5天登陸,則判定為7日回流。用戶數據間隔時間短為14天(固定)。

準備數據

database語法和mysql一致。

創建表:

create table user_login(
u_id int,
login_date timestamp default current_timestamp
);

插入數據:

DELIMITER //
create procedure loop_insert()
begin

declare days int;
declare usrs int;
declare mx int;
declare i int;

set days = 14;
set usrs = 30;
set mx = 500;
set i = 1;

repeat 
    insert into user_login(u_id,login_date) values (floor((RAND() * usrs)),subdate(sysdate(),(RAND() * (days+1))));
    set i = i + 1;
until i >= mx

end repeat;

end//
call loop_insert();

最開始我想到的是用group_concat,sql是這樣:

SELECT 
    u_id, group_concat(distinct DATE_FORMAT(login_date, '%Y%m%d') order by DATE_FORMAT(login_date, '%Y%m%d') desc separator '-') AS yyyymmdd
FROM
    user_login
GROUP BY u_id;

給老婆的一篇文章

后來想想日期轉換成INTEGER相減是不準確的(比如跨月),而且這樣顯示并不能解決需求。

好吧,考慮行轉列。

行轉列

行轉列,需要case when枚舉,好在日期只有14天,可以做到:

SELECT 
        u_id,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180413' THEN login_date
                ELSE ''
            END AS d20180413,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180414' THEN login_date
                ELSE ''
            END AS d20180414,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180415' THEN login_date
                ELSE ''
            END AS d20180415,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180416' THEN login_date
                ELSE ''
            END AS d20180416,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180417' THEN login_date
                ELSE ''
            END AS d20180417,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180418' THEN login_date
                ELSE ''
            END AS d20180418,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180419' THEN login_date
                ELSE ''
            END AS d20180419,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180420' THEN login_date
                ELSE ''
            END AS d20180420,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180421' THEN login_date
                ELSE ''
            END AS d20180421,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180422' THEN login_date
                ELSE ''
            END AS d20180422,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180423' THEN login_date
                ELSE ''
            END AS d20180423,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180424' THEN login_date
                ELSE ''
            END AS d20180424,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180425' THEN login_date
                ELSE ''
            END AS d20180425,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180426' THEN login_date
                ELSE ''
            END AS d20180426
    FROM
        user_login

查詢結果:

給老婆的一篇文章

按用戶合并

按用戶合并日期,去重,用max可以保證單條數據,有數據置為1無數據置為0:

SELECT 
        u_id,
            CASE
                WHEN MAX(d20180413) = '' THEN '0'
                ELSE '1'
            END AS isZ20180413,
            CASE
                WHEN MAX(d20180414) = '' THEN '0'
                ELSE '1'
            END AS isZ20180414,
            CASE
                WHEN MAX(d20180415) = '' THEN '0'
                ELSE '1'
            END AS isZ20180415,
            CASE
                WHEN MAX(d20180416) = '' THEN '0'
                ELSE '1'
            END AS isZ20180416,
            CASE
                WHEN MAX(d20180417) = '' THEN '0'
                ELSE '1'
            END AS isZ20180417,
            CASE
                WHEN MAX(d20180418) = '' THEN '0'
                ELSE '1'
            END AS isZ20180418,
            CASE
                WHEN MAX(d20180419) = '' THEN '0'
                ELSE '1'
            END AS isZ20180419,
            CASE
                WHEN MAX(d20180420) = '' THEN '0'
                ELSE '1'
            END AS isZ20180420,
            CASE
                WHEN MAX(d20180421) = '' THEN '0'
                ELSE '1'
            END AS isZ20180421,
            CASE
                WHEN MAX(d20180422) = '' THEN '0'
                ELSE '1'
            END AS isZ20180422,
            CASE
                WHEN MAX(d20180423) = '' THEN '0'
                ELSE '1'
            END AS isZ20180423,
            CASE
                WHEN MAX(d20180424) = '' THEN '0'
                ELSE '1'
            END AS isZ20180424,
            CASE
                WHEN MAX(d20180425) = '' THEN '0'
                ELSE '1'
            END AS isZ20180425,
            CASE
                WHEN MAX(d20180426) = '' THEN '0'
                ELSE '1'
            END AS isZ20180426
    FROM
        (SELECT 
        u_id,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180413' THEN login_date
                ELSE ''
            END AS d20180413,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180414' THEN login_date
                ELSE ''
            END AS d20180414,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180415' THEN login_date
                ELSE ''
            END AS d20180415,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180416' THEN login_date
                ELSE ''
            END AS d20180416,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180417' THEN login_date
                ELSE ''
            END AS d20180417,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180418' THEN login_date
                ELSE ''
            END AS d20180418,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180419' THEN login_date
                ELSE ''
            END AS d20180419,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180420' THEN login_date
                ELSE ''
            END AS d20180420,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180421' THEN login_date
                ELSE ''
            END AS d20180421,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180422' THEN login_date
                ELSE ''
            END AS d20180422,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180423' THEN login_date
                ELSE ''
            END AS d20180423,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180424' THEN login_date
                ELSE ''
            END AS d20180424,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180425' THEN login_date
                ELSE ''
            END AS d20180425,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180426' THEN login_date
                ELSE ''
            END AS d20180426
    FROM
        user_login) t0
    GROUP BY u_id

查詢結果:

給老婆的一篇文章

列轉行

使用group_concat:

SELECT 
        u_id,
            CONCAT(isZ20180413, isZ20180414, isZ20180415, isZ20180416, isZ20180417, isZ20180418, isZ20180419, isZ20180420, isZ20180421, isZ20180422, isZ20180423, isZ20180424, isZ20180425, isZ20180426) AS sumIsZ
    FROM
        (SELECT 
        u_id,
            CASE
                WHEN MAX(d20180413) = '' THEN '0'
                ELSE '1'
            END AS isZ20180413,
            CASE
                WHEN MAX(d20180414) = '' THEN '0'
                ELSE '1'
            END AS isZ20180414,
            CASE
                WHEN MAX(d20180415) = '' THEN '0'
                ELSE '1'
            END AS isZ20180415,
            CASE
                WHEN MAX(d20180416) = '' THEN '0'
                ELSE '1'
            END AS isZ20180416,
            CASE
                WHEN MAX(d20180417) = '' THEN '0'
                ELSE '1'
            END AS isZ20180417,
            CASE
                WHEN MAX(d20180418) = '' THEN '0'
                ELSE '1'
            END AS isZ20180418,
            CASE
                WHEN MAX(d20180419) = '' THEN '0'
                ELSE '1'
            END AS isZ20180419,
            CASE
                WHEN MAX(d20180420) = '' THEN '0'
                ELSE '1'
            END AS isZ20180420,
            CASE
                WHEN MAX(d20180421) = '' THEN '0'
                ELSE '1'
            END AS isZ20180421,
            CASE
                WHEN MAX(d20180422) = '' THEN '0'
                ELSE '1'
            END AS isZ20180422,
            CASE
                WHEN MAX(d20180423) = '' THEN '0'
                ELSE '1'
            END AS isZ20180423,
            CASE
                WHEN MAX(d20180424) = '' THEN '0'
                ELSE '1'
            END AS isZ20180424,
            CASE
                WHEN MAX(d20180425) = '' THEN '0'
                ELSE '1'
            END AS isZ20180425,
            CASE
                WHEN MAX(d20180426) = '' THEN '0'
                ELSE '1'
            END AS isZ20180426
    FROM
        (SELECT 
        u_id,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180413' THEN login_date
                ELSE ''
            END AS d20180413,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180414' THEN login_date
                ELSE ''
            END AS d20180414,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180415' THEN login_date
                ELSE ''
            END AS d20180415,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180416' THEN login_date
                ELSE ''
            END AS d20180416,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180417' THEN login_date
                ELSE ''
            END AS d20180417,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180418' THEN login_date
                ELSE ''
            END AS d20180418,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180419' THEN login_date
                ELSE ''
            END AS d20180419,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180420' THEN login_date
                ELSE ''
            END AS d20180420,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180421' THEN login_date
                ELSE ''
            END AS d20180421,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180422' THEN login_date
                ELSE ''
            END AS d20180422,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180423' THEN login_date
                ELSE ''
            END AS d20180423,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180424' THEN login_date
                ELSE ''
            END AS d20180424,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180425' THEN login_date
                ELSE ''
            END AS d20180425,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180426' THEN login_date
                ELSE ''
            END AS d20180426
    FROM
        user_login) t0
    GROUP BY u_id) t1

查詢結果:

給老婆的一篇文章

統計

SELECT 
    u_id,
    sumIsZ,
    CASE
        WHEN LOCATE('11', sumIsZ) > 0 THEN 2
        ELSE 0
    END AS is2Back,/*2日回流*/
    CASE
        WHEN LOCATE('101', sumIsZ) > 0 THEN 3
        ELSE 0
    END AS is3Back,/*3日回流*/
    CASE
        WHEN LOCATE('1001', sumIsZ) > 0 THEN 4
        ELSE 0
    END AS is4Back,/*4日回流*/
    CASE
        WHEN LOCATE('10001', sumIsZ) > 0 THEN 5
        ELSE 0
    END AS is5Back,/*5日回流*/
    CASE
        WHEN LOCATE('100001', sumIsZ) > 0 THEN 6
        ELSE 0
    END AS is6Back,/*6日回流*/
    CASE
        WHEN LOCATE('1000001', sumIsZ) > 0 THEN 7
        ELSE 0
    END AS is7Back/*7日回流*/
FROM
    (SELECT 
        u_id,
            CONCAT(isZ20180413, isZ20180414, isZ20180415, isZ20180416, isZ20180417, isZ20180418, isZ20180419, isZ20180420, isZ20180421, isZ20180422, isZ20180423, isZ20180424, isZ20180425, isZ20180426) AS sumIsZ
    FROM
        (SELECT 
        u_id,
            CASE
                WHEN MAX(d20180413) = '' THEN '0'
                ELSE '1'
            END AS isZ20180413,
            CASE
                WHEN MAX(d20180414) = '' THEN '0'
                ELSE '1'
            END AS isZ20180414,
            CASE
                WHEN MAX(d20180415) = '' THEN '0'
                ELSE '1'
            END AS isZ20180415,
            CASE
                WHEN MAX(d20180416) = '' THEN '0'
                ELSE '1'
            END AS isZ20180416,
            CASE
                WHEN MAX(d20180417) = '' THEN '0'
                ELSE '1'
            END AS isZ20180417,
            CASE
                WHEN MAX(d20180418) = '' THEN '0'
                ELSE '1'
            END AS isZ20180418,
            CASE
                WHEN MAX(d20180419) = '' THEN '0'
                ELSE '1'
            END AS isZ20180419,
            CASE
                WHEN MAX(d20180420) = '' THEN '0'
                ELSE '1'
            END AS isZ20180420,
            CASE
                WHEN MAX(d20180421) = '' THEN '0'
                ELSE '1'
            END AS isZ20180421,
            CASE
                WHEN MAX(d20180422) = '' THEN '0'
                ELSE '1'
            END AS isZ20180422,
            CASE
                WHEN MAX(d20180423) = '' THEN '0'
                ELSE '1'
            END AS isZ20180423,
            CASE
                WHEN MAX(d20180424) = '' THEN '0'
                ELSE '1'
            END AS isZ20180424,
            CASE
                WHEN MAX(d20180425) = '' THEN '0'
                ELSE '1'
            END AS isZ20180425,
            CASE
                WHEN MAX(d20180426) = '' THEN '0'
                ELSE '1'
            END AS isZ20180426
    FROM
        (SELECT 
        u_id,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180413' THEN login_date
                ELSE ''
            END AS d20180413,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180414' THEN login_date
                ELSE ''
            END AS d20180414,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180415' THEN login_date
                ELSE ''
            END AS d20180415,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180416' THEN login_date
                ELSE ''
            END AS d20180416,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180417' THEN login_date
                ELSE ''
            END AS d20180417,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180418' THEN login_date
                ELSE ''
            END AS d20180418,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180419' THEN login_date
                ELSE ''
            END AS d20180419,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180420' THEN login_date
                ELSE ''
            END AS d20180420,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180421' THEN login_date
                ELSE ''
            END AS d20180421,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180422' THEN login_date
                ELSE ''
            END AS d20180422,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180423' THEN login_date
                ELSE ''
            END AS d20180423,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180424' THEN login_date
                ELSE ''
            END AS d20180424,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180425' THEN login_date
                ELSE ''
            END AS d20180425,
            CASE DATE_FORMAT(login_date, '%Y%m%d')
                WHEN '20180426' THEN login_date
                ELSE ''
            END AS d20180426
    FROM
        user_login) t0
    GROUP BY u_id) t1) t2
;

查詢結果:

給老婆的一篇文章

結語

老婆,明天照著這個邏輯寫就可以了,么么噠。(2018年4月26日23點50分)

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

炎陵县| 郑州市| 喀什市| 鄂州市| 扶沟县| 朝阳县| 天津市| 镇远县| 泌阳县| 禄丰县| 靖安县| 台南县| 石城县| 威远县| 泽普县| 文登市| 上蔡县| 普安县| 梁河县| 集贤县| 乐平市| 安乡县| 桂阳县| 海南省| 灵山县| 襄樊市| 永定县| 梅河口市| 依安县| 五家渠市| 江孜县| 胶南市| 乌兰察布市| 贡嘎县| 绥棱县| 亚东县| 阳泉市| 上虞市| 皋兰县| 保山市| 肃南|