简体   繁体   中英

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

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!

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

Select distinct top 2 * from mytable

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 

如果A的值不重要,则可以使用以下内容

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM