路由器数据统计SQL脚本

一、路由器部分
    //【饼图】统计路由器在线、离线数量
    SELECT COUNT(*) AS total,
    COUNT(CASE WHEN last_heartbeat_time >= UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL -30 MINUTE)) THEN 1 ELSE NULL END) AS livecount,
    COUNT(CASE WHEN last_heartbeat_time <  UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL -30 MINUTE)) THEN 1 WHEN last_heartbeat_time IS NULL THEN 1 ELSE NULL END) AS diecount
    FROM wifi_routemap

二、Portal Page页面统计部分

    //【饼图】访问页面数量,以路由器进行排名
    SELECT `ap_mac`, COUNT(`ap_mac`) AS `total`
    FROM `wifi_status_page`
    GROUP BY `ap_mac`
    ORDER BY `total` DESC

    //【饼图】访问页面数量,以用户进行排名
    SELECT `user_mac`, COUNT(`user_mac`) AS `total`
    FROM `wifi_status_page`
    GROUP BY `user_mac`
    ORDER BY `total` DESC

    //【饼图】访问页面数量,以页面进行排名
    SELECT `page_url`, COUNT(`page_url`) AS `total`
    FROM `wifi_status_page`
    GROUP BY `page_url`
    ORDER BY `total` DESC

    //【饼图】统计访问页面数量,以分辨率进行排名,
    // 实际数据为浏览器,而非屏幕的,取消
    SELECT CONCAT(`height`, "*", `width`) AS `resolution`, COUNT(CONCAT(`height`, "*", `width`)) AS `total`
    FROM `wifi_status_page`
    GROUP BY CONCAT(`height`, "*", `width`)
    ORDER BY `total` DESC
    LIMIT 0 , 30

    //【饼图】查询手机操作系统类型(分Android、IOS、WP、BB、Symbian及其他),不包含在下面的,需要用php计算得出并放入other字段
    SELECT COUNT(id) AS total,
    COUNT(CASE WHEN user_agent LIKE  "%android%"         THEN 1 ELSE NULL END ) AS Android,
    COUNT(CASE WHEN user_agent LIKE  "%iphone%"         THEN 1 ELSE NULL END ) AS iOS,
    COUNT(CASE WHEN user_agent LIKE  "%windows phone%"     THEN 1 ELSE NULL END ) AS `Windows Phone`,
    COUNT(CASE WHEN user_agent LIKE  "%blackberry%"     THEN 1 ELSE NULL END ) AS BlackBerry,
    COUNT(CASE WHEN user_agent LIKE  "%symbian%"         THEN 1 ELSE NULL END ) AS Symbian
    FROM wifi_status_page
    
    //【饼图】查询iPhone手机型号,不包含在下面的,需要用php计算得出并放入other字段
    SELECT COUNT(CASE WHEN user_agent LIKE "%iphone%" THEN 1 ELSE NULL END) AS total,
    COUNT(CASE WHEN user_agent LIKE "%iphone%" AND CONCAT(height,"*",width) = "480*320"     THEN 1 ELSE NULL END ) AS `iPhone 3G/3GS`,
    COUNT(CASE WHEN user_agent LIKE "%iphone%" AND CONCAT(height,"*",width) = "960*640"     THEN 1 ELSE NULL END ) AS `iPhone 4/4S`,
    COUNT(CASE WHEN user_agent LIKE "%iphone%" AND CONCAT(height,"*",width) = "1136*640"     THEN 1 ELSE NULL END ) AS `iPhone 5/5C/5S`,
    COUNT(CASE WHEN user_agent LIKE "%iphone%" AND (CONCAT(height,"*",width) = "1334*750" OR CONCAT(height,"*",width) = "568*320") THEN 1 ELSE NULL END ) AS `iPhone 6/6S`,
    COUNT(CASE WHEN user_agent LIKE "%iphone%" AND CONCAT(height,"*",width) = "1920*1080"    THEN 1 ELSE NULL END ) AS `iPhone 6/6S Plus`
    FROM wifi_status_page

    //【饼图】查询浏览器类型,不包含在下面的,需要用php计算得出并放入other字段
    SELECT COUNT( id ) AS total,
    COUNT(CASE WHEN user_agent LIKE  "%QQBrowser%" THEN 1 ELSE NULL END ) AS QQBrowser,
    COUNT(CASE WHEN user_agent LIKE  "%MicroMessenger%" THEN 1 ELSE NULL END ) AS WeChat,
    COUNT(CASE WHEN user_agent LIKE  "%UCBrowser%" OR user_agent LIKE "%UCWeb%" THEN 1 ELSE NULL END ) AS UC,
    COUNT(CASE WHEN user_agent LIKE  "%Opera%" THEN 1 ELSE NULL END ) AS Opera,
    COUNT(CASE WHEN user_agent LIKE  "%Sogou%" THEN 1 ELSE NULL END ) AS Sogou,
    COUNT(CASE WHEN user_agent LIKE  "%Chrome%" THEN 1 ELSE NULL END ) AS Chrome,
    COUNT(CASE WHEN user_agent LIKE  "%360SE%" OR user_agent LIKE "%360EE%" THEN 1 ELSE NULL END ) AS `360`,
    COUNT(CASE WHEN user_agent LIKE  "%Maxthon%" THEN 1 ELSE NULL END ) AS Maxthon,
    COUNT(CASE WHEN user_agent LIKE  "%IEMobile%" THEN 1 ELSE NULL END ) AS MSIE,
    COUNT(CASE WHEN user_agent LIKE  "%dolphin%" THEN 1 ELSE NULL END ) AS dolphin,
    COUNT(CASE WHEN user_agent LIKE  "%Oupeng%" THEN 1 ELSE NULL END ) AS Oupeng,
    COUNT(CASE WHEN user_agent LIKE  "%LieBao%" AND user_agent not LIKE "%chrome%" THEN 1 ELSE NULL END ) AS LieBao,
    COUNT(CASE WHEN user_agent LIKE  "%baidu%" AND user_agent not LIKE "%chrome%" THEN 1 ELSE NULL END ) AS Baidu,
    COUNT(CASE WHEN user_agent LIKE  "%xiaomi%" AND user_agent not LIKE "%chrome%" THEN 1 ELSE NULL END ) AS XiaoMi,
    COUNT(CASE WHEN user_agent LIKE  "%Firefox%" THEN 1 ELSE NULL END ) AS Firefox
    FROM wifi_status_page

    //【饼图】查询手机厂商,不包含在下面的,需要用php计算得出并放入other字段,
    // 厂家数据太乱,ua也不全不完整,不做...........................................
    // SELECT COUNT( id ) AS total,
    // COUNT(CASE WHEN user_agent LIKE  "%iphone%"         THEN 1 ELSE NULL END ) AS Apple,
    // COUNT(CASE WHEN user_agent LIKE  "%Huawei%"         THEN 1 ELSE NULL END ) AS Huawei,
    // COUNT(CASE WHEN user_agent LIKE  "%XiaoMi%"         THEN 1 ELSE NULL END ) AS XiaoMi,
    // COUNT(CASE WHEN user_agent LIKE  "%MeiZu%"         THEN 1 ELSE NULL END ) AS MeiZu,
    // COUNT(CASE WHEN user_agent LIKE  "%Samsung%"     THEN 1 ELSE NULL END ) AS Samsung,
    // COUNT(CASE WHEN user_agent LIKE  "%VIVO%"         THEN 1 ELSE NULL END ) AS VIVO,
    // COUNT(CASE WHEN user_agent LIKE  "%Nokia%"        THEN 1 ELSE NULL END ) AS Nokia,
    // COUNT(CASE WHEN user_agent LIKE  "%nubia%"         THEN 1 ELSE NULL END ) AS nubia,
    // COUNT(CASE WHEN user_agent LIKE  "%lenovo%"         THEN 1 ELSE NULL END ) AS lenovo,
    // COUNT(CASE WHEN user_agent LIKE  "%oppo%"         THEN 1 ELSE NULL END ) AS oppo,
    // COUNT(CASE WHEN user_agent LIKE  "%zte%"         THEN 1 ELSE NULL END ) AS zte,
    // COUNT(CASE WHEN user_agent LIKE  "%qiku%"        THEN 1 ELSE NULL END ) AS qiku,
    // COUNT(CASE WHEN user_agent LIKE  "%smart%"         THEN 1 ELSE NULL END ) AS smartisam,
    // COUNT(CASE WHEN user_agent LIKE  "%coolpad%"     THEN 1 ELSE NULL END ) AS coolpad,
    // COUNT(CASE WHEN user_agent LIKE  "%oneplus%"     THEN 1 ELSE NULL END ) AS oneplus,
    // COUNT(CASE WHEN user_agent LIKE  "%letv%"         THEN 1 ELSE NULL END ) AS letv,
    // COUNT(CASE WHEN user_agent LIKE  "%htc%"         THEN 1 ELSE NULL END ) AS htc,
    // COUNT(CASE WHEN user_agent LIKE  "%tcl%"         THEN 1 ELSE NULL END ) AS tcl,
    // COUNT(CASE WHEN user_agent LIKE  "%microsoft%"     THEN 1 ELSE NULL END ) AS microsoft,
    // COUNT(CASE WHEN user_agent LIKE  "%philips%"     THEN 1 ELSE NULL END ) AS philips,
    // COUNT(CASE WHEN user_agent LIKE  "%motorola%"     THEN 1 ELSE NULL END ) AS motorola,
    // COUNT(CASE WHEN user_agent LIKE  "%haier%"         THEN 1 ELSE NULL END ) AS haier,
    // COUNT(CASE WHEN user_agent LIKE  "%hisense%"     THEN 1 ELSE NULL END ) AS hisense,
    // COUNT(CASE WHEN user_agent LIKE  "%LG%"             THEN 1 ELSE NULL END ) AS LG
    // FROM wifi_status_page

    

    //【曲线图】今天分时统计(需要用php以当前小时为基点,控制首列小时的显示个数,但在曲线图中横坐标要写满24小时,未到的时间点以空白显示)
    //当前几点就减到第几条
    SELECT td AS showhour, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac
    FROM (
        SELECT  DATE_FORMAT(now(),'%H:00')  AS td
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -1 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -2 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -3 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -4 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -5 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -6 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -7 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -8 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -9 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -10 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -11 HOUR),'%H:00')
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%H:00') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac
        FROM wifi_status_page
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = CURDATE()
        GROUP BY add_data
    )b ON a.td = b.add_data

    //【曲线图】昨天24小时分时统计,每小时统计数量,路由器数,手机数,以小时为序,
    SELECT td AS showhour, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac
    FROM (
        SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -1 HOUR),'%Y-%m-%d %H:00') as td
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -2 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -3 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -4 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -5 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -6 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -7 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -8 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -9 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -10 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -11 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -12 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -13 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -14 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -15 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -16 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -17 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -18 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -19 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -20 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -21 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -22 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -23 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -24 HOUR),'%Y-%m-%d %H:00')
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d %H:00') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac
        FROM wifi_status_page
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = date_sub(CURDATE(), INTERVAL 1 DAY) AND ap_mac = 'bc14efab747d'
        GROUP BY add_data
    )b ON a.td = b.add_data

    //【曲线图】最近7天页面访问量,每日统计数量,路由器数,手机数,以日期为序,
    SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac
    FROM (
        SELECT CURDATE( ) AS td
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -1 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -2 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -3 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -4 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -5 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -6 DAY)
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac
        FROM wifi_status_page
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') BETWEEN DATE_ADD(CURDATE(), INTERVAL -6 DAY) AND CURDATE()
        GROUP BY add_data
    )b ON a.td = b.add_data

    //【曲线图】最近30天页面访问量,每日统计数量,路由器数,手机数,以日期为序,
    SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac
    FROM (
        SELECT CURDATE( ) AS td
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -1 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -2 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -3 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -4 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -5 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -6 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -7 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -8 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -9 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -10 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -11 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -12 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -13 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -14 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -15 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -16 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -17 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -18 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -19 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -20 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -21 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -22 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -23 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -24 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -25 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -26 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -27 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -28 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -29 DAY)
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac
        FROM wifi_status_page
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') BETWEEN DATE_ADD(CURDATE(), INTERVAL -29 DAY) AND CURDATE()
        GROUP BY add_data
    )b ON a.td = b.add_data

    //【曲线图】指定开始结束日期,每日统计数量,路由器数,手机数,以日期为序,
    //开始 '2016-01-10' 结束 '2016-01-26'
    SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac
    FROM (
        SELECT '2016-01-10' AS td
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 1 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 2 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 3 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 4 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 5 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 6 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 7 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 8 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 9 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 10 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 11 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 12 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 13 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 14 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 15 DAY) 
        UNION all SELECT DATE_ADD('2016-01-10', INTERVAL 16 DAY)
    )a LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac
        FROM wifi_status_page
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') BETWEEN '2016-01-10' AND '2016-01-26'
        GROUP BY add_data
    )b ON a.td = b.add_data

    //php实现
    $sdate="2016-01-10";
    $edate="2016-01-25";

    $datetime1 = date_create($sdate);  
    $datetime2 = date_create($edate);  
    $leftday   = intval(date_diff($datetime1, $datetime2)->format('%a'));

    $sql="SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac FROM ( ";
    $sql.="SELECT '$sdate' as td ";
    for($i=1; $i <= $leftday;$i++){
        $sql.="UNION all SELECT DATE_ADD('$sdate', INTERVAL $i DAY) ";
    }
    $sql.=") a LEFT JOIN ( ";
    $sql.="SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac FROM wifi_status_page ";
    $sql.="WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') between '$sdate' and '$edate' GROUP BY  add_data ";
    $sql.=") b on a.td=b.add_data";

    echo $sql;


