我不知道如何为这个问题编写标题,但我需要的是一个返回具有特定值的N
记录的查询。
我拥有的表超过了5.2M记录
记录类似于:
我需要知道的是同一个客户在下订单之前登录网站的次数(不同会话次数)(order_number为0,除非在该会话期间下达订单)
样本数据可以(简化数据)
session | c_id | clicks | ord_num | entry | abc | 123 | 2 | 0 | 2012-08-01 00:00:00 | cde | 456 | 2 | 0 | 2012-08-01 00:00:01 | efg | 457 | 2 | 0 | 2012-08-01 00:00:02 | hij | 123 | 5 | 0 | 2012-08-01 00:00:03 | kod | 986 | 10 | 0 | 2012-08-01 00:00:04 | wdg | 123 | 2 | 9876 | 2012-08-01 00:00:05 | qwe | 123 | 2 | 0 | 2012-08-01 00:00:06 | wvr | 986 | 12 | 8656 | 2012-08-01 00:00:07 |
我想要的是一个显示类似于:
的查询entry |tot_sess|tot_cust| 1sess | 2sess | 3sess | 4sess | more4sess | order1sess | order2sess | order3sess | order4sess | orderMore4Sess | 2012-08-01 | 8 | 4 | 2 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 |
我已经能够通过以下查询获得有关会话的信息:
SELECT
t.`date_entry`,
COUNT(sess) `cust`,
SUM(sess) `session`,
COUNT(IF(sess>1,sess,NULL)) `more than once`,
COUNT(IF(sess=1,sess,NULL)) `one`,
COUNT(IF(sess=2,sess,NULL)) `two`,
COUNT(IF(sess=3,sess,NULL)) `three`,
COUNT(IF(sess=4,sess,NULL)) `four`,
COUNT(IF(sess>4,sess,NULL)) `more`,
ROUND(COUNT(IF(sess>1,sess,NULL))/COUNT(sess),2) `perc > 1`,
ROUND(COUNT(IF(sess>2,sess,NULL))/COUNT(sess),2) `perc > 2`,
ROUND(COUNT(IF(sess>3,sess,NULL))/COUNT(sess),2) `perc > 3`,
ROUND(COUNT(IF(sess>4,sess,NULL))/COUNT(sess),2) `perc > 4`
FROM
(
SELECT
`customer_id`,
COUNT(`session`) `sess`,
DATE(`date_entry`) `date_entry`
FROM `customer_activity_log`
WHERE
`clicks` > 1
AND `customer_id` > 0
AND `date_entry` > '2012-08-01'
AND subsite_id <=1
GROUP BY `date_entry`, `customer_id`
) t
GROUP BY date_entry
一旦我有了这个,我还需要以不同的方式查看数据,例如,如果客户123在2012-01-01第一次显示,然后返回15次并在2012年下订单-08-01然后再回来5次并在2012-10-12再次下订单我将需要一个不会按日期限制但只能由客户限制的查询,换句话说,限制date_entry
将被删除
我希望它有意义
答案 0 :(得分:2)
SELECT e AS entry,
SUM(sessions) AS tot_sess,
COUNT(*) AS tot_cust,
SUM(sessions=1) AS 1sess,
SUM(sessions=2) AS 2sess,
SUM(sessions=3) AS 3sess,
SUM(sessions=4) AS 4sess,
SUM(sessions>4) AS more4sess,
SUM(orders =1) AS order1sess,
SUM(orders =2) AS order2sess,
SUM(orders =3) AS order3sess,
SUM(orders =4) AS order4sess,
SUM(orders >4) AS orderMore4Sess
FROM (
SELECT b.e, b.c_id, b.sessions, COUNT(a.entry) AS orders
FROM customer_activity_log a RIGHT JOIN (
SELECT DATE(entry) AS e, c_id, COUNT(*) AS sessions,
MIN(IF(ord_num=0,NULL,entry)) AS o
FROM customer_activity_log
GROUP BY e, c_id
) b ON a.c_id = b.c_id AND DATE(a.entry) = b.e AND a.entry <= b.o
GROUP BY b.e, b.c_id
) t
在sqlfiddle上查看。