SQL GROUP BY with two tables involved











up vote
0
down vote

favorite












Could you guys help me to make SELECT query for my simple case:



Table A:        
UserID UserName
10 John
11 Mike
12 Matt


Table B:
SessionID UserID SessionTime
124 10 20
123 10 122
42 10 30
324 11 55
534 11 42
344 12 19
653 12 32


I need this result:



Result Table:               
UserName UserID TotalTime
John 10 172
Mike 11 97
Matt 12 51


For one Table B this works:



SELECT UserID, SUM(SessionTime) AS "Total Time"
FROM TableB
GROUP BY UserID;


but I need to attach UserName to the final result



thank you










share|improve this question






















  • This is a simple JOIN + GROUP BY. What challenges did you face in the SQL build?
    – Parfait
    Nov 10 at 23:54










  • sorry for that simple question, the main challenge probably is late night in Europe zone
    – MikZ
    Nov 10 at 23:57















up vote
0
down vote

favorite












Could you guys help me to make SELECT query for my simple case:



Table A:        
UserID UserName
10 John
11 Mike
12 Matt


Table B:
SessionID UserID SessionTime
124 10 20
123 10 122
42 10 30
324 11 55
534 11 42
344 12 19
653 12 32


I need this result:



Result Table:               
UserName UserID TotalTime
John 10 172
Mike 11 97
Matt 12 51


For one Table B this works:



SELECT UserID, SUM(SessionTime) AS "Total Time"
FROM TableB
GROUP BY UserID;


but I need to attach UserName to the final result



thank you










share|improve this question






















  • This is a simple JOIN + GROUP BY. What challenges did you face in the SQL build?
    – Parfait
    Nov 10 at 23:54










  • sorry for that simple question, the main challenge probably is late night in Europe zone
    – MikZ
    Nov 10 at 23:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Could you guys help me to make SELECT query for my simple case:



Table A:        
UserID UserName
10 John
11 Mike
12 Matt


Table B:
SessionID UserID SessionTime
124 10 20
123 10 122
42 10 30
324 11 55
534 11 42
344 12 19
653 12 32


I need this result:



Result Table:               
UserName UserID TotalTime
John 10 172
Mike 11 97
Matt 12 51


For one Table B this works:



SELECT UserID, SUM(SessionTime) AS "Total Time"
FROM TableB
GROUP BY UserID;


but I need to attach UserName to the final result



thank you










share|improve this question













Could you guys help me to make SELECT query for my simple case:



Table A:        
UserID UserName
10 John
11 Mike
12 Matt


Table B:
SessionID UserID SessionTime
124 10 20
123 10 122
42 10 30
324 11 55
534 11 42
344 12 19
653 12 32


I need this result:



Result Table:               
UserName UserID TotalTime
John 10 172
Mike 11 97
Matt 12 51


For one Table B this works:



SELECT UserID, SUM(SessionTime) AS "Total Time"
FROM TableB
GROUP BY UserID;


but I need to attach UserName to the final result



thank you







mysql sql database-table






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 23:39









MikZ

344213




344213












  • This is a simple JOIN + GROUP BY. What challenges did you face in the SQL build?
    – Parfait
    Nov 10 at 23:54










  • sorry for that simple question, the main challenge probably is late night in Europe zone
    – MikZ
    Nov 10 at 23:57


















  • This is a simple JOIN + GROUP BY. What challenges did you face in the SQL build?
    – Parfait
    Nov 10 at 23:54










  • sorry for that simple question, the main challenge probably is late night in Europe zone
    – MikZ
    Nov 10 at 23:57
















This is a simple JOIN + GROUP BY. What challenges did you face in the SQL build?
– Parfait
Nov 10 at 23:54




This is a simple JOIN + GROUP BY. What challenges did you face in the SQL build?
– Parfait
Nov 10 at 23:54












sorry for that simple question, the main challenge probably is late night in Europe zone
– MikZ
Nov 10 at 23:57




sorry for that simple question, the main challenge probably is late night in Europe zone
– MikZ
Nov 10 at 23:57












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










You can do that by using join and group by:



select a.UserId, a.UserName, sum(b.SessionTime) as TotalTime
from tableA a
left join tableB b on a.UserId = b.UserId
group by a.UserId, a.UserName;


Note: This would work for 1-to-many relations as in your case.






