Got 3 columns of interest, need to show their graph, using MYSQLHow do you set a default value for a MySQL Datetime column?How to find all the tables in MySQL with specific column names in them?Getting the number of rows with a GROUP BY queryHow do I specify unique constraint for multiple columns in MySQL?How to use count and group by at the same select statementmysql counting records in a single table, help neededGroup results by periodReference - What does this error mean in PHP?Divide MySQL SELECT all into sections of 7 results and run new query on each sectionMySQL - combine record counts having same column value

Best Ergonomic Design for a handheld ranged weapon

How and when is the best time to reveal to new hires you are trans?

Would people understand me speaking German all over Europe?

May a hotel provide accommodation for fewer people than booked?

Is it unprofessional to mention your cover letter and resume are best viewed in Chrome?

Best practice for keeping temperature constant during film development at home

Rampant sharing of authorship among colleagues in the name of "collaboration". Is not taking part in it a death knell for a future in academia?

Should students have access to past exams or an exam bank?

Can I shorten this filter, that finds disk sizes over 100G?

Were there any unmanned expeditions to the moon that returned to Earth prior to Apollo?

How can flights operated by the same company have such different prices when marketed by another?

What is the oxidation state of Mn in HMn(CO)5?

How do I respond appropriately to an overseas company that obtained a visa for me without hiring me?

What does 「ちんちんかいかい」 mean?

Why is “deal 6 damage” a legit phrase?

Russian pronunciation of /etc (a directory)

Just how much information should you share with a former client?

Applications of pure mathematics in operations research

How to structure presentation to avoid getting questions that will be answered later in the presentation?

Why does Latex make a small adjustment when I change section color

Password management for kids - what's a good way to start?

Stationing Callouts using VBScript Labeling in ArcMap?

Easy way to get process information from a window

How would a lunar colony attack Earth?



Got 3 columns of interest, need to show their graph, using MYSQL


How do you set a default value for a MySQL Datetime column?How to find all the tables in MySQL with specific column names in them?Getting the number of rows with a GROUP BY queryHow do I specify unique constraint for multiple columns in MySQL?How to use count and group by at the same select statementmysql counting records in a single table, help neededGroup results by periodReference - What does this error mean in PHP?Divide MySQL SELECT all into sections of 7 results and run new query on each sectionMySQL - combine record counts having same column value






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















So I have 3 metric columns that are of interest:



OPENED_AT, READ_AT, CLICKED_AT


each of which is a DATETIME stating actions made by the users.



The objective is to show a graph throughout the year segmented into weeks, showing how many users opened, read throughout the content and clicked a button.

Pretty much like this:



Sample graph



My main objective is to stay lean and efficient throughout the code and queries. I'd like to practically divide each color (metric) by the week.
I would like to refrain from using 3 seperate SELECT queries with a different GROUP BY (WEEK_OPENED_AT), then GROUP BY (WEEK_READ_AT) and GROUP BY (WEEK_CLICKED_AT).



My guess was this would work:



SELECT WEEK(`OPENED_AT`) AS WEEK_OPENED_AT, COUNT(`OPENED_AT`) AS 
COUNT_OPENED_AT from `tbl_usage` GROUP BY WEEK_OPENED_AT


then



SELECT WEEK(`READ_AT`) AS WEEK_READ_AT, COUNT(`READ_AT`) AS COUNT_READ_AT 
from `tbl_usage` GROUP BY WEEK_READ_AT


then the last



SELECT WEEK(`CLICKED_AT`) AS WEEK_CLICKED_AT, COUNT(`CLICKED_AT`) AS 
COUNT_CLICKED_AT from `tbl_usage` GROUP BY WEEK_CLICKED_AT


Can I accomplish that result that with a single SELECT?

Thanks upfront



PS: Maybe an option would be to add a running number like 1..52 representing week numbers, so that group by can segment all the same?

The problem is that weeks can pass by without any any clicks, but with many say opens.










