简体   繁体   中英

How do i group for the MAX of a string, while keeping the string instead of its character count?

How do I turn this

 +--------+---------------+           
 | ID     |  name         |           
 |--------|---------------|           
 +   1    | tim           |           
 +-------------------------           
 |   1    | timothy       |           
 +--------++--------------+           
 |   1    | timmy         |           
 +--------|---------------|           
 |   2    | jane          +           
 +--------+---------------+           

into this?

 +--------+---------------+           
 | ID     |  name         |           
 |--------|---------------|           
 +   1    | timothy       |           
 +-------------------------           
 |   2    | jane          |           
 +--------++--------------+           

The problem seems to be one of using MAX on a string, while keeping the string and grouping by ID.

FWIW, the table actually has 7K rows and about 40 columns; I don't think that should matter, but I'm mentioning it just in case.

On my existing MAX efforts, I'm getting integers to consolidate, but not strings...

Select Id, Name
From MyTable
    Join    (
            Select Id, Max( Char_Length( name ) ) As NameLen
            From MyTable
            Group By Id
            ) As Z
        On Z.Id = MyTable.Id
            And Z.NameLen = Char_Length( MyTable.Name )

Of course, this will not handle a scenario like Rob , Bob . In that case, both would be returned.

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