SQL select question for hourly data












1















I need help with MySQL statement . I need to know how many calls come in every hour. This is what we have so far.



    SELECT
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 09:00:00' and '2015-12-02 10:00:00' THEN 1 else 0 END) as Hour2,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour3,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour4,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour5,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour6,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour7,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour8,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour9,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour10,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour11,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour12,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour13
FROM session
Inner Join mxuser
ON session.extensionid2=mxuser.extensionid
WHERE
mxuser.userprofilename = 'Team1'
And session.legtype1 = 2
AND session.Legtype2 = 1
or session.Legtype2= 2









share|improve this question














bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Cindy, welcome to the site. The table definitions would help. You can edit your question to add these. A table showing your desired output would help, too.

    – Michael Green
    Jan 8 '16 at 5:19
















1















I need help with MySQL statement . I need to know how many calls come in every hour. This is what we have so far.



    SELECT
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 09:00:00' and '2015-12-02 10:00:00' THEN 1 else 0 END) as Hour2,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour3,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour4,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour5,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour6,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour7,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour8,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour9,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour10,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour11,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour12,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour13
FROM session
Inner Join mxuser
ON session.extensionid2=mxuser.extensionid
WHERE
mxuser.userprofilename = 'Team1'
And session.legtype1 = 2
AND session.Legtype2 = 1
or session.Legtype2= 2









share|improve this question














bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Cindy, welcome to the site. The table definitions would help. You can edit your question to add these. A table showing your desired output would help, too.

    – Michael Green
    Jan 8 '16 at 5:19














1












1








1








I need help with MySQL statement . I need to know how many calls come in every hour. This is what we have so far.



    SELECT
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 09:00:00' and '2015-12-02 10:00:00' THEN 1 else 0 END) as Hour2,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour3,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour4,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour5,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour6,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour7,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour8,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour9,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour10,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour11,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour12,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour13
FROM session
Inner Join mxuser
ON session.extensionid2=mxuser.extensionid
WHERE
mxuser.userprofilename = 'Team1'
And session.legtype1 = 2
AND session.Legtype2 = 1
or session.Legtype2= 2









share|improve this question














I need help with MySQL statement . I need to know how many calls come in every hour. This is what we have so far.



    SELECT
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 09:00:00' and '2015-12-02 10:00:00' THEN 1 else 0 END) as Hour2,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour3,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour4,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour5,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour6,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour7,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour8,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour9,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour10,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour11,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour12,
sum(CASE WHEN session.creationtimestamp BETWEEN '2015-12-02 00:00:00' and '2015-12-02 23:00:00' THEN 1 else 0 END) as Hour13
FROM session
Inner Join mxuser
ON session.extensionid2=mxuser.extensionid
WHERE
mxuser.userprofilename = 'Team1'
And session.legtype1 = 2
AND session.Legtype2 = 1
or session.Legtype2= 2






mysql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 8 '16 at 4:14









CindyCindy

61




61





bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Cindy, welcome to the site. The table definitions would help. You can edit your question to add these. A table showing your desired output would help, too.

    – Michael Green
    Jan 8 '16 at 5:19



















  • Cindy, welcome to the site. The table definitions would help. You can edit your question to add these. A table showing your desired output would help, too.

    – Michael Green
    Jan 8 '16 at 5:19

















Cindy, welcome to the site. The table definitions would help. You can edit your question to add these. A table showing your desired output would help, too.

– Michael Green
Jan 8 '16 at 5:19





Cindy, welcome to the site. The table definitions would help. You can edit your question to add these. A table showing your desired output would help, too.

– Michael Green
Jan 8 '16 at 5:19










1 Answer
1






active

oldest

votes


















0














I will call creationtimestamp simply ts...



SELECT  FLOOR(UNIX_TIMESTAMP(ts)/3600) AS start_of_hour,
COUNT(*)
FROM session ...
GROUP BY FLOOR(UNIX_TIMESTAMP(ts)/3600);


If you need to "pivot" the output into 24 columns, then generate the code from http://mysql.rjweb.org/doc.php/pivot .






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2fdba.stackexchange.com%2fquestions%2f125585%2fsql-select-question-for-hourly-data%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














    I will call creationtimestamp simply ts...



    SELECT  FLOOR(UNIX_TIMESTAMP(ts)/3600) AS start_of_hour,
    COUNT(*)
    FROM session ...
    GROUP BY FLOOR(UNIX_TIMESTAMP(ts)/3600);


    If you need to "pivot" the output into 24 columns, then generate the code from http://mysql.rjweb.org/doc.php/pivot .






    share|improve this answer




























      0














      I will call creationtimestamp simply ts...



      SELECT  FLOOR(UNIX_TIMESTAMP(ts)/3600) AS start_of_hour,
      COUNT(*)
      FROM session ...
      GROUP BY FLOOR(UNIX_TIMESTAMP(ts)/3600);


      If you need to "pivot" the output into 24 columns, then generate the code from http://mysql.rjweb.org/doc.php/pivot .






      share|improve this answer


























        0












        0








        0







        I will call creationtimestamp simply ts...



        SELECT  FLOOR(UNIX_TIMESTAMP(ts)/3600) AS start_of_hour,
        COUNT(*)
        FROM session ...
        GROUP BY FLOOR(UNIX_TIMESTAMP(ts)/3600);


        If you need to "pivot" the output into 24 columns, then generate the code from http://mysql.rjweb.org/doc.php/pivot .






        share|improve this answer













        I will call creationtimestamp simply ts...



        SELECT  FLOOR(UNIX_TIMESTAMP(ts)/3600) AS start_of_hour,
        COUNT(*)
        FROM session ...
        GROUP BY FLOOR(UNIX_TIMESTAMP(ts)/3600);


        If you need to "pivot" the output into 24 columns, then generate the code from http://mysql.rjweb.org/doc.php/pivot .







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 8 '16 at 5:48









        Rick JamesRick James

        41.3k22258




        41.3k22258






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • 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%2fdba.stackexchange.com%2fquestions%2f125585%2fsql-select-question-for-hourly-data%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

            Ronny Ackermann

            Köttigit

            MySQL 8.0.15 starts normally but any connection hangs