share|improve this answer




























    up vote
    2
    down vote













    SELECT TableA.Username, TableA.User_ID, SUM(SessionTime) INNER JOIN
    TableB ON TableA.User_ID = TableB.User_ID GROUP BY TableA.Username,
    TableA.User_ID





    share|improve this answer























    • Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
      – Bobby Bridgeman
      Nov 10 at 23:49


















    up vote
    0
    down vote













    SELECT a.UserName as "UserName" 
    ,a.UserID as "UserID"
    ,sum(b.SessionTime) as "TotalTime"
    FROM a LEFT JOIN b
    ON a.UserID = b.UserID GROUP BY a.UserID


    Here. I used TABLE a and Table b






    share|improve this answer























    • There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
      – Eray Balkanli
      Nov 11 at 4:29











    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',
    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%2f53244496%2fsql-group-by-with-two-tables-involved%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You can do that by using join and group by:



    select a.UserId, a.UserName, sum(b.SessionTime) as TotalTime
    from tableA a
    left join tableB b on a.UserId = b.UserId
    group by a.UserId, a.UserName;


    Note: This would work for 1-to-many relations as in your case.






    share|improve this answer

























      up vote
      2
      down vote



      accepted










      You can do that by using join and group by:



      select a.UserId, a.UserName, sum(b.SessionTime) as TotalTime
      from tableA a
      left join tableB b on a.UserId = b.UserId
      group by a.UserId, a.UserName;


      Note: This would work for 1-to-many relations as in your case.






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        You can do that by using join and group by:



        select a.UserId, a.UserName, sum(b.SessionTime) as TotalTime
        from tableA a
        left join tableB b on a.UserId = b.UserId
        group by a.UserId, a.UserName;


        Note: This would work for 1-to-many relations as in your case.






        share|improve this answer












        You can do that by using join and group by:



        select a.UserId, a.UserName, sum(b.SessionTime) as TotalTime
        from tableA a
        left join tableB b on a.UserId = b.UserId
        group by a.UserId, a.UserName;


        Note: This would work for 1-to-many relations as in your case.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 23:43









        Cetin Basoz

        10.7k11526




        10.7k11526
























            up vote
            2
            down vote













            SELECT TableA.Username, TableA.User_ID, SUM(SessionTime) INNER JOIN
            TableB ON TableA.User_ID = TableB.User_ID GROUP BY TableA.Username,
            TableA.User_ID





            share|improve this answer























            • Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
              – Bobby Bridgeman
              Nov 10 at 23:49















            up vote
            2
            down vote













            SELECT TableA.Username, TableA.User_ID, SUM(SessionTime) INNER JOIN
            TableB ON TableA.User_ID = TableB.User_ID GROUP BY TableA.Username,
            TableA.User_ID





            share|improve this answer























            • Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
              – Bobby Bridgeman
              Nov 10 at 23:49













            up vote
            2
            down vote










            up vote
            2
            down vote









            SELECT TableA.Username, TableA.User_ID, SUM(SessionTime) INNER JOIN
            TableB ON TableA.User_ID = TableB.User_ID GROUP BY TableA.Username,
            TableA.User_ID





            share|improve this answer














            SELECT TableA.Username, TableA.User_ID, SUM(SessionTime) INNER JOIN
            TableB ON TableA.User_ID = TableB.User_ID GROUP BY TableA.Username,
            TableA.User_ID






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 23:48









            Brad

            9,57932865




            9,57932865










            answered Nov 10 at 23:46









            Bobby Bridgeman

            312




            312












            • Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
              – Bobby Bridgeman
              Nov 10 at 23:49


















            • Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
              – Bobby Bridgeman
              Nov 10 at 23:49
















            Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
            – Bobby Bridgeman
            Nov 10 at 23:49




            Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
            – Bobby Bridgeman
            Nov 10 at 23:49










            up vote
            0
            down vote













            SELECT a.UserName as "UserName" 
            ,a.UserID as "UserID"
            ,sum(b.SessionTime) as "TotalTime"
            FROM a LEFT JOIN b
            ON a.UserID = b.UserID GROUP BY a.UserID


            Here. I used TABLE a and Table b






            share|improve this answer























            • There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
              – Eray Balkanli
              Nov 11 at 4:29















            up vote
            0
            down vote













            SELECT a.UserName as "UserName" 
            ,a.UserID as "UserID"
            ,sum(b.SessionTime) as "TotalTime"
            FROM a LEFT JOIN b
            ON a.UserID = b.UserID GROUP BY a.UserID


            Here. I used TABLE a and Table b






            share|improve this answer























            • There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
              – Eray Balkanli
              Nov 11 at 4:29













            up vote
            0
            down vote










            up vote
            0
            down vote









            SELECT a.UserName as "UserName" 
            ,a.UserID as "UserID"
            ,sum(b.SessionTime) as "TotalTime"
            FROM a LEFT JOIN b
            ON a.UserID = b.UserID GROUP BY a.UserID


            Here. I used TABLE a and Table b






            share|improve this answer














            SELECT a.UserName as "UserName" 
            ,a.UserID as "UserID"
            ,sum(b.SessionTime) as "TotalTime"
            FROM a LEFT JOIN b
            ON a.UserID = b.UserID GROUP BY a.UserID


            Here. I used TABLE a and Table b







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 4:28









            Eray Balkanli

            3,85741943




            3,85741943










            answered Nov 11 at 4:22









            Shakil Hossain

            7919




            7919












            • There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
              – Eray Balkanli
              Nov 11 at 4:29


















            • There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
              – Eray Balkanli
              Nov 11 at 4:29
















            There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
            – Eray Balkanli
            Nov 11 at 4:29




            There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
            – Eray Balkanli
            Nov 11 at 4:29


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244496%2fsql-group-by-with-two-tables-involved%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