SQL select only one row (with all attibute values) of each different value The Next CEO of Stack OverflowAdd a column with a default value to an existing table in SQL ServerFetch the row which has the Max value for a columnCan I concatenate multiple MySQL rows into one field?Inserting multiple rows in a single SQL query?How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?SQL Server: How to Join to first rowHow do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableSelect first row in each GROUP BY group?SQL select only rows with max value on a column

Easy to read palindrome checker

Purpose of level-shifter with same in and out voltages

Does destroying a Lich's phylactery destroy the soul within it?

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Lucky Feat: How can "more than one creature spend a luck point to influence the outcome of a roll"?

Is it okay to majorly distort historical facts while writing a fiction story?

Yu-Gi-Oh cards in Python 3

What would be the main consequences for a country leaving the WTO?

What does "shotgun unity" refer to here in this sentence?

Can someone explain this formula for calculating Manhattan distance?

What day is it again?

Help/tips for a first time writer?

Audio Conversion With ADS1243

What was the first Unix version to run on a microcomputer?

"Eavesdropping" vs "Listen in on"

Is there such a thing as a proper verb, like a proper noun?

In the "Harry Potter and the Order of the Phoenix" video game, what potion is used to sabotage Umbridge's speakers?

Decide between Polyglossia and Babel for LuaLaTeX in 2019

I dug holes for my pergola too wide

Is it professional to write unrelated content in an almost-empty email?

Does higher Oxidation/ reduction potential translate to higher energy storage in battery?

How to use ReplaceAll on an expression that contains a rule

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

IC has pull-down resistors on SMBus lines?



SQL select only one row (with all attibute values) of each different value



The Next CEO of Stack OverflowAdd a column with a default value to an existing table in SQL ServerFetch the row which has the Max value for a columnCan I concatenate multiple MySQL rows into one field?Inserting multiple rows in a single SQL query?How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?SQL Server: How to Join to first rowHow do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableSelect first row in each GROUP BY group?SQL select only rows with max value on a column










0















in SQL Developer, i want to select only one row from my table (with all attibute values) of each different value.
It's not important what row is selected for each type, what matters is to select only one row for type.



for example i have this table:



| A | B | C |
X SS G
Y SB T
Z SB T


Note that in my table there aren't numbers.



The result i want is:



| A | B | C |
X SS G
Z SB T


But is correct also



| A | B | C |
X SS G
Y SB T


Thank you!










share|improve this question
























  • Type of what? The question is not clear.

    – Gordon Linoff
    Mar 8 at 20:05















0















in SQL Developer, i want to select only one row from my table (with all attibute values) of each different value.
It's not important what row is selected for each type, what matters is to select only one row for type.



for example i have this table:



| A | B | C |
X SS G
Y SB T
Z SB T


Note that in my table there aren't numbers.



The result i want is:



| A | B | C |
X SS G
Z SB T


But is correct also



| A | B | C |
X SS G
Y SB T


Thank you!










share|improve this question
























  • Type of what? The question is not clear.

    – Gordon Linoff
    Mar 8 at 20:05













0












0








0








in SQL Developer, i want to select only one row from my table (with all attibute values) of each different value.
It's not important what row is selected for each type, what matters is to select only one row for type.



for example i have this table:



| A | B | C |
X SS G
Y SB T
Z SB T


Note that in my table there aren't numbers.



The result i want is:



| A | B | C |
X SS G
Z SB T


But is correct also



| A | B | C |
X SS G
Y SB T


Thank you!










share|improve this question
















in SQL Developer, i want to select only one row from my table (with all attibute values) of each different value.
It's not important what row is selected for each type, what matters is to select only one row for type.



for example i have this table:



| A | B | C |
X SS G
Y SB T
Z SB T


Note that in my table there aren't numbers.



The result i want is:



| A | B | C |
X SS G
Z SB T


But is correct also



| A | B | C |
X SS G
Y SB T


Thank you!







sql oracle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 17:59









a_horse_with_no_name

306k46468566




306k46468566










asked Mar 8 at 17:00









James The BeardJames The Beard

10819




10819












  • Type of what? The question is not clear.

    – Gordon Linoff
    Mar 8 at 20:05

















  • Type of what? The question is not clear.

    – Gordon Linoff
    Mar 8 at 20:05
















