Apply group by on part of a column and get contradictions












3















Please consider this Table:



Id           FullName           Gender
---------------------------------------
1 Tom Hanksi Junior 1
2 Tom Cruisi 2
3 Meril Strippi 2
4 Leo Dicaprioi 1
5 Robert Deniroi 1
6 Al Pcinoi 1
7 Chanrilize theroni 2
8 Robert Green 1
9 Nicole Kidmani 2
10 Nicole Wagner 2
11 Peter Pan Green 1
12 Peter Viera 1
13 Peter J. Dark 2
14 Tom Henry 1


Gender Values are: 1 for Male and 2 for Female.



Now I want to create a Table for names and genders(It's assumed that every name should has one corresponding gender).



Name       Gender
-----------------
Tom 1
Meril 2
Leo 1
Rebert 1
Al 1
Charlize 2
Nicole 2
Peter 1


1)How can I apply GROUP BY on just Name part of full name and gender?



2)How can I get contradictions of names and genders. For example for Tom we have male and female values.



Thanks










share|improve this question



























    3















    Please consider this Table:



    Id           FullName           Gender
    ---------------------------------------
    1 Tom Hanksi Junior 1
    2 Tom Cruisi 2
    3 Meril Strippi 2
    4 Leo Dicaprioi 1
    5 Robert Deniroi 1
    6 Al Pcinoi 1
    7 Chanrilize theroni 2
    8 Robert Green 1
    9 Nicole Kidmani 2
    10 Nicole Wagner 2
    11 Peter Pan Green 1
    12 Peter Viera 1
    13 Peter J. Dark 2
    14 Tom Henry 1


    Gender Values are: 1 for Male and 2 for Female.



    Now I want to create a Table for names and genders(It's assumed that every name should has one corresponding gender).



    Name       Gender
    -----------------
    Tom 1
    Meril 2
    Leo 1
    Rebert 1
    Al 1
    Charlize 2
    Nicole 2
    Peter 1


    1)How can I apply GROUP BY on just Name part of full name and gender?



    2)How can I get contradictions of names and genders. For example for Tom we have male and female values.



    Thanks










    share|improve this question

























      3












      3








      3








      Please consider this Table:



      Id           FullName           Gender
      ---------------------------------------
      1 Tom Hanksi Junior 1
      2 Tom Cruisi 2
      3 Meril Strippi 2
      4 Leo Dicaprioi 1
      5 Robert Deniroi 1
      6 Al Pcinoi 1
      7 Chanrilize theroni 2
      8 Robert Green 1
      9 Nicole Kidmani 2
      10 Nicole Wagner 2
      11 Peter Pan Green 1
      12 Peter Viera 1
      13 Peter J. Dark 2
      14 Tom Henry 1


      Gender Values are: 1 for Male and 2 for Female.



      Now I want to create a Table for names and genders(It's assumed that every name should has one corresponding gender).



      Name       Gender
      -----------------
      Tom 1
      Meril 2
      Leo 1
      Rebert 1
      Al 1
      Charlize 2
      Nicole 2
      Peter 1


      1)How can I apply GROUP BY on just Name part of full name and gender?



      2)How can I get contradictions of names and genders. For example for Tom we have male and female values.



      Thanks










      share|improve this question














      Please consider this Table:



      Id           FullName           Gender
      ---------------------------------------
      1 Tom Hanksi Junior 1
      2 Tom Cruisi 2
      3 Meril Strippi 2
      4 Leo Dicaprioi 1
      5 Robert Deniroi 1
      6 Al Pcinoi 1
      7 Chanrilize theroni 2
      8 Robert Green 1
      9 Nicole Kidmani 2
      10 Nicole Wagner 2
      11 Peter Pan Green 1
      12 Peter Viera 1
      13 Peter J. Dark 2
      14 Tom Henry 1


      Gender Values are: 1 for Male and 2 for Female.



      Now I want to create a Table for names and genders(It's assumed that every name should has one corresponding gender).



      Name       Gender
      -----------------
      Tom 1
      Meril 2
      Leo 1
      Rebert 1
      Al 1
      Charlize 2
      Nicole 2
      Peter 1


      1)How can I apply GROUP BY on just Name part of full name and gender?



      2)How can I get contradictions of names and genders. For example for Tom we have male and female values.



      Thanks







      sql sql-server group-by sql-server-2014






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 8:37









      ArianArian

      4,56951151248




      4,56951151248
























          2 Answers
          2






          active

          oldest

          votes


















          2














          A bad assumption I am making here is that the first name is the part in Name before first space.



          then the first name is found from name as



          select left(Name, charindex(' ',Name)-1)


          you can group by on this and gender



          select 
          Name=left(Name, charindex(' ',Name)-1),Gender
          from
          yourTableName
          group by
          left(Name, charindex(' ',Name)-1),Gender
          order by left(Name, charindex(' ',Name)-1),Gender


          to find people with two genders and same name you can use



          select 
          Name=left(Name, charindex(' ',Name)-1)
          from
          yourTableName
          group by
          left(Name, charindex(' ',Name)-1)
          having count(distinct gender)>1


          In case you want to use both together, possibly in scenario when you want to discard name which have two genders associated you can do something like below



          ; with NnG as 
          (
          select
          Name=left(Name, charindex(' ',Name)-1),Gender
          from
          yourTableName
          group by
          left(Name, charindex(' ',Name)-1),Gender
          ),
          N2G as
          (
          select
          Name=left(Name, charindex(' ',Name)-1)
          from
          yourTableName
          group by
          left(Name, charindex(' ',Name)-1)
          having count(distinct gender)>1
          )

          select * from nng left join n2g
          on nng.Name=N2G.name
          where n2g.name is null





          share|improve this answer































            2














            Substring everything up to the first space as the first name. Group on the name and reduce to those names having a count of more than one distinct gender:



            SELECT
            SUBSTRING(Name, 1, charindex(' ',Name)-1), Gender
            FROM
            table
            GROUP BY
            SUBSTRING(Name, 1, charindex(' ',Name)-1)
            HAVING
            COUNT(DISTINCT gender) > 1


            If you have 1000 males, count distinct gender will return 1, because ther eis only one gender in the set (male). If you have 1000 males and 200 females, count distinct gender will return 2 because there are 2 kinds of gender in the set (male and female). If you were to omit the DISTINCT keyword, then the count() would return 1000 for the first example and 1200 for the second (it would count all the non null items in the set, not the variations therein)






            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%2f53477293%2fapply-group-by-on-part-of-a-column-and-get-contradictions%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














              A bad assumption I am making here is that the first name is the part in Name before first space.



              then the first name is found from name as



              select left(Name, charindex(' ',Name)-1)


              you can group by on this and gender



              select 
              Name=left(Name, charindex(' ',Name)-1),Gender
              from
              yourTableName
              group by
              left(Name, charindex(' ',Name)-1),Gender
              order by left(Name, charindex(' ',Name)-1),Gender


              to find people with two genders and same name you can use



              select 
              Name=left(Name, charindex(' ',Name)-1)
              from
              yourTableName
              group by
              left(Name, charindex(' ',Name)-1)
              having count(distinct gender)>1


              In case you want to use both together, possibly in scenario when you want to discard name which have two genders associated you can do something like below



              ; with NnG as 
              (
              select
              Name=left(Name, charindex(' ',Name)-1),Gender
              from
              yourTableName
              group by
              left(Name, charindex(' ',Name)-1),Gender
              ),
              N2G as
              (
              select
              Name=left(Name, charindex(' ',Name)-1)
              from
              yourTableName
              group by
              left(Name, charindex(' ',Name)-1)
              having count(distinct gender)>1
              )

              select * from nng left join n2g
              on nng.Name=N2G.name
              where n2g.name is null





              share|improve this answer




























                2














                A bad assumption I am making here is that the first name is the part in Name before first space.



                then the first name is found from name as



                select left(Name, charindex(' ',Name)-1)


                you can group by on this and gender



                select 
                Name=left(Name, charindex(' ',Name)-1),Gender
                from
                yourTableName
                group by
                left(Name, charindex(' ',Name)-1),Gender
                order by left(Name, charindex(' ',Name)-1),Gender


                to find people with two genders and same name you can use



                select 
                Name=left(Name, charindex(' ',Name)-1)
                from
                yourTableName
                group by
                left(Name, charindex(' ',Name)-1)
                having count(distinct gender)>1


                In case you want to use both together, possibly in scenario when you want to discard name which have two genders associated you can do something like below



                ; with NnG as 
                (
                select
                Name=left(Name, charindex(' ',Name)-1),Gender
                from
                yourTableName
                group by
                left(Name, charindex(' ',Name)-1),Gender
                ),
                N2G as
                (
                select
                Name=left(Name, charindex(' ',Name)-1)
                from
                yourTableName
                group by
                left(Name, charindex(' ',Name)-1)
                having count(distinct gender)>1
                )

                select * from nng left join n2g
                on nng.Name=N2G.name
                where n2g.name is null





                share|improve this answer


























                  2












                  2








                  2







                  A bad assumption I am making here is that the first name is the part in Name before first space.



                  then the first name is found from name as



                  select left(Name, charindex(' ',Name)-1)


                  you can group by on this and gender



                  select 
                  Name=left(Name, charindex(' ',Name)-1),Gender
                  from
                  yourTableName
                  group by
                  left(Name, charindex(' ',Name)-1),Gender
                  order by left(Name, charindex(' ',Name)-1),Gender


                  to find people with two genders and same name you can use



                  select 
                  Name=left(Name, charindex(' ',Name)-1)
                  from
                  yourTableName
                  group by
                  left(Name, charindex(' ',Name)-1)
                  having count(distinct gender)>1


                  In case you want to use both together, possibly in scenario when you want to discard name which have two genders associated you can do something like below



                  ; with NnG as 
                  (
                  select
                  Name=left(Name, charindex(' ',Name)-1),Gender
                  from
                  yourTableName
                  group by
                  left(Name, charindex(' ',Name)-1),Gender
                  ),
                  N2G as
                  (
                  select
                  Name=left(Name, charindex(' ',Name)-1)
                  from
                  yourTableName
                  group by
                  left(Name, charindex(' ',Name)-1)
                  having count(distinct gender)>1
                  )

                  select * from nng left join n2g
                  on nng.Name=N2G.name
                  where n2g.name is null





                  share|improve this answer













                  A bad assumption I am making here is that the first name is the part in Name before first space.



                  then the first name is found from name as



                  select left(Name, charindex(' ',Name)-1)


                  you can group by on this and gender



                  select 
                  Name=left(Name, charindex(' ',Name)-1),Gender
                  from
                  yourTableName
                  group by
                  left(Name, charindex(' ',Name)-1),Gender
                  order by left(Name, charindex(' ',Name)-1),Gender


                  to find people with two genders and same name you can use



                  select 
                  Name=left(Name, charindex(' ',Name)-1)
                  from
                  yourTableName
                  group by
                  left(Name, charindex(' ',Name)-1)
                  having count(distinct gender)>1


                  In case you want to use both together, possibly in scenario when you want to discard name which have two genders associated you can do something like below



                  ; with NnG as 
                  (
                  select
                  Name=left(Name, charindex(' ',Name)-1),Gender
                  from
                  yourTableName
                  group by
                  left(Name, charindex(' ',Name)-1),Gender
                  ),
                  N2G as
                  (
                  select
                  Name=left(Name, charindex(' ',Name)-1)
                  from
                  yourTableName
                  group by
                  left(Name, charindex(' ',Name)-1)
                  having count(distinct gender)>1
                  )

                  select * from nng left join n2g
                  on nng.Name=N2G.name
                  where n2g.name is null






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 8:54









                  DhruvJoshiDhruvJoshi

                  12.2k62746




                  12.2k62746

























                      2














                      Substring everything up to the first space as the first name. Group on the name and reduce to those names having a count of more than one distinct gender:



                      SELECT
                      SUBSTRING(Name, 1, charindex(' ',Name)-1), Gender
                      FROM
                      table
                      GROUP BY
                      SUBSTRING(Name, 1, charindex(' ',Name)-1)
                      HAVING
                      COUNT(DISTINCT gender) > 1


                      If you have 1000 males, count distinct gender will return 1, because ther eis only one gender in the set (male). If you have 1000 males and 200 females, count distinct gender will return 2 because there are 2 kinds of gender in the set (male and female). If you were to omit the DISTINCT keyword, then the count() would return 1000 for the first example and 1200 for the second (it would count all the non null items in the set, not the variations therein)






                      share|improve this answer




























                        2














                        Substring everything up to the first space as the first name. Group on the name and reduce to those names having a count of more than one distinct gender:



                        SELECT
                        SUBSTRING(Name, 1, charindex(' ',Name)-1), Gender
                        FROM
                        table
                        GROUP BY
                        SUBSTRING(Name, 1, charindex(' ',Name)-1)
                        HAVING
                        COUNT(DISTINCT gender) > 1


                        If you have 1000 males, count distinct gender will return 1, because ther eis only one gender in the set (male). If you have 1000 males and 200 females, count distinct gender will return 2 because there are 2 kinds of gender in the set (male and female). If you were to omit the DISTINCT keyword, then the count() would return 1000 for the first example and 1200 for the second (it would count all the non null items in the set, not the variations therein)






                        share|improve this answer


























                          2












                          2








                          2







                          Substring everything up to the first space as the first name. Group on the name and reduce to those names having a count of more than one distinct gender:



                          SELECT
                          SUBSTRING(Name, 1, charindex(' ',Name)-1), Gender
                          FROM
                          table
                          GROUP BY
                          SUBSTRING(Name, 1, charindex(' ',Name)-1)
                          HAVING
                          COUNT(DISTINCT gender) > 1


                          If you have 1000 males, count distinct gender will return 1, because ther eis only one gender in the set (male). If you have 1000 males and 200 females, count distinct gender will return 2 because there are 2 kinds of gender in the set (male and female). If you were to omit the DISTINCT keyword, then the count() would return 1000 for the first example and 1200 for the second (it would count all the non null items in the set, not the variations therein)






                          share|improve this answer













                          Substring everything up to the first space as the first name. Group on the name and reduce to those names having a count of more than one distinct gender:



                          SELECT
                          SUBSTRING(Name, 1, charindex(' ',Name)-1), Gender
                          FROM
                          table
                          GROUP BY
                          SUBSTRING(Name, 1, charindex(' ',Name)-1)
                          HAVING
                          COUNT(DISTINCT gender) > 1


                          If you have 1000 males, count distinct gender will return 1, because ther eis only one gender in the set (male). If you have 1000 males and 200 females, count distinct gender will return 2 because there are 2 kinds of gender in the set (male and female). If you were to omit the DISTINCT keyword, then the count() would return 1000 for the first example and 1200 for the second (it would count all the non null items in the set, not the variations therein)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 '18 at 8:57









                          Caius JardCaius Jard

                          11.7k21239




                          11.7k21239






























                              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%2f53477293%2fapply-group-by-on-part-of-a-column-and-get-contradictions%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

                              Contact image not getting when fetch all contact list from iPhone by CNContact

                              count number of partitions of a set with n elements into k subsets

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks