Solved

Select bigger string from 2 or more strings in a column

Posted on 2009-06-27
8
388 Views
Last Modified: 2012-05-07
I want to make a group seletc that retrieves a distinct string from col1 and the larger string from col2.....
ex.
[col1]           [col2 ]  [col3]
1111          aaa        3
2222          bbb         3
1111          ccccc     5
2222          ddddd     5
3333          eeee       4
3333          f              1

results wanted!!
[col1]   [col2]  
1111    ccccc
2222    ddddd  
3333    eeee

col1 and col3 are string , col3 int with the length of the string in col2.....
i made several mysql query and sometimes retrieves the larger string, others not.

i know how to do this with php but i want to doit in a mysql query, maybe i need a sub query...
Thanks.
0
Comment
Question by:dmonzon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 63 total points
ID: 24729128
what version of mysql?

let's try this:
select t.*
  from yourtable t
 where len(col2) = ( SELECT max(len(i.col2)) from yourtable i where i.col1 = t.col1 )

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24729129
SELECT Col1, MAX(Col2)Col2
FROM urTable
GROUP BY Col1
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24729183
I think you're looking for this:
select a.col1, a.col2
from yourtable a
join (select col1, max(col3) as mcol3 from yourtable group by col1) b on a.col1 = b.col1 and a.col3 = b.mcol3

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 32

Assisted Solution

by:awking00
awking00 earned 62 total points
ID: 24736780
See attached.
select col1, col2
from yourtable y,
(select col1, max(len(col2)) col2_len
 from yourtable
 group by col1) x
where y.col1 = x.col1
  and len(y.col2) = x.col2_len;

Open in new window

0
 
LVL 1

Author Comment

by:dmonzon
ID: 24786500
sorry for the delay, i already resolve this the same day and i forgot to close the question, any way i will try your answers and let you know .....
thanks
0
 
LVL 41

Expert Comment

by:ralmada
ID: 25039499
Angel, Not sure why my query will not help the asker. Can you please advise?
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question