三、微信连WiFi统计部分

    //【饼图】微信连WiFi数量,以路由器进行排名
    SELECT `ap_mac`, COUNT(`ap_mac`) AS `total`
    FROM `wifi_status_wechat`
    GROUP BY `ap_mac`
    ORDER BY `total` DESC

    //【饼图】微信连WiFi数量,以微信公众账号进行排名,右连接显示公众账号名称
    SELECT wifi_wxwifi.wxgzh, COUNT( wifi_wxwifi.wxgzh ) AS total
    FROM wifi_status_wechat
    LEFT JOIN wifi_wxwifi ON wifi_status_wechat.shopid = wifi_wxwifi.shopId
    GROUP BY wxgzh
    ORDER BY total DESC


    //【曲线图】今天分时统计,到当前小时为止, 微信连WiFi量,每日总连接次数,路由器被连数量,公众账号被连数量,连接手机数量,连接OpenID数量
    SELECT td AS showhour, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac, COALESCE(totalappid, 0) AS totalappid, COALESCE(totalopenid, 0) AS totalopenid
    FROM (
        SELECT  DATE_FORMAT(now(),'%H:00')  AS td
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -1 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -2 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -3 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -4 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -5 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -6 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -7 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -8 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -9 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -10 HOUR),'%H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -11 HOUR),'%H:00')
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%H:00') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac, COUNT(DISTINCT appid) AS totalappid, COUNT(DISTINCT openid) AS totalopenid
        FROM wifi_status_wechat
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = CURDATE()
        GROUP BY add_data
    )b ON a.td = b.add_data

    //【曲线图】昨天24小时分时统计, 微信连WiFi量,每日总连接次数,路由器被连数量,公众账号被连数量,连接手机数量,连接OpenID数量
    SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac, COALESCE(totalappid, 0) AS totalappid, COALESCE(totalopenid, 0) AS totalopenid
    FROM (
        SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -1 HOUR),'%Y-%m-%d %H:00') as td
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -2 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -3 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -4 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -5 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -6 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -7 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -8 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -9 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -10 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -11 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -12 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -13 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -14 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -15 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -16 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -17 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -18 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -19 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -20 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -21 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -22 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -23 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -24 HOUR),'%Y-%m-%d %H:00')
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d %H:00') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac, COUNT(DISTINCT appid) AS totalappid, COUNT(DISTINCT openid) AS totalopenid
        FROM wifi_status_wechat
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
        GROUP BY add_data
    )b ON a.td = b.add_data

    //【曲线图】最近7天微信连WiFi量,每日总连接次数,路由器被连数量,公众账号被连数量,连接手机数量,连接OpenID数量
    SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac, COALESCE(totalappid, 0) AS totalappid, COALESCE(totalopenid, 0) AS totalopenid
    FROM (
        SELECT CURDATE( ) AS td
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -1 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -2 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -3 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -4 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -5 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -6 DAY)
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac, COUNT(DISTINCT appid) AS totalappid, COUNT(DISTINCT openid) AS totalopenid
        FROM wifi_status_wechat
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') BETWEEN DATE_ADD(CURDATE(), INTERVAL -6 DAY) AND CURDATE()
        GROUP BY add_data
    )b ON a.td = b.add_data

    //【曲线图】最近30天微信连WiFi量,每日总连接次数,路由器被连数量,公众账号被连数量,连接手机数量,连接OpenID数量
    SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac, COALESCE(totalappid, 0) AS totalappid, COALESCE(totalopenid, 0) AS totalopenid
    FROM (
        SELECT CURDATE( ) AS td
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -1 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -2 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -3 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -4 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -5 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -6 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -7 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -8 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -9 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -10 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -11 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -12 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -13 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -14 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -15 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -16 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -17 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -18 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -19 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -20 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -21 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -22 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -23 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -24 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -25 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -26 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -27 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -28 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -29 DAY)
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac, COUNT(DISTINCT appid) AS totalappid, COUNT(DISTINCT openid) AS totalopenid
        FROM wifi_status_wechat
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') BETWEEN DATE_ADD(CURDATE(), INTERVAL -29 DAY) AND CURDATE()
        GROUP BY add_data
    )b ON a.td = b.add_data


