SQL how to count the number of events that happened over a time period?












-3















I need to know how a phone call affects our customers



so after I give a customer a phone call I need to see if they logged in to their account within 1 day after, or within 7 days after or within 14 days after the call.



How do I even go about using the datediff in this case?



enter image description here










share|improve this question























  • Show your table definitions, sample rows, and expected output using that data.

    – Shawn
    Nov 13 '18 at 3:29











  • that is the expected output of the data, the columns i have are customer_id, call_time, and logged_in_time.

    – Gus
    Nov 13 '18 at 3:39











  • Can't help without knowing what we have to work with.

    – Shawn
    Nov 13 '18 at 4:35
















-3















I need to know how a phone call affects our customers



so after I give a customer a phone call I need to see if they logged in to their account within 1 day after, or within 7 days after or within 14 days after the call.



How do I even go about using the datediff in this case?



enter image description here










share|improve this question























  • Show your table definitions, sample rows, and expected output using that data.

    – Shawn
    Nov 13 '18 at 3:29











  • that is the expected output of the data, the columns i have are customer_id, call_time, and logged_in_time.

    – Gus
    Nov 13 '18 at 3:39











  • Can't help without knowing what we have to work with.

    – Shawn
    Nov 13 '18 at 4:35














-3












-3








-3








I need to know how a phone call affects our customers



so after I give a customer a phone call I need to see if they logged in to their account within 1 day after, or within 7 days after or within 14 days after the call.



How do I even go about using the datediff in this case?



enter image description here










share|improve this question














I need to know how a phone call affects our customers



so after I give a customer a phone call I need to see if they logged in to their account within 1 day after, or within 7 days after or within 14 days after the call.



How do I even go about using the datediff in this case?



enter image description here







sql sqlite sum window-functions datediff






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 2:52









GusGus

535




535













  • Show your table definitions, sample rows, and expected output using that data.

    – Shawn
    Nov 13 '18 at 3:29











  • that is the expected output of the data, the columns i have are customer_id, call_time, and logged_in_time.

    – Gus
    Nov 13 '18 at 3:39











  • Can't help without knowing what we have to work with.

    – Shawn
    Nov 13 '18 at 4:35



















  • Show your table definitions, sample rows, and expected output using that data.

    – Shawn
    Nov 13 '18 at 3:29











  • that is the expected output of the data, the columns i have are customer_id, call_time, and logged_in_time.

    – Gus
    Nov 13 '18 at 3:39











  • Can't help without knowing what we have to work with.

    – Shawn
    Nov 13 '18 at 4:35

















Show your table definitions, sample rows, and expected output using that data.

– Shawn
Nov 13 '18 at 3:29





Show your table definitions, sample rows, and expected output using that data.

– Shawn
Nov 13 '18 at 3:29













that is the expected output of the data, the columns i have are customer_id, call_time, and logged_in_time.

– Gus
Nov 13 '18 at 3:39





that is the expected output of the data, the columns i have are customer_id, call_time, and logged_in_time.

– Gus
Nov 13 '18 at 3:39













Can't help without knowing what we have to work with.

– Shawn
Nov 13 '18 at 4:35





Can't help without knowing what we have to work with.

– Shawn
Nov 13 '18 at 4:35












2 Answers
2






active

oldest

votes


















0














Assuming that;
- the table is named logininfo and
- that call_time and logged_in_time are stored according to YYYY-MM-DD HH:MM (or one of the supported formats as per Date And Time Functions)



Then I believe that the following will do what you want :-



WITH 
CTE1 AS (
SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
FROM logininfo
WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
AND customer_id = 1 -- must be for this customer
AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
)

SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
(SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
(SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
(SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
;



  • You would apply the appropriate customer_id and call_time to the where clause.

  • Your expected results don't appear to comply with so after I give a customer a phone call I need to see if they logged in to their account within 1 day after, or within 7 days after or within 14 days after the call. as within 7 days after would include the number of calls made within 1 day after and so on. If this isn;t the case it's just a matter of changing the last 3 WHERE clauses appropriately.


Assuming a table populated as :-



enter image description here



The above will result in :-



enter image description here



The following is the full testing script used :-



DROP TABLE IF EXISTS logininfo;
CREATE TABLE IF NOT EXISTS logininfo (customer_id INTEGER, call_time TEXT, logged_in_time TEXT);
INSERT INTO logininfo VALUES
(1,'2018-01-01 11:30','2018-01-02 10:00'),
(1,'2018-01-01 11:30','2018-01-03 10:00'),
(1,'2018-01-01 11:30','2018-01-04 10:00'),
(1,'2018-01-01 11:30','2018-01-05 10:00'),
(1,'2018-01-01 11:30','2018-01-06 10:00'),
(1,'2018-01-01 11:30','2018-01-07 10:00'),
(1,'2018-01-01 11:30','2018-01-08 10:00'),
(1,'2018-01-01 11:30','2018-01-15 10:00'),
(1,'2018-01-01 11:30','2018-01-16 10:00'),
(1,'2018-01-01 11:30','2018-01-17 10:00'),

(1,'2018-02-01 11:30','2018-02-14 10:00'),
(1,'2018-02-01 11:30','2018-02-15 10:00'),
(1,'2018-02-01 11:30','2018-02-16 10:00'),
(1,'2018-02-01 11:30','2018-02-17 10:00'),
(1,'2018-02-01 11:30','2018-02-18 10:00'),
(1,'2018-02-01 11:30','2018-02-19 10:00'),

(2,'2018-01-01 11:30','2018-01-02 10:00'),
(2,'2018-01-01 11:30','2018-01-03 10:00'),
(2,'2018-01-01 11:30','2018-01-04 10:00'),
(2,'2018-01-01 11:30','2018-01-05 10:00'),
(2,'2018-01-01 11:30','2018-01-15 10:00'),
(2,'2018-01-01 11:30','2018-01-16 10:00'),
(2,'2018-01-01 11:30','2018-01-17 10:00')
;

SELECT * FROM logininfo;

WITH
CTE1 AS (
SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
FROM logininfo
WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
AND customer_id = 1 -- must be for this customer
AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
)

SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
(SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
(SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
(SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
;


Note this doesn't use datediff rather the date difference is determined in the query.






share|improve this answer

































    0














    You can make use of Conditional Sums in SQL Aggregate methods



    SELECT SUM(CASE WHEN time > 1 AND time <= 7 THEN 1 ELSE 0 END)   AS LoggedInAfter1day,
    SUM(CASE WHEN time > 7 AND time <= 14 THEN 1 ELSE 0 END ) AS LoggedInAfter7day,
    SUM(CASE WHEN time > 14 THEN 1 ELSE 0 END ) AS LoggedInAfter14day
    FROM ( SELECT (logged_in_time - call_time) As time FROM customers ) AS c





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273088%2fsql-how-to-count-the-number-of-events-that-happened-over-a-time-period%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Assuming that;
      - the table is named logininfo and
      - that call_time and logged_in_time are stored according to YYYY-MM-DD HH:MM (or one of the supported formats as per Date And Time Functions)



      Then I believe that the following will do what you want :-



      WITH 
      CTE1 AS (
      SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
      FROM logininfo
      WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
      AND customer_id = 1 -- must be for this customer
      AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
      )

      SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
      (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
      (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
      (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
      ;



      • You would apply the appropriate customer_id and call_time to the where clause.

      • Your expected results don't appear to comply with so after I give a customer a phone call I need to see if they logged in to their account within 1 day after, or within 7 days after or within 14 days after the call. as within 7 days after would include the number of calls made within 1 day after and so on. If this isn;t the case it's just a matter of changing the last 3 WHERE clauses appropriately.


      Assuming a table populated as :-



      enter image description here



      The above will result in :-



      enter image description here



      The following is the full testing script used :-



      DROP TABLE IF EXISTS logininfo;
      CREATE TABLE IF NOT EXISTS logininfo (customer_id INTEGER, call_time TEXT, logged_in_time TEXT);
      INSERT INTO logininfo VALUES
      (1,'2018-01-01 11:30','2018-01-02 10:00'),
      (1,'2018-01-01 11:30','2018-01-03 10:00'),
      (1,'2018-01-01 11:30','2018-01-04 10:00'),
      (1,'2018-01-01 11:30','2018-01-05 10:00'),
      (1,'2018-01-01 11:30','2018-01-06 10:00'),
      (1,'2018-01-01 11:30','2018-01-07 10:00'),
      (1,'2018-01-01 11:30','2018-01-08 10:00'),
      (1,'2018-01-01 11:30','2018-01-15 10:00'),
      (1,'2018-01-01 11:30','2018-01-16 10:00'),
      (1,'2018-01-01 11:30','2018-01-17 10:00'),

      (1,'2018-02-01 11:30','2018-02-14 10:00'),
      (1,'2018-02-01 11:30','2018-02-15 10:00'),
      (1,'2018-02-01 11:30','2018-02-16 10:00'),
      (1,'2018-02-01 11:30','2018-02-17 10:00'),
      (1,'2018-02-01 11:30','2018-02-18 10:00'),
      (1,'2018-02-01 11:30','2018-02-19 10:00'),

      (2,'2018-01-01 11:30','2018-01-02 10:00'),
      (2,'2018-01-01 11:30','2018-01-03 10:00'),
      (2,'2018-01-01 11:30','2018-01-04 10:00'),
      (2,'2018-01-01 11:30','2018-01-05 10:00'),
      (2,'2018-01-01 11:30','2018-01-15 10:00'),
      (2,'2018-01-01 11:30','2018-01-16 10:00'),
      (2,'2018-01-01 11:30','2018-01-17 10:00')
      ;

      SELECT * FROM logininfo;

      WITH
      CTE1 AS (
      SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
      FROM logininfo
      WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
      AND customer_id = 1 -- must be for this customer
      AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
      )

      SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
      (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
      (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
      (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
      ;


      Note this doesn't use datediff rather the date difference is determined in the query.






      share|improve this answer






























        0














        Assuming that;
        - the table is named logininfo and
        - that call_time and logged_in_time are stored according to YYYY-MM-DD HH:MM (or one of the supported formats as per Date And Time Functions)



        Then I believe that the following will do what you want :-



        WITH 
        CTE1 AS (
        SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
        FROM logininfo
        WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
        AND customer_id = 1 -- must be for this customer
        AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
        )

        SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
        (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
        (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
        (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
        ;



        • You would apply the appropriate customer_id and call_time to the where clause.

        • Your expected results don't appear to comply with so after I give a customer a phone call I need to see if they logged in to their account within 1 day after, or within 7 days after or within 14 days after the call. as within 7 days after would include the number of calls made within 1 day after and so on. If this isn;t the case it's just a matter of changing the last 3 WHERE clauses appropriately.


        Assuming a table populated as :-



        enter image description here



        The above will result in :-



        enter image description here



        The following is the full testing script used :-



        DROP TABLE IF EXISTS logininfo;
        CREATE TABLE IF NOT EXISTS logininfo (customer_id INTEGER, call_time TEXT, logged_in_time TEXT);
        INSERT INTO logininfo VALUES
        (1,'2018-01-01 11:30','2018-01-02 10:00'),
        (1,'2018-01-01 11:30','2018-01-03 10:00'),
        (1,'2018-01-01 11:30','2018-01-04 10:00'),
        (1,'2018-01-01 11:30','2018-01-05 10:00'),
        (1,'2018-01-01 11:30','2018-01-06 10:00'),
        (1,'2018-01-01 11:30','2018-01-07 10:00'),
        (1,'2018-01-01 11:30','2018-01-08 10:00'),
        (1,'2018-01-01 11:30','2018-01-15 10:00'),
        (1,'2018-01-01 11:30','2018-01-16 10:00'),
        (1,'2018-01-01 11:30','2018-01-17 10:00'),

        (1,'2018-02-01 11:30','2018-02-14 10:00'),
        (1,'2018-02-01 11:30','2018-02-15 10:00'),
        (1,'2018-02-01 11:30','2018-02-16 10:00'),
        (1,'2018-02-01 11:30','2018-02-17 10:00'),
        (1,'2018-02-01 11:30','2018-02-18 10:00'),
        (1,'2018-02-01 11:30','2018-02-19 10:00'),

        (2,'2018-01-01 11:30','2018-01-02 10:00'),
        (2,'2018-01-01 11:30','2018-01-03 10:00'),
        (2,'2018-01-01 11:30','2018-01-04 10:00'),
        (2,'2018-01-01 11:30','2018-01-05 10:00'),
        (2,'2018-01-01 11:30','2018-01-15 10:00'),
        (2,'2018-01-01 11:30','2018-01-16 10:00'),
        (2,'2018-01-01 11:30','2018-01-17 10:00')
        ;

        SELECT * FROM logininfo;

        WITH
        CTE1 AS (
        SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
        FROM logininfo
        WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
        AND customer_id = 1 -- must be for this customer
        AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
        )

        SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
        (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
        (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
        (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
        ;


        Note this doesn't use datediff rather the date difference is determined in the query.






        share|improve this answer




























          0












          0








          0







          Assuming that;
          - the table is named logininfo and
          - that call_time and logged_in_time are stored according to YYYY-MM-DD HH:MM (or one of the supported formats as per Date And Time Functions)



          Then I believe that the following will do what you want :-



          WITH 
          CTE1 AS (
          SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
          FROM logininfo
          WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
          AND customer_id = 1 -- must be for this customer
          AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
          )

          SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
          ;



          • You would apply the appropriate customer_id and call_time to the where clause.

          • Your expected results don't appear to comply with so after I give a customer a phone call I need to see if they logged in to their account within 1 day after, or within 7 days after or within 14 days after the call. as within 7 days after would include the number of calls made within 1 day after and so on. If this isn;t the case it's just a matter of changing the last 3 WHERE clauses appropriately.


          Assuming a table populated as :-



          enter image description here



          The above will result in :-



          enter image description here



          The following is the full testing script used :-



          DROP TABLE IF EXISTS logininfo;
          CREATE TABLE IF NOT EXISTS logininfo (customer_id INTEGER, call_time TEXT, logged_in_time TEXT);
          INSERT INTO logininfo VALUES
          (1,'2018-01-01 11:30','2018-01-02 10:00'),
          (1,'2018-01-01 11:30','2018-01-03 10:00'),
          (1,'2018-01-01 11:30','2018-01-04 10:00'),
          (1,'2018-01-01 11:30','2018-01-05 10:00'),
          (1,'2018-01-01 11:30','2018-01-06 10:00'),
          (1,'2018-01-01 11:30','2018-01-07 10:00'),
          (1,'2018-01-01 11:30','2018-01-08 10:00'),
          (1,'2018-01-01 11:30','2018-01-15 10:00'),
          (1,'2018-01-01 11:30','2018-01-16 10:00'),
          (1,'2018-01-01 11:30','2018-01-17 10:00'),

          (1,'2018-02-01 11:30','2018-02-14 10:00'),
          (1,'2018-02-01 11:30','2018-02-15 10:00'),
          (1,'2018-02-01 11:30','2018-02-16 10:00'),
          (1,'2018-02-01 11:30','2018-02-17 10:00'),
          (1,'2018-02-01 11:30','2018-02-18 10:00'),
          (1,'2018-02-01 11:30','2018-02-19 10:00'),

          (2,'2018-01-01 11:30','2018-01-02 10:00'),
          (2,'2018-01-01 11:30','2018-01-03 10:00'),
          (2,'2018-01-01 11:30','2018-01-04 10:00'),
          (2,'2018-01-01 11:30','2018-01-05 10:00'),
          (2,'2018-01-01 11:30','2018-01-15 10:00'),
          (2,'2018-01-01 11:30','2018-01-16 10:00'),
          (2,'2018-01-01 11:30','2018-01-17 10:00')
          ;

          SELECT * FROM logininfo;

          WITH
          CTE1 AS (
          SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
          FROM logininfo
          WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
          AND customer_id = 1 -- must be for this customer
          AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
          )

          SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
          ;


          Note this doesn't use datediff rather the date difference is determined in the query.






          share|improve this answer















          Assuming that;
          - the table is named logininfo and
          - that call_time and logged_in_time are stored according to YYYY-MM-DD HH:MM (or one of the supported formats as per Date And Time Functions)



          Then I believe that the following will do what you want :-



          WITH 
          CTE1 AS (
          SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
          FROM logininfo
          WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
          AND customer_id = 1 -- must be for this customer
          AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
          )

          SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
          ;



          • You would apply the appropriate customer_id and call_time to the where clause.

          • Your expected results don't appear to comply with so after I give a customer a phone call I need to see if they logged in to their account within 1 day after, or within 7 days after or within 14 days after the call. as within 7 days after would include the number of calls made within 1 day after and so on. If this isn;t the case it's just a matter of changing the last 3 WHERE clauses appropriately.


          Assuming a table populated as :-



          enter image description here



          The above will result in :-



          enter image description here



          The following is the full testing script used :-



          DROP TABLE IF EXISTS logininfo;
          CREATE TABLE IF NOT EXISTS logininfo (customer_id INTEGER, call_time TEXT, logged_in_time TEXT);
          INSERT INTO logininfo VALUES
          (1,'2018-01-01 11:30','2018-01-02 10:00'),
          (1,'2018-01-01 11:30','2018-01-03 10:00'),
          (1,'2018-01-01 11:30','2018-01-04 10:00'),
          (1,'2018-01-01 11:30','2018-01-05 10:00'),
          (1,'2018-01-01 11:30','2018-01-06 10:00'),
          (1,'2018-01-01 11:30','2018-01-07 10:00'),
          (1,'2018-01-01 11:30','2018-01-08 10:00'),
          (1,'2018-01-01 11:30','2018-01-15 10:00'),
          (1,'2018-01-01 11:30','2018-01-16 10:00'),
          (1,'2018-01-01 11:30','2018-01-17 10:00'),

          (1,'2018-02-01 11:30','2018-02-14 10:00'),
          (1,'2018-02-01 11:30','2018-02-15 10:00'),
          (1,'2018-02-01 11:30','2018-02-16 10:00'),
          (1,'2018-02-01 11:30','2018-02-17 10:00'),
          (1,'2018-02-01 11:30','2018-02-18 10:00'),
          (1,'2018-02-01 11:30','2018-02-19 10:00'),

          (2,'2018-01-01 11:30','2018-01-02 10:00'),
          (2,'2018-01-01 11:30','2018-01-03 10:00'),
          (2,'2018-01-01 11:30','2018-01-04 10:00'),
          (2,'2018-01-01 11:30','2018-01-05 10:00'),
          (2,'2018-01-01 11:30','2018-01-15 10:00'),
          (2,'2018-01-01 11:30','2018-01-16 10:00'),
          (2,'2018-01-01 11:30','2018-01-17 10:00')
          ;

          SELECT * FROM logininfo;

          WITH
          CTE1 AS (
          SELECT customer_id, strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time) AS daysafter
          FROM logininfo
          WHERE (strftime('%Y%m%d',logged_in_time) - strftime('%Y%m%d',call_time)) > 0 -- ignore login the same day
          AND customer_id = 1 -- must be for this customer
          AND date(call_time) = date('2018-01-01') -- must be in relation to this call (if wanted)
          )

          SELECT (SELECT customer_id FROM CTE1 ORDER BY customer_id),
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 1) AS 'logged in 1 day after',
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 7) AS 'logged in 7 days after',
          (SELECT count() FROM CTE1 WHERE daysafter > 0 AND daysafter <= 14) AS 'logged in 14 days after'
          ;


          Note this doesn't use datediff rather the date difference is determined in the query.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 5:21

























          answered Nov 13 '18 at 5:08









          MikeTMikeT

          15.5k112541




          15.5k112541

























              0














              You can make use of Conditional Sums in SQL Aggregate methods



              SELECT SUM(CASE WHEN time > 1 AND time <= 7 THEN 1 ELSE 0 END)   AS LoggedInAfter1day,
              SUM(CASE WHEN time > 7 AND time <= 14 THEN 1 ELSE 0 END ) AS LoggedInAfter7day,
              SUM(CASE WHEN time > 14 THEN 1 ELSE 0 END ) AS LoggedInAfter14day
              FROM ( SELECT (logged_in_time - call_time) As time FROM customers ) AS c





              share|improve this answer




























                0














                You can make use of Conditional Sums in SQL Aggregate methods



                SELECT SUM(CASE WHEN time > 1 AND time <= 7 THEN 1 ELSE 0 END)   AS LoggedInAfter1day,
                SUM(CASE WHEN time > 7 AND time <= 14 THEN 1 ELSE 0 END ) AS LoggedInAfter7day,
                SUM(CASE WHEN time > 14 THEN 1 ELSE 0 END ) AS LoggedInAfter14day
                FROM ( SELECT (logged_in_time - call_time) As time FROM customers ) AS c





                share|improve this answer


























                  0












                  0








                  0







                  You can make use of Conditional Sums in SQL Aggregate methods



                  SELECT SUM(CASE WHEN time > 1 AND time <= 7 THEN 1 ELSE 0 END)   AS LoggedInAfter1day,
                  SUM(CASE WHEN time > 7 AND time <= 14 THEN 1 ELSE 0 END ) AS LoggedInAfter7day,
                  SUM(CASE WHEN time > 14 THEN 1 ELSE 0 END ) AS LoggedInAfter14day
                  FROM ( SELECT (logged_in_time - call_time) As time FROM customers ) AS c





                  share|improve this answer













                  You can make use of Conditional Sums in SQL Aggregate methods



                  SELECT SUM(CASE WHEN time > 1 AND time <= 7 THEN 1 ELSE 0 END)   AS LoggedInAfter1day,
                  SUM(CASE WHEN time > 7 AND time <= 14 THEN 1 ELSE 0 END ) AS LoggedInAfter7day,
                  SUM(CASE WHEN time > 14 THEN 1 ELSE 0 END ) AS LoggedInAfter14day
                  FROM ( SELECT (logged_in_time - call_time) As time FROM customers ) AS c






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 4:35









                  Sanal SunnySanal Sunny

                  6628




                  6628






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273088%2fsql-how-to-count-the-number-of-events-that-happened-over-a-time-period%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues