1. 程式人生 > >路由器資料統計SQL指令碼

路由器資料統計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 -