四、视频点播统计
    //【饼图】视频点播数量,以路由器进行排名
    SELECT `ap_mac`, COUNT(`ap_mac`) AS `total`
    FROM `wifi_status_video`
    GROUP BY `ap_mac`
    ORDER BY `total` DESC

    //【饼图】视频点播数量,以点播次数进行排名,右连接视频名称
    SELECT wifi_videolist.id, wifi_videolist.name, COUNT( wifi_videolist.name ) AS total
    FROM wifi_status_video
    LEFT JOIN wifi_videolist ON wifi_status_video.videoid = wifi_videolist.id
    GROUP BY name
    ORDER BY total DESC
    
    //【曲线图】最近24小时视频点播量,每日总连接次数,路由器被点数量,手机点播数量
    
    //【曲线图】昨天24小时视频点播量,每日总连接次数,路由器被点数量,手机点播数量
    SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac
    FROM (
        SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -1 HOUR),'%Y-%m-%d %H:00') as td
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -2 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -3 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -4 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -5 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -6 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -7 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -8 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -9 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -10 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -11 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -12 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -13 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -14 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -15 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -16 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -17 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -18 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -19 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -20 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -21 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -22 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -23 HOUR),'%Y-%m-%d %H:00')
        UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -24 HOUR),'%Y-%m-%d %H:00')
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d %H:00') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac
        FROM wifi_status_video
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
        GROUP BY add_data
    )b ON a.td = b.add_data
    
    //【曲线图】最近7天视频点播量,每日总连接次数,路由器被点数量,手机点播数量
    
    //【曲线图】最近30天视频点播量,每日总连接次数,路由器被点数量,手机点播数量
    SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac, COALESCE(totalusermac, 0) AS totalusermac
    FROM (
        SELECT CURDATE( ) AS td
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -1 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -2 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -3 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -4 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -5 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -6 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -7 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -8 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -9 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -10 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -11 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -12 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -13 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -14 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -15 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -16 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -17 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -18 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -19 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -20 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -21 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -22 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -23 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -24 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -25 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -26 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -27 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -28 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -29 DAY)
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d') add_data, COUNT(id) AS totalcount, COUNT(DISTINCT ap_mac) AS totalapmac, COUNT(DISTINCT user_mac) AS totalusermac
        FROM wifi_status_video
        WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') BETWEEN DATE_ADD(CURDATE(), INTERVAL -29 DAY) AND CURDATE()
        GROUP BY add_data
    )b ON a.td = b.add_data
    
