Jump to content

Recommended Posts

I think the query would explain it better than I would ever be able to:

SELECT * FROM table WHERE col1 <= value GROUP BY col2 ORDER BY col1 DESC

 

I would almost think that worked the way I want to, but it does not...

 

What I want is to select one row per unique value in col2, which is easy.

I also want it to only select rows where col1 is equal and greater than a value (variable), which also is easy and works just fine.

The problem and what I can't seem to do is to make it only select the rows with the highest col1 values for each of the unique values in col2...

 

Can anyone help me please?

Link to comment
https://forums.phpfreaks.com/topic/222303-a-bit-complicated-select-query/
Share on other sites

hmm, sorry, but I still can't seem to figure it out >_<

I will give a graphical example:

 

table:

| col1 | col2 |

| 1 | 1 |

| 1 | 2 |

| 1 | 3 |

| 1 | 4 |

| 1 | 5 |

| 2 | 1 |

| 2 | 2 |

| 2 | 3 |

| 3 | 5 |

| 4 | 1 |

| 4 | 2 |

| 5 | 1 |

| 5 | 3 |

| 6 | 1 |

| 6 | 3 |

| 6 | 4 |

| 6 | 5 |

| 7 | 2 |

 

 

I got a variable, let's say it is 4.

I want to select data from rows that are equal or lower than the variable.

 

So no rows with col1 values over 4 should be selected!

 

I also want to be a bit more specific about what data that I want.

I don't want any duped col2 values, but I don't just want it to filter away rows that I might be interested in.

I want it to select the row with the highest col1 value, but remember it must be equal or less than 4.

 

 

The result of this query should be:

 

| col1 | col2 |

| 4 | 1 |

| 4 | 2 |

| 2 | 3 |

| 1 | 4 |

| 3 | 5 |

 

I don't really are if it is sorted by col2, but as long as it gives these rows back...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.