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
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
add a comment |
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
Type of what? The question is not clear.
– Gordon Linoff
Mar 8 at 20:05
add a comment |
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
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
sql oracle
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
It isn't very clear what you want. You could get your result just with
Select distinct top 2 * from mytable
add a comment |
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
add a comment |
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.
1
MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also usestring_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
add a comment |
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
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
It isn't very clear what you want. You could get your result just with
Select distinct top 2 * from mytable
add a comment |
It isn't very clear what you want. You could get your result just with
Select distinct top 2 * from mytable
add a comment |
It isn't very clear what you want. You could get your result just with
Select distinct top 2 * from mytable
It isn't very clear what you want. You could get your result just with
Select distinct top 2 * from mytable
answered Mar 8 at 17:16
iainciainc
367313
367313
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 8 at 18:06
forpasforpas
19.1k3828
19.1k3828
add a comment |
add a comment |
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.
1
MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also usestring_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
add a comment |
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.
1
MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also usestring_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
add a comment |
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.
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.
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 usestring_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
add a comment |
1
MySQL does not return an array - it doesn't even have arrays. And for Postgres you could also usestring_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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Mar 9 at 7:51
answered Mar 9 at 7:43
psaraj12psaraj12
2,55511526
2,55511526
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 11 at 11:51
James The BeardJames The Beard
10819
10819
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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

Type of what? The question is not clear.
– Gordon Linoff
Mar 8 at 20:05