五、APP统计
    //【饼图】APP点播数量,以路由器进行排名
    SELECT `ap_mac`, COUNT(`ap_mac`) AS `total`
    FROM `wifi_status_app`
    GROUP BY `ap_mac`
    ORDER BY `total` DESC

    //【饼图】APP下载数量,以下载次数进行排名,右连接APP名称
    SELECT wifi_applist.appId, wifi_applist.appName, COUNT( wifi_applist.appName ) AS total
    FROM wifi_status_app
    LEFT JOIN wifi_applist ON wifi_status_app.appid = wifi_applist.appId
    GROUP BY appName
    ORDER BY total DESC
        
    //【曲线图】最近N小时APP下载量,每日总连接次数,路由器被点数量,手机下载数量
    
    //【曲线图】昨天24小时APP下载量,每日总连接次数,路由器被点数量,手机点播数量
    
    //【曲线图】最近7天APP下载量,每日总连接次数,路由器被点数量,手机点播数量
    
    //【曲线图】最近30天APP下载量,每日总连接次数,路由器被点数量,手机点播数量
    
六、探针
    //【柱状图】 24小时内 用户MAC 停留小时长度及用户个数,横坐标:小时长度(1-24); 纵坐标:时间长度(1-24)
    // step 1 
        // //【条状图】 所有apmac的人流量排名,
        // SELECT `apmac`, SUM(LENGTH(clients) - LENGTH(REPLACE(clients,',',''))) AS totalcount
        // FROM `wifi_probe_status`
        // GROUP BY `apmac`
        // ORDER BY `totalcount` DESC
    
    // step 2
        // 1). 获取昨天24小时内所有mac列表,再使用php去重
        // SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d %H') add_data , clients
        // FROM wifi_probe_status
        // WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND `apmac` = 'bc14efab747d'
        // ORDER BY add_data

        // SELECT apmac, GROUP_CONCAT(clients)
        // FROM wifi_probe_status 
        // WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND `apmac` = 'bc14efab747d'
             
             
        // 2). 获取每个mac在昨天内停留的小时长度
        // SELECT MAX(FROM_UNIXTIME(add_time, '%H')) AS max, MIN(FROM_UNIXTIME(add_time, '%H')) AS min, (max - min) as interval
        // FROM wifi_probe_status 
        // WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND clients LIKE "%e82aeadc1950%" 
        
        
        
        // 3). 根据所有mac停留时间长度,算出分布图
    
    【曲线图】昨日各小时附近用户总数,指定路由器
    SELECT ap_mac as apmac, COUNT(id) AS total, 
        COUNT(CASE WHEN h00_time <> '' THEN 1 ELSE NULL END) AS `00`,
        COUNT(CASE WHEN h01_time <> '' THEN 1 ELSE NULL END) AS `01`,
        COUNT(CASE WHEN h02_time <> '' THEN 1 ELSE NULL END) AS `02`,
        COUNT(CASE WHEN h03_time <> '' THEN 1 ELSE NULL END) AS `03`,
        COUNT(CASE WHEN h04_time <> '' THEN 1 ELSE NULL END) AS `04`,
        COUNT(CASE WHEN h05_time <> '' THEN 1 ELSE NULL END) AS `05`,
        COUNT(CASE WHEN h06_time <> '' THEN 1 ELSE NULL END) AS `06`,
        COUNT(CASE WHEN h07_time <> '' THEN 1 ELSE NULL END) AS `07`,
        COUNT(CASE WHEN h08_time <> '' THEN 1 ELSE NULL END) AS `08`,
        COUNT(CASE WHEN h09_time <> '' THEN 1 ELSE NULL END) AS `09`,
        COUNT(CASE WHEN h10_time <> '' THEN 1 ELSE NULL END) AS `10`,
        COUNT(CASE WHEN h11_time <> '' THEN 1 ELSE NULL END) AS `11`,
        COUNT(CASE WHEN h12_time <> '' THEN 1 ELSE NULL END) AS `12`,
        COUNT(CASE WHEN h13_time <> '' THEN 1 ELSE NULL END) AS `13`,
        COUNT(CASE WHEN h14_time <> '' THEN 1 ELSE NULL END) AS `14`,
        COUNT(CASE WHEN h15_time <> '' THEN 1 ELSE NULL END) AS `15`,
        COUNT(CASE WHEN h16_time <> '' THEN 1 ELSE NULL END) AS `16`,
        COUNT(CASE WHEN h17_time <> '' THEN 1 ELSE NULL END) AS `17`,
        COUNT(CASE WHEN h18_time <> '' THEN 1 ELSE NULL END) AS `18`,
        COUNT(CASE WHEN h19_time <> '' THEN 1 ELSE NULL END) AS `19`,
        COUNT(CASE WHEN h20_time <> '' THEN 1 ELSE NULL END) AS `20`,
        COUNT(CASE WHEN h21_time <> '' THEN 1 ELSE NULL END) AS `21`,
        COUNT(CASE WHEN h22_time <> '' THEN 1 ELSE NULL END) AS `22`,
        COUNT(CASE WHEN h23_time <> '' THEN 1 ELSE NULL END) AS `23`
    FROM wifi_status_probe
    WHERE ap_user_date LIKE  "%20160126%" AND `ap_mac` = 'bc14efab747d'
    GROUP BY `apmac`
    ORDER BY `total` DESC
    
    

    // SELECT td AS showhour, COALESCE(totalcount, 0) AS totalcount, COALESCE(totalapmac, 0) AS totalapmac
    // FROM (
        // SELECT DATE_FORMAT(now(),'%Y-%m-%d %H:00') AS td
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -1 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -2 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -3 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -4 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -5 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -6 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -7 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -8 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL  -9 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -10 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -11 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -12 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -13 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -14 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -15 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -16 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -17 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -18 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -19 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -20 HOUR),'%Y-%m-%d %H:00')
        // ORDER BY td
    // )a
    // LEFT JOIN (
        // SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d %H:00') add_data, SUM(LENGTH(clients) - LENGTH(REPLACE(clients,',',''))) AS totalcount, COUNT(DISTINCT apmac) AS totalapmac
        // FROM wifi_probe_status
        // WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = CURDATE()
        // GROUP BY add_data
    // )b ON a.td = b.add_data

    //【曲线图】昨天24小时附近用户总数,不同路由器附近数量
    // SELECT td AS showdate, COALESCE(totalcount, 0) AS totalcount
    // FROM (
        // SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -1 HOUR),'%Y-%m-%d %H:00') as td
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -2 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -3 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -4 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -5 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -6 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -7 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -8 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL  -9 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -10 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -11 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -12 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -13 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -14 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -15 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -16 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -17 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -18 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -19 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -20 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -21 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -22 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -23 HOUR),'%Y-%m-%d %H:00')
        // UNION ALL SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -24 HOUR),'%Y-%m-%d %H:00')
        // ORDER BY td
    // )a
    // LEFT JOIN (
        // SELECT FROM_UNIXTIME(add_time, '%Y-%m-%d %H:00') add_data, SUM(LENGTH(clients) - LENGTH(REPLACE(clients,',',''))) AS totalcount
        // FROM wifi_probe_status
        // WHERE FROM_UNIXTIME(add_time, '%Y-%m-%d') = DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND `apmac` = 'bc14efab747d'
        // GROUP BY add_data
    // )b ON a.td = b.add_data
    
    【柱状图】用户访问次数分布图,每个mac的访问次数
    SELECT td AS showvisit, COALESCE(totalcount, 0) AS totalcount
    FROM (
        SELECT 1 AS td
        UNION ALL SELECT 2
        UNION ALL SELECT 3
        UNION ALL SELECT 4
        UNION ALL SELECT 5
        UNION ALL SELECT 6
        UNION ALL SELECT 7
        UNION ALL SELECT 8
        UNION ALL SELECT 9
        UNION ALL SELECT 10
        UNION ALL SELECT 11
        UNION ALL SELECT 12
        UNION ALL SELECT 13
        UNION ALL SELECT 14
        UNION ALL SELECT 15
        UNION ALL SELECT 16
        UNION ALL SELECT 17
        UNION ALL SELECT 18
        UNION ALL SELECT 19
        UNION ALL SELECT 20
        UNION ALL SELECT 21
        UNION ALL SELECT 22
        UNION ALL SELECT 23
        UNION ALL SELECT 24
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT times, COUNT(times) AS totalcount
        FROM (
            SELECT id, user_mac, ROUND(LENGTH(CONCAT_WS('', h00_time,h01_time,h02_time,h03_time,h04_time,h05_time,h06_time,h07_time,h08_time,h09_time,h10_time,h11_time,h12_time,h13_time,h14_time,h15_time,h16_time,h17_time,h18_time,h19_time,h20_time,h21_time,h22_time,h23_time))/10) as times
            FROM wifi_status_probe
            WHERE ap_user_date LIKE  "%20160128%" AND `ap_mac` = 'bc14efab7486'
            ORDER BY times DESC 
        ) b
        GROUP BY times
    )b ON a.td = b.times
    
    【柱状图】用户访问次数分布图,每个mac的访问次数
    SELECT td AS showvisit, COALESCE(totalcount, 0) AS totalcount
    FROM (
        SELECT 1 AS td
        UNION ALL SELECT 2
        UNION ALL SELECT 3
        UNION ALL SELECT 4
        UNION ALL SELECT 5
        UNION ALL SELECT 6
        UNION ALL SELECT 7
        UNION ALL SELECT 8
        UNION ALL SELECT 9
        UNION ALL SELECT 10
        UNION ALL SELECT 11
        UNION ALL SELECT 12
        UNION ALL SELECT 13
        UNION ALL SELECT 14
        UNION ALL SELECT 15
        UNION ALL SELECT 16
        UNION ALL SELECT 17
        UNION ALL SELECT 18
        UNION ALL SELECT 19
        UNION ALL SELECT 20
        UNION ALL SELECT 21
        UNION ALL SELECT 22
        UNION ALL SELECT 23
        UNION ALL SELECT 24
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT times, COUNT(times) AS totalcount
        FROM (
            SELECT id, user_mac, ROUND(LENGTH(CONCAT_WS('', h00_time,h01_time,h02_time,h03_time,h04_time,h05_time,h06_time,h07_time,h08_time,h09_time,h10_time,h11_time,h12_time,h13_time,h14_time,h15_time,h16_time,h17_time,h18_time,h19_time,h20_time,h21_time,h22_time,h23_time))/10) AS times
            FROM wifi_status_probe
            WHERE ap_user_date LIKE  "%20160222%" AND `ap_mac` = 'bc14efab7486'
            ORDER BY times DESC 
        ) b
        GROUP BY times
    )b ON a.td = b.times
    
    
    //【曲线图】最近30天指定ap MAC每天的user mac数量分布
    SELECT td AS showdate, COALESCE(totalusermac, 0) AS totalusermac
    FROM (
        SELECT CURDATE( ) AS td
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -1 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -2 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -3 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -4 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -5 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -6 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -7 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -8 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL  -9 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -10 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -11 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -12 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -13 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -14 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -15 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -16 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -17 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -18 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -19 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -20 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -21 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -22 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -23 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -24 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -25 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -26 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -27 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -28 DAY)
        UNION ALL SELECT DATE_ADD(CURDATE(), INTERVAL -29 DAY)
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT STR_TO_DATE(RIGHT(ap_user_date, 8), '%Y%m%d') add_data,  COUNT(DISTINCT user_mac) AS totalusermac
        FROM wifi_status_probe
        WHERE STR_TO_DATE(RIGHT(ap_user_date, 8), '%Y%m%d') BETWEEN DATE_ADD(CURDATE(), INTERVAL -29 DAY) AND CURDATE()  AND `ap_mac` = 'bc14efab7486'
        GROUP BY add_data
    )b ON a.td = b.add_data
    
    
    
    【柱状图】指定ap mac时,用户30天内到到访天数分布
    SELECT td AS showvisit, COALESCE(totalcount, 0) AS totalcount
    FROM (
        SELECT 1 AS td
        UNION ALL SELECT 2
        UNION ALL SELECT 3
        UNION ALL SELECT 4
        UNION ALL SELECT 5
        UNION ALL SELECT 6
        UNION ALL SELECT 7
        UNION ALL SELECT 8
        UNION ALL SELECT 9
        UNION ALL SELECT 10
        UNION ALL SELECT 11
        UNION ALL SELECT 12
        UNION ALL SELECT 13
        UNION ALL SELECT 14
        UNION ALL SELECT 15
        UNION ALL SELECT 16
        UNION ALL SELECT 17
        UNION ALL SELECT 18
        UNION ALL SELECT 19
        UNION ALL SELECT 20
        UNION ALL SELECT 21
        UNION ALL SELECT 22
        UNION ALL SELECT 23
        UNION ALL SELECT 24
        UNION ALL SELECT 25
        UNION ALL SELECT 26
        UNION ALL SELECT 27
        UNION ALL SELECT 28
        UNION ALL SELECT 29
        UNION ALL SELECT 30
        ORDER BY td
    )a
    LEFT JOIN (
        SELECT times, COUNT(times) AS totalcount
        FROM (
            SELECT `user_mac`, count(`user_mac`) as times
            FROM wifi_status_probe 
            WHERE STR_TO_DATE(RIGHT(ap_user_date, 8), '%Y%m%d') BETWEEN DATE_ADD(CURDATE(), INTERVAL -29 DAY) AND CURDATE() AND `ap_mac` = 'bc14efab7486'
            GROUP BY user_mac
        ) b
        GROUP BY times
    )b ON a.td = b.times

    
    //15天内所有记录 , 每个user mac的访问次数 
    SELECT `user_mac`, count(`user_mac`) as times
    FROM wifi_status_probe 
    WHERE STR_TO_DATE(RIGHT(ap_user_date, 8), '%Y%m%d') BETWEEN DATE_ADD(CURDATE(), INTERVAL -29 DAY) AND CURDATE() AND `ap_mac` = 'bc14efab7486'
    GROUP BY user_mac

 

posted @ 2018-08-15 15:51 方倍工作室 阅读(...) 评论(...) 编辑 收藏