Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8634165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:39:18+00:00 2026-06-12T09:39:18+00:00

I don’t know how to writ the Title for this question, but what I

  • 0

I don’t know how to writ the Title for this question, but what I need is a query that return what is the N record with a specific value.

The table that I have is over 5.2M records

The records are similar to:

  • session (string, primary indexed)
  • customer_id (int, indexed)
  • clicks (int, indexed)
  • order_number (int, indexed)
  • date_entry (datetime, indexed)
  • many other fields

what I need to know is how many times the same customer logged into the site (different sessions) before placing an order (order_number is 0 unless an order is placed during that session)

a sample data can be (simplify data)

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  |

What I want is a query that shows something similar to:

  • entry – date entry
  • tot_sess – total number of session
  • tot_cust – total number of customers
  • 1sess – customer1 with only one session
  • 2sess – customers with 2 sessions
  • 3sess – customers with 3 sessions
  • 4sess – customers with 4 sessions
  • more4sess – customers with more than 4 sessions
  • order1sess – customers that ordered on the first session
  • order2sess – customers that ordered on the second session
  • order3sess – customers that ordered on the third session
  • order4sess – customers that ordered on the fourth session
  • orderMore4Sess – customers that ordered after the fourth session
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         |

I am already able to get the information about the session with the following query:

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

Once I had that I will also need to look at the data in a different way, for example, if customer 123 showed on the first time on 2012-01-01 and then came back 15 times and placed the order on 2012-08-01 and then came back 5 more times and placed another order on 2012-10-12 I will need a query that will not restrain by date but only by customer, in other words the restrain date_entry will be removed

I hope it makes sense

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:39:20+00:00Added an answer on June 12, 2026 at 9:39 am
    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
    

    See it on sqlfiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if this question is trivial or not. But after a couple
Don't know if this is an eclipse specific problem but whenever I declare a
don't know better title for this, but here's my code. I have class user
don't know if the title describes anything about what I'm trying to say but
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
I don't know why, but this code worked for me a month ago... maybe
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.