Group by a field in my sql and concatinate another field values as one string












0














My table is something like that.



Location | Building | Department | 

L1 | B1 | D1
L2 | B2 | D2
L3 | B1 | D3


What I want to do is a query that counts locations and groups by buildings, but then display departments in that building as a string



Building | Count L | Departments | 

B1 | 2 | D1,D3
B2 | 1 | D2









share|improve this question





























    0














    My table is something like that.



    Location | Building | Department | 

    L1 | B1 | D1
    L2 | B2 | D2
    L3 | B1 | D3


    What I want to do is a query that counts locations and groups by buildings, but then display departments in that building as a string



    Building | Count L | Departments | 

    B1 | 2 | D1,D3
    B2 | 1 | D2









    share|improve this question



























      0












      0








      0







      My table is something like that.



      Location | Building | Department | 

      L1 | B1 | D1
      L2 | B2 | D2
      L3 | B1 | D3


      What I want to do is a query that counts locations and groups by buildings, but then display departments in that building as a string



      Building | Count L | Departments | 

      B1 | 2 | D1,D3
      B2 | 1 | D2









      share|improve this question















      My table is something like that.



      Location | Building | Department | 

      L1 | B1 | D1
      L2 | B2 | D2
      L3 | B1 | D3


      What I want to do is a query that counts locations and groups by buildings, but then display departments in that building as a string



      Building | Count L | Departments | 

      B1 | 2 | D1,D3
      B2 | 1 | D2






      mysql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 0:32









      dwir182

      1,418618




      1,418618










      asked Nov 13 '18 at 0:28









      user6437700user6437700

      6919




      6919
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You can use Group_Concat function



          Definition:




          This function returns a string result with the concatenated non-NULL
          values from a group. It returns NULL if there are no non-NULL values




          The query would be like:



          Select 
          Building,
          count(Location) as `Count L`,
          group_concat(Department) as Departments
          From
          your_table
          Group By
          Building





          share|improve this answer































            0














            select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,'')) 
            from yourtable
            group by building


            This will help with nulls also.






            share|improve this answer

















            • 2




              Actually use backtick `` in mysql to escape that name column.. Not single quote.. :)
              – dwir182
              Nov 13 '18 at 0:43










            • I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
              – user6437700
              Nov 14 '18 at 22:01













            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%2f53272067%2fgroup-by-a-field-in-my-sql-and-concatinate-another-field-values-as-one-string%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









            2














            You can use Group_Concat function



            Definition:




            This function returns a string result with the concatenated non-NULL
            values from a group. It returns NULL if there are no non-NULL values




            The query would be like:



            Select 
            Building,
            count(Location) as `Count L`,
            group_concat(Department) as Departments
            From
            your_table
            Group By
            Building





            share|improve this answer




























              2














              You can use Group_Concat function



              Definition:




              This function returns a string result with the concatenated non-NULL
              values from a group. It returns NULL if there are no non-NULL values




              The query would be like:



              Select 
              Building,
              count(Location) as `Count L`,
              group_concat(Department) as Departments
              From
              your_table
              Group By
              Building





              share|improve this answer


























                2












                2








                2






                You can use Group_Concat function



                Definition:




                This function returns a string result with the concatenated non-NULL
                values from a group. It returns NULL if there are no non-NULL values




                The query would be like:



                Select 
                Building,
                count(Location) as `Count L`,
                group_concat(Department) as Departments
                From
                your_table
                Group By
                Building





                share|improve this answer














                You can use Group_Concat function



                Definition:




                This function returns a string result with the concatenated non-NULL
                values from a group. It returns NULL if there are no non-NULL values




                The query would be like:



                Select 
                Building,
                count(Location) as `Count L`,
                group_concat(Department) as Departments
                From
                your_table
                Group By
                Building






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 5:16









                Madhur Bhaiya

                19.5k62236




                19.5k62236










                answered Nov 13 '18 at 0:34









                dwir182dwir182

                1,418618




                1,418618

























                    0














                    select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,'')) 
                    from yourtable
                    group by building


                    This will help with nulls also.






                    share|improve this answer

















                    • 2




                      Actually use backtick `` in mysql to escape that name column.. Not single quote.. :)
                      – dwir182
                      Nov 13 '18 at 0:43










                    • I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
                      – user6437700
                      Nov 14 '18 at 22:01


















                    0














                    select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,'')) 
                    from yourtable
                    group by building


                    This will help with nulls also.






                    share|improve this answer

















                    • 2




                      Actually use backtick `` in mysql to escape that name column.. Not single quote.. :)
                      – dwir182
                      Nov 13 '18 at 0:43










                    • I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
                      – user6437700
                      Nov 14 '18 at 22:01
















                    0












                    0








                    0






                    select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,'')) 
                    from yourtable
                    group by building


                    This will help with nulls also.






                    share|improve this answer












                    select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,'')) 
                    from yourtable
                    group by building


                    This will help with nulls also.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 0:41









                    GauravsaGauravsa

                    2,3251816




                    2,3251816








                    • 2




                      Actually use backtick `` in mysql to escape that name column.. Not single quote.. :)
                      – dwir182
                      Nov 13 '18 at 0:43










                    • I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
                      – user6437700
                      Nov 14 '18 at 22:01
















                    • 2




                      Actually use backtick `` in mysql to escape that name column.. Not single quote.. :)
                      – dwir182
                      Nov 13 '18 at 0:43










                    • I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
                      – user6437700
                      Nov 14 '18 at 22:01










                    2




                    2




                    Actually use backtick `` in mysql to escape that name column.. Not single quote.. :)
                    – dwir182
                    Nov 13 '18 at 0:43




                    Actually use backtick `` in mysql to escape that name column.. Not single quote.. :)
                    – dwir182
                    Nov 13 '18 at 0:43












                    I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
                    – user6437700
                    Nov 14 '18 at 22:01






                    I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
                    – user6437700
                    Nov 14 '18 at 22:01




















                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53272067%2fgroup-by-a-field-in-my-sql-and-concatinate-another-field-values-as-one-string%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







                    這個網誌中的熱門文章

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Xamarin.form Move up view when keyboard appear

                    JBPM : POST request for execute process go wrong