简体   繁体   中英

SQL, One id with different values in one column

Here is my query:

SELECT t.id, t.phone
FROM tablename t

It results in duplicate IDs because in one column there are two or more different values in it.

ID   Phone
1    540-500-5000
1    540-888-8888
2    340-600-6000
2    340-777-7777
3    210-200-2000
4    950-600-6000
4    950-444-4444

I want to select just the first phone for each ID , in order to avoid duplicated rows just because there are two or more phones under the same ID .

Desired output:

ID   Phone
1    540-500-5000
2    340-600-6000
3    210-200-2000
4    950-600-6000

SQL Fiddle :

SELECT t.id, MIN(t.phone)
FROM tablename t
GROUP BY t.id
SELECT ID, MIN(phone) MinIsTheFirst
FROM tableName
GROUP BY ID

Just having fun with the word "FIRST"

Try this:

select ID, MIN(Phone)
from tablename
group by ID

This will give you what you want if you don't care which phone is returned. If you have a way of determining the first phone, we can adjust.

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