share|improve this question






























    0















    So I have 3 metric columns that are of interest:



    OPENED_AT, READ_AT, CLICKED_AT


    each of which is a DATETIME stating actions made by the users.



    The objective is to show a graph throughout the year segmented into weeks, showing how many users opened, read throughout the content and clicked a button.

    Pretty much like this:



    Sample graph



    My main objective is to stay lean and efficient throughout the code and queries. I'd like to practically divide each color (metric) by the week.
    I would like to refrain from using 3 seperate SELECT queries with a different GROUP BY (WEEK_OPENED_AT), then GROUP BY (WEEK_READ_AT) and GROUP BY (WEEK_CLICKED_AT).



    My guess was this would work:



    SELECT WEEK(`OPENED_AT`) AS WEEK_OPENED_AT, COUNT(`OPENED_AT`) AS 
    COUNT_OPENED_AT from `tbl_usage` GROUP BY WEEK_OPENED_AT


    then



    SELECT WEEK(`READ_AT`) AS WEEK_READ_AT, COUNT(`READ_AT`) AS COUNT_READ_AT 
    from `tbl_usage` GROUP BY WEEK_READ_AT


    then the last



    SELECT WEEK(`CLICKED_AT`) AS WEEK_CLICKED_AT, COUNT(`CLICKED_AT`) AS 
    COUNT_CLICKED_AT from `tbl_usage` GROUP BY WEEK_CLICKED_AT


    Can I accomplish that result that with a single SELECT?

    Thanks upfront



    PS: Maybe an option would be to add a running number like 1..52 representing week numbers, so that group by can segment all the same?

    The problem is that weeks can pass by without any any clicks, but with many say opens.










    share|improve this question


























      0












      0








      0








      So I have 3 metric columns that are of interest:



      OPENED_AT, READ_AT, CLICKED_AT


      each of which is a DATETIME stating actions made by the users.



      The objective is to show a graph throughout the year segmented into weeks, showing how many users opened, read throughout the content and clicked a button.

      Pretty much like this:



      Sample graph



      My main objective is to stay lean and efficient throughout the code and queries. I'd like to practically divide each color (metric) by the week.
      I would like to refrain from using 3 seperate SELECT queries with a different GROUP BY (WEEK_OPENED_AT), then GROUP BY (WEEK_READ_AT) and GROUP BY (WEEK_CLICKED_AT).



      My guess was this would work:



      SELECT WEEK(`OPENED_AT`) AS WEEK_OPENED_AT, COUNT(`OPENED_AT`) AS 
      COUNT_OPENED_AT from `tbl_usage` GROUP BY WEEK_OPENED_AT


      then



      SELECT WEEK(`READ_AT`) AS WEEK_READ_AT, COUNT(`READ_AT`) AS COUNT_READ_AT 
      from `tbl_usage` GROUP BY WEEK_READ_AT


      then the last



      SELECT WEEK(`CLICKED_AT`) AS WEEK_CLICKED_AT, COUNT(`CLICKED_AT`) AS 
      COUNT_CLICKED_AT from `tbl_usage` GROUP BY WEEK_CLICKED_AT


      Can I accomplish that result that with a single SELECT?

      Thanks upfront



      PS: Maybe an option would be to add a running number like 1..52 representing week numbers, so that group by can segment all the same?

      The problem is that weeks can pass by without any any clicks, but with many say opens.










      share|improve this question














      So I have 3 metric columns that are of interest:



      OPENED_AT, READ_AT, CLICKED_AT


      each of which is a DATETIME stating actions made by the users.



      The objective is to show a graph throughout the year segmented into weeks, showing how many users opened, read throughout the content and clicked a button.

      Pretty much like this:



      Sample graph



      My main objective is to stay lean and efficient throughout the code and queries. I'd like to practically divide each color (metric) by the week.
      I would like to refrain from using 3 seperate SELECT queries with a different GROUP BY (WEEK_OPENED_AT), then GROUP BY (WEEK_READ_AT) and GROUP BY (WEEK_CLICKED_AT).



      My guess was this would work:



      SELECT WEEK(`OPENED_AT`) AS WEEK_OPENED_AT, COUNT(`OPENED_AT`) AS 
      COUNT_OPENED_AT from `tbl_usage` GROUP BY WEEK_OPENED_AT


      then



      SELECT WEEK(`READ_AT`) AS WEEK_READ_AT, COUNT(`READ_AT`) AS COUNT_READ_AT 
      from `tbl_usage` GROUP BY WEEK_READ_AT


      then the last



      SELECT WEEK(`CLICKED_AT`) AS WEEK_CLICKED_AT, COUNT(`CLICKED_AT`) AS 
      COUNT_CLICKED_AT from `tbl_usage` GROUP BY WEEK_CLICKED_AT


      Can I accomplish that result that with a single SELECT?

      Thanks upfront



      PS: Maybe an option would be to add a running number like 1..52 representing week numbers, so that group by can segment all the same?

      The problem is that weeks can pass by without any any clicks, but with many say opens.







      mysql sql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 22:23









      TedTed

      1,8608 gold badges43 silver badges86 bronze badges




      1,8608 gold badges43 silver badges86 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0














          Yes you can. If the columns you show are in the same rows (i.e. same row has both CLICKED_AT and READ_AT values), then you'd need separate counts / joins.



          I'd recommend using a recursive CTE to generate the week dates first, then individual CTEs to get each of the aggregate values, and then outer-join those to the week dimension, coalescing NULLs to zeroes.



          Example:



          WITH RECURSIVE all_weeks(weekStart) AS (
          SELECT CAST('2018-12-31' AS date)
          UNION ALL
          SELECT DATE_ADD(weekStart, INTERVAL 1 WEEK)
          FROM all_months
          WHERE DATE_ADD(weekStart, INTERVAL 1 WEEK) < '2019-04-01'
          )
          , op_cnt as (SELECT
          SUBDATE('OPENED_AT', WEEKDAY('OPENED_AT')) AS WEEK_OPENED_AT,
          COUNT('OPENED_AT') AS COUNT_OPENED_AT
          from `tbl_usage` GROUP BY WEEK_OPENED_AT
          )
          SELECT weekStart, COALESCE(COUNT_OPENED_AT , 0) as cnt_Opens
          from all_weeks
          LEFT JOIN op_cnt ON (weekStart = WEEK_OPENED_AT)





          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%2f55367017%2fgot-3-columns-of-interest-need-to-show-their-graph-using-mysql%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Yes you can. If the columns you show are in the same rows (i.e. same row has both CLICKED_AT and READ_AT values), then you'd need separate counts / joins.



            I'd recommend using a recursive CTE to generate the week dates first, then individual CTEs to get each of the aggregate values, and then outer-join those to the week dimension, coalescing NULLs to zeroes.



            Example:



            WITH RECURSIVE all_weeks(weekStart) AS (
            SELECT CAST('2018-12-31' AS date)
            UNION ALL
            SELECT DATE_ADD(weekStart, INTERVAL 1 WEEK)
            FROM all_months
            WHERE DATE_ADD(weekStart, INTERVAL 1 WEEK) < '2019-04-01'
            )
            , op_cnt as (SELECT
            SUBDATE('OPENED_AT', WEEKDAY('OPENED_AT')) AS WEEK_OPENED_AT,
            COUNT('OPENED_AT') AS COUNT_OPENED_AT
            from `tbl_usage` GROUP BY WEEK_OPENED_AT
            )
            SELECT weekStart, COALESCE(COUNT_OPENED_AT , 0) as cnt_Opens
            from all_weeks
            LEFT JOIN op_cnt ON (weekStart = WEEK_OPENED_AT)





            share|improve this answer





























              0














              Yes you can. If the columns you show are in the same rows (i.e. same row has both CLICKED_AT and READ_AT values), then you'd need separate counts / joins.



              I'd recommend using a recursive CTE to generate the week dates first, then individual CTEs to get each of the aggregate values, and then outer-join those to the week dimension, coalescing NULLs to zeroes.



              Example:



              WITH RECURSIVE all_weeks(weekStart) AS (
              SELECT CAST('2018-12-31' AS date)
              UNION ALL
              SELECT DATE_ADD(weekStart, INTERVAL 1 WEEK)
              FROM all_months
              WHERE DATE_ADD(weekStart, INTERVAL 1 WEEK) < '2019-04-01'
              )
              , op_cnt as (SELECT
              SUBDATE('OPENED_AT', WEEKDAY('OPENED_AT')) AS WEEK_OPENED_AT,
              COUNT('OPENED_AT') AS COUNT_OPENED_AT
              from `tbl_usage` GROUP BY WEEK_OPENED_AT
              )
              SELECT weekStart, COALESCE(COUNT_OPENED_AT , 0) as cnt_Opens
              from all_weeks
              LEFT JOIN op_cnt ON (weekStart = WEEK_OPENED_AT)





              share|improve this answer



























                0












                0








                0







                Yes you can. If the columns you show are in the same rows (i.e. same row has both CLICKED_AT and READ_AT values), then you'd need separate counts / joins.



                I'd recommend using a recursive CTE to generate the week dates first, then individual CTEs to get each of the aggregate values, and then outer-join those to the week dimension, coalescing NULLs to zeroes.



                Example:



                WITH RECURSIVE all_weeks(weekStart) AS (
                SELECT CAST('2018-12-31' AS date)
                UNION ALL
                SELECT DATE_ADD(weekStart, INTERVAL 1 WEEK)
                FROM all_months
                WHERE DATE_ADD(weekStart, INTERVAL 1 WEEK) < '2019-04-01'
                )
                , op_cnt as (SELECT
                SUBDATE('OPENED_AT', WEEKDAY('OPENED_AT')) AS WEEK_OPENED_AT,
                COUNT('OPENED_AT') AS COUNT_OPENED_AT
                from `tbl_usage` GROUP BY WEEK_OPENED_AT
                )
                SELECT weekStart, COALESCE(COUNT_OPENED_AT , 0) as cnt_Opens
                from all_weeks
                LEFT JOIN op_cnt ON (weekStart = WEEK_OPENED_AT)





                share|improve this answer













                Yes you can. If the columns you show are in the same rows (i.e. same row has both CLICKED_AT and READ_AT values), then you'd need separate counts / joins.



                I'd recommend using a recursive CTE to generate the week dates first, then individual CTEs to get each of the aggregate values, and then outer-join those to the week dimension, coalescing NULLs to zeroes.



                Example:



                WITH RECURSIVE all_weeks(weekStart) AS (
                SELECT CAST('2018-12-31' AS date)
                UNION ALL
                SELECT DATE_ADD(weekStart, INTERVAL 1 WEEK)
                FROM all_months
                WHERE DATE_ADD(weekStart, INTERVAL 1 WEEK) < '2019-04-01'
                )
                , op_cnt as (SELECT
                SUBDATE('OPENED_AT', WEEKDAY('OPENED_AT')) AS WEEK_OPENED_AT,
                COUNT('OPENED_AT') AS COUNT_OPENED_AT
                from `tbl_usage` GROUP BY WEEK_OPENED_AT
                )
                SELECT weekStart, COALESCE(COUNT_OPENED_AT , 0) as cnt_Opens
                from all_weeks
                LEFT JOIN op_cnt ON (weekStart = WEEK_OPENED_AT)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 0:30









                MegadestMegadest

                3191 silver badge8 bronze badges




                3191 silver badge8 bronze badges



















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    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%2f55367017%2fgot-3-columns-of-interest-need-to-show-their-graph-using-mysql%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

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript