开发者

How to eliminate duplicates from select query?

Before asking this question I searched using Google but I couldn't understand or maybe could not find a solution suitable for my situation.

So, I have one Table with 10 columns, I want to eliminate duplicates from select result. And in the result all columns should be presented which has unique userID's

+-----------------------------------+------+---------------------+---开发者_高级运维---+
| name                              | yr   |   some Columns      |userID|
+-----------------------------------+------+---------------------+------+
| abc                               | 2000 |                     |   10 |
| jack                              | 2000 |                     |   11 |
| dadas                             | 2000 |                     |   12 |
| jack                              | 2004 | .............       |   11 |
| jack                              | 2000 | ...........         |   11 |
| nell                              | 2006 | .............       |   13 |
| ......                            | 2000 | .............       |   1  |
| .............                     | 2000 | .............       |   2  |
| again                             | 2000 | .............       |   3  |
| again                             | 2000 |                     |   3  |
| .......                           | 1973 | .............       |   2  |
| abc                               | 2000 |                     |   10 |


If you don't need to keep different yrs, just use DISTINCT ON (FIELD_NAME)

SELECT DISTINCT ON (userID) userdID, name, yr FROM TABLE_NAME


Try this one:

SELECT * FROM TABLE_NAME GROUP BY userID


For PostgreSQL as well as SQL Server 2005+, DB2 and later versions of Oracle (9+), you can use the windowing function ROW_NUMBER()

select *
from
(
select *, ROW_NUMBER() over (partition by userID order by yr desc) rown
) X
where rown = 1


An easy one would be:

SELECT DISTINCT (userID) userdID, name, yr FROM TABLE_NAME
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