Type of what? The question is not clear.

– Gordon Linoff
Mar 8 at 20:05





Type of what? The question is not clear.

– Gordon Linoff
Mar 8 at 20:05












5 Answers
5






active

oldest

votes


















0














It isn't very clear what you want. You could get your result just with



Select distinct top 2 * from mytable





share|improve this answer






























    0














    You want only 1 row from the rows with c = 'T', right?



    select a, b, c from tablename where c <> 'T'
    union all
    select a, b, c from (
    select a, b, c from tablename where c = 'T'
    ) where rownum <= 1





    share|improve this answer






























      0














      You probably need to use GROUP BY and then aggregate column A into a new comma separated string. Use this for the group by:



      SELECT B,C FROM your_table GROUP BY B,C


      This will output:



      +---+---+
      | B| C|
      +---+---+
      | SS| G|
      | SB| T|
      +---+---+


      Then you need to collect somehow the aggregated results of A into a string containing all the values for the specific row:



      mysql



      SELECT B,C,GROUP_CONCAT(A) as A_STRING FROM tmp GROUP BY B,C;


      PostgreSQL



      SELECT B,C,string_agg(A, ',') as A_STRING FROM tmp GROUP BY B,C;


      SQL Server



      SELECT B,C,STRING_AGG(A, ',') as A_STRING FROM tmp GROUP BY B,C;


      Then the output will be:



      +---+---+--------+
      | B| C|A_STRING|
      +---+---+--------+
      | SS| G|X |
      | SB| T|Y,Z |
      +---+---+--------+


      Hence a new string per row.






      share|improve this answer




















      • 1





        MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also use string_agg(a, ',') to get comma separated string (Postgres had that long before SQL Server)

        – a_horse_with_no_name
        Mar 8 at 18:01











      • Thank you @a_horse_with_no_name for the correction :) It should be fixed now

        – Alexandros Biratsis
        Mar 8 at 18:07



















      0














      You can use the below if the value of A is not important



      SELECT max(A) as A,B,C FROM your_table GROUP BY B,C





      share|improve this answer
































        0














        Thank you to all for your answers, but I solved in this way:



        SELECT MAX(A), B, MAX(C)
        FROM MY_TABLE
        GROUP BY B;


        With this query I can extract all values for each different type of B.
        I hope this will be helpful for someone.






        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%2f55067755%2fsql-select-only-one-row-with-all-attibute-values-of-each-different-value%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          It isn't very clear what you want. You could get your result just with



          Select distinct top 2 * from mytable





          share|improve this answer



























            0














            It isn't very clear what you want. You could get your result just with



            Select distinct top 2 * from mytable





            share|improve this answer

























              0












              0








              0







              It isn't very clear what you want. You could get your result just with



              Select distinct top 2 * from mytable





              share|improve this answer













              It isn't very clear what you want. You could get your result just with



              Select distinct top 2 * from mytable






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 8 at 17:16









              iainciainc

              367313




              367313























                  0














                  You want only 1 row from the rows with c = 'T', right?



                  select a, b, c from tablename where c <> 'T'
                  union all
                  select a, b, c from (
                  select a, b, c from tablename where c = 'T'
                  ) where rownum <= 1





                  share|improve this answer



























                    0














                    You want only 1 row from the rows with c = 'T', right?



                    select a, b, c from tablename where c <> 'T'
                    union all
                    select a, b, c from (
                    select a, b, c from tablename where c = 'T'
                    ) where rownum <= 1





                    share|improve this answer

























                      0












                      0








                      0







                      You want only 1 row from the rows with c = 'T', right?



                      select a, b, c from tablename where c <> 'T'
                      union all
                      select a, b, c from (
                      select a, b, c from tablename where c = 'T'
                      ) where rownum <= 1





                      share|improve this answer













                      You want only 1 row from the rows with c = 'T', right?



                      select a, b, c from tablename where c <> 'T'
                      union all
                      select a, b, c from (
                      select a, b, c from tablename where c = 'T'
                      ) where rownum <= 1






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 8 at 18:06









                      forpasforpas

                      19.1k3828




                      19.1k3828





















                          0














                          You probably need to use GROUP BY and then aggregate column A into a new comma separated string. Use this for the group by:



                          SELECT B,C FROM your_table GROUP BY B,C


                          This will output:



                          +---+---+
                          | B| C|
                          +---+---+
                          | SS| G|
                          | SB| T|
                          +---+---+


                          Then you need to collect somehow the aggregated results of A into a string containing all the values for the specific row:



                          mysql



                          SELECT B,C,GROUP_CONCAT(A) as A_STRING FROM tmp GROUP BY B,C;


                          PostgreSQL



                          SELECT B,C,string_agg(A, ',') as A_STRING FROM tmp GROUP BY B,C;


                          SQL Server



                          SELECT B,C,STRING_AGG(A, ',') as A_STRING FROM tmp GROUP BY B,C;


                          Then the output will be:



                          +---+---+--------+
                          | B| C|A_STRING|
                          +---+---+--------+
                          | SS| G|X |
                          | SB| T|Y,Z |
                          +---+---+--------+


                          Hence a new string per row.






                          share|improve this answer




















                          • 1





                            MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also use string_agg(a, ',') to get comma separated string (Postgres had that long before SQL Server)

                            – a_horse_with_no_name
                            Mar 8 at 18:01











                          • Thank you @a_horse_with_no_name for the correction :) It should be fixed now

                            – Alexandros Biratsis
                            Mar 8 at 18:07
















                          0














                          You probably need to use GROUP BY and then aggregate column A into a new comma separated string. Use this for the group by:



                          SELECT B,C FROM your_table GROUP BY B,C


                          This will output:



                          +---+---+
                          | B| C|
                          +---+---+
                          | SS| G|
                          | SB| T|
                          +---+---+


                          Then you need to collect somehow the aggregated results of A into a string containing all the values for the specific row:



                          mysql



                          SELECT B,C,GROUP_CONCAT(A) as A_STRING FROM tmp GROUP BY B,C;


                          PostgreSQL



                          SELECT B,C,string_agg(A, ',') as A_STRING FROM tmp GROUP BY B,C;


                          SQL Server



                          SELECT B,C,STRING_AGG(A, ',') as A_STRING FROM tmp GROUP BY B,C;


                          Then the output will be:



                          +---+---+--------+
                          | B| C|A_STRING|
                          +---+---+--------+
                          | SS| G|X |
                          | SB| T|Y,Z |
                          +---+---+--------+


                          Hence a new string per row.






                          share|improve this answer




















                          • 1





                            MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also use string_agg(a, ',') to get comma separated string (Postgres had that long before SQL Server)

                            – a_horse_with_no_name
                            Mar 8 at 18:01











                          • Thank you @a_horse_with_no_name for the correction :) It should be fixed now

                            – Alexandros Biratsis
                            Mar 8 at 18:07














                          0












                          0








                          0







                          You probably need to use GROUP BY and then aggregate column A into a new comma separated string. Use this for the group by:



                          SELECT B,C FROM your_table GROUP BY B,C


                          This will output:



                          +---+---+
                          | B| C|
                          +---+---+
                          | SS| G|
                          | SB| T|
                          +---+---+


                          Then you need to collect somehow the aggregated results of A into a string containing all the values for the specific row:



                          mysql



                          SELECT B,C,GROUP_CONCAT(A) as A_STRING FROM tmp GROUP BY B,C;


                          PostgreSQL



                          SELECT B,C,string_agg(A, ',') as A_STRING FROM tmp GROUP BY B,C;


                          SQL Server



                          SELECT B,C,STRING_AGG(A, ',') as A_STRING FROM tmp GROUP BY B,C;


                          Then the output will be:



                          +---+---+--------+
                          | B| C|A_STRING|
                          +---+---+--------+
                          | SS| G|X |
                          | SB| T|Y,Z |
                          +---+---+--------+


                          Hence a new string per row.






                          share|improve this answer















                          You probably need to use GROUP BY and then aggregate column A into a new comma separated string. Use this for the group by:



                          SELECT B,C FROM your_table GROUP BY B,C


                          This will output:



                          +---+---+
                          | B| C|
                          +---+---+
                          | SS| G|
                          | SB| T|
                          +---+---+


                          Then you need to collect somehow the aggregated results of A into a string containing all the values for the specific row:



                          mysql



                          SELECT B,C,GROUP_CONCAT(A) as A_STRING FROM tmp GROUP BY B,C;


                          PostgreSQL



                          SELECT B,C,string_agg(A, ',') as A_STRING FROM tmp GROUP BY B,C;


                          SQL Server



                          SELECT B,C,STRING_AGG(A, ',') as A_STRING FROM tmp GROUP BY B,C;


                          Then the output will be:



                          +---+---+--------+
                          | B| C|A_STRING|
                          +---+---+--------+
                          | SS| G|X |
                          | SB| T|Y,Z |
                          +---+---+--------+


                          Hence a new string per row.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 8 at 18:06

























                          answered Mar 8 at 17:42









                          Alexandros BiratsisAlexandros Biratsis

                          1,0241020




                          1,0241020







                          • 1





                            MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also use string_agg(a, ',') to get comma separated string (Postgres had that long before SQL Server)

                            – a_horse_with_no_name
                            Mar 8 at 18:01











                          • Thank you @a_horse_with_no_name for the correction :) It should be fixed now

                            – Alexandros Biratsis
                            Mar 8 at 18:07













                          • 1





                            MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also use string_agg(a, ',') to get comma separated string (Postgres had that long before SQL Server)

                            – a_horse_with_no_name
                            Mar 8 at 18:01











                          • Thank you @a_horse_with_no_name for the correction :) It should be fixed now

                            – Alexandros Biratsis
                            Mar 8 at 18:07








                          1




                          1





                          MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also use string_agg(a, ',') to get comma separated string (Postgres had that long before SQL Server)

                          – a_horse_with_no_name
                          Mar 8 at 18:01





                          MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also use string_agg(a, ',') to get comma separated string (Postgres had that long before SQL Server)

                          – a_horse_with_no_name
                          Mar 8 at 18:01













                          Thank you @a_horse_with_no_name for the correction :) It should be fixed now

                          – Alexandros Biratsis
                          Mar 8 at 18:07






                          Thank you @a_horse_with_no_name for the correction :) It should be fixed now

                          – Alexandros Biratsis
                          Mar 8 at 18:07












                          0














                          You can use the below if the value of A is not important



                          SELECT max(A) as A,B,C FROM your_table GROUP BY B,C





                          share|improve this answer





























                            0














                            You can use the below if the value of A is not important



                            SELECT max(A) as A,B,C FROM your_table GROUP BY B,C





                            share|improve this answer



























                              0












                              0








                              0







                              You can use the below if the value of A is not important



                              SELECT max(A) as A,B,C FROM your_table GROUP BY B,C





                              share|improve this answer















                              You can use the below if the value of A is not important



                              SELECT max(A) as A,B,C FROM your_table GROUP BY B,C






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 9 at 7:51

























                              answered Mar 9 at 7:43









                              psaraj12psaraj12

                              2,55511526




                              2,55511526





















                                  0














                                  Thank you to all for your answers, but I solved in this way:



                                  SELECT MAX(A), B, MAX(C)
                                  FROM MY_TABLE
                                  GROUP BY B;


                                  With this query I can extract all values for each different type of B.
                                  I hope this will be helpful for someone.






                                  share|improve this answer



























                                    0














                                    Thank you to all for your answers, but I solved in this way:



                                    SELECT MAX(A), B, MAX(C)
                                    FROM MY_TABLE
                                    GROUP BY B;


                                    With this query I can extract all values for each different type of B.
                                    I hope this will be helpful for someone.






                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      Thank you to all for your answers, but I solved in this way:



                                      SELECT MAX(A), B, MAX(C)
                                      FROM MY_TABLE
                                      GROUP BY B;


                                      With this query I can extract all values for each different type of B.
                                      I hope this will be helpful for someone.






                                      share|improve this answer













                                      Thank you to all for your answers, but I solved in this way:



                                      SELECT MAX(A), B, MAX(C)
                                      FROM MY_TABLE
                                      GROUP BY B;


                                      With this query I can extract all values for each different type of B.
                                      I hope this will be helpful for someone.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 11 at 11:51









                                      James The BeardJames The Beard

                                      10819




                                      10819



























                                          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%2f55067755%2fsql-select-only-one-row-with-all-attibute-values-of-each-different-value%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

                                          How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                                          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                                          List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229