我试图获得点击/用户到我的网站的数量,为此我有他们的cookie ID和日期。但是从我到目前为止所写的查询中我得到以下错误
表
cookieid date
--------------------
001 2011-03-17
001 2011-03-17
002 2011-03-17
001 2011-03-17
001 2011-03-20
002 2011-03-21
114 2011-03-21
114 2011-03-21
计数应表明的确切方式是:
on 2011-03-17 => 4 clicks / 2 unique visitors
on 2011-03-20 => 1 click / 1 unique visitor
on 2011-03-21 => 3 clicks / 2 unique visitors
如果我在查询中确实如此
"INSERT INTO visitos SELECT cookieid,date FROM ",@tab," GROUP BY cookieid"
这通过正确计数给出了点击/用户的正确计数,但它忽略了日期,根据上表它将告诉2011-03-17 => 4次点击/ 2次唯一身份访问者但忽略了20次点击/访问,并且没有计算20次。因为cookie id正在重复
用另一种方式
"INSERT INTO visitos SELECT cookieid,date FROM ",@tab," GROUP BY date"
它会给出点击/访问的所有日期,但计数的结果是错误的, 它会告诉2011-03-17 => 4次点击/ 4位唯一身份访问者,将每次点击作为唯一身份用户,同时忽略Cookie ID
任何人都可以帮我编写SQL查询,以便通过cookie id和日期立即对此进行分组
P.S "INSERT INTO visitos SELECT cookieid,date FROM ",@tab," GROUP BY date,cookieid"
无效
存储过程
BEGIN
SET @tab = CONCAT("monitortable_",pr);
DROP TABLE IF EXISTS uniquevisitors;
CREATE TEMPORARY TABLE uniquevisitors (`cookieid` INT, `date` DATETIME);
**SET @sqlstring = CONCAT("(PLACE WHERE GROUPING QUERY SHOULD TAKE PLACE)INSERT INTO uniquevisitors SELECT cookieid,date FROM ",@tab," GROUP BY cookieid");**
PREPARE stmt FROM @sqlstring;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
DROP TABLE IF EXISTS uniquevisitorscount;
CREATE TEMPORARY TABLE uniquevisitorscount (`cnt` INT, `dat` DATETIME);
INSERT INTO uniquevisitorscount SELECT COUNT(cookieid) AS cnt ,DATE(date) AS dat FROM uniquevisitors;
DROP TABLE IF EXISTS nonuniquevisitcount;
CREATE TEMPORARY TABLE nonuniquevisitcount (`cnt` INT, `dat` DATETIME);
SET @sqlstring = CONCAT("INSERT INTO nonuniquevisitcount SELECT COUNT(cookieid) AS cnt ,DATE(date) AS dat
FROM ",@tab," GROUP BY DATE_FORMAT(date, '%y'),DATE_FORMAT(date, '%m'),DATE_FORMAT(date, '%d')");
PREPARE stmt FROM @sqlstring;
EXECUTE stmt;
SELECT un.cnt AS ucnt, nu.cnt AS cnt, un.dat AS dat FROM uniquevisitorscount un JOIN nonuniquevisitcount nu ON un.dat = nu.dat;
END$$
答案 0 :(得分:0)
试试这个
“INSERT INTO visitos date,count(cookie_id)no_of_clicks,count(distinct cookie_id)no_of_visitors FROM”,@ tab,“GROUP BY date”
答案 1 :(得分:0)
试试这个:
select x.date, sum(x.cnt) as clicks, count(*) as uniqueVisitors from
(select date, cookieid, count(*) as cnt from #temp group by date, cookieid) x
group by x.date
返回:
date clicks uniqueVisitors
2011-03-17 4 2
2011-03-20 1 1
2011-03-21 3 2
完全按照你的意愿......你只需要创建一个插入语句......