Solved

Select bigger string from 2 or more strings in a column

Posted on 2009-06-27
8
383 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
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

809 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