开发者

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

开发者 https://www.devze.com 2023-03-13 15:59 出处:网络
How do I turn this +--------+---------------+ ID|name| --------|---------------| +1| tim| +-------------------------

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 t开发者_StackOverflow中文版hat 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.

0

精彩评论

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

关注公众号