![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
How can I achieve the following: Suppose I have a table with a colum holding a date. Furthermore, I have a specific date t. Now I want to select all colums whose dates are before t and that are closest to t. |
#3
| |||
| |||
|
|
Ulrich Scholz <d7 (AT) thispla (DOT) net> wrote: How can I achieve the following: Suppose I have a table with a colum holding a date. Furthermore, I have a specific date t. Now I want to select all colums whose dates are before t and that are closest to t. SELECT ... WHERE date_column < t ORDER BY date_column DESC LIMIT 1 XL |
#4
| |||
| |||
|
|
On 06-09-2011 17:08, Axel Schwenke wrote: Ulrich Scholz <d7 (AT) thispla (DOT) net> wrote: How can I achieve the following: Suppose I have a table with a colum holding a date. Furthermore, I have a specific date t. Now I want to select all colums whose dates are before t and that are closest to t. SELECT ... WHERE date_column < t ORDER BY date_column DESC LIMIT 1 This will fail when there are more records which match the date in date_column that is found. |
#5
| |||
| |||
|
|
Maybe you have more information than I have, but Ulrich did not say he wants *all rows* that are below and next to t. He speaks of columns and in the topic he asks to find "a date ..." which I read as "some row for that date" |
#6
| |||
| |||
|
|
On 6 Sep., 22:31, Axel Schwenke <axel.schwe... (AT) gmx (DOT) de> wrote: Maybe you have more information than I have, but Ulrich did not say he wants *all rows* that are below and next to t. He speaks of columns and in the topic he asks to find "a date ..." which I read as "some row for that date" Sorry. Of course what I meant to say was "I want to select all *rows* whose dates are before t and that are closest to t." |
|
Thanks for your help. Ulrich |
#7
| |||
| |||
|
|
On 06-09-2011 17:08, Axel Schwenke wrote: Ulrich Scholz <d7 (AT) thispla (DOT) net> wrote: How can I achieve the following: Suppose I have a table with a colum holding a date. Furthermore, I have a specific date t. Now I want to select all colums whose dates are before t and that are closest to t. SELECT ... WHERE date_column < t ORDER BY date_column DESC LIMIT 1 XL This will fail when there are more records which match the date in date_column that is found. SELECT .... WHERE date_column = ( SELECT date_column FROM .. WHERE date_column < t ORDER BY data column DESC LIMIT 1) |
#8
| |||
| |||
|
|
Ulrich Scholz wrote: On 6 Sep., 22:31, Axel Schwenke <axel.schwe... (AT) gmx (DOT) de> wrote: Maybe you have more information than I have, but Ulrich did not say he wants *all rows* that are below and next to t. He speaks of columns and in the topic he asks to find "a date ..." which I read as "some row for that date" Sorry. Of course what tI meant to say was "I want to select all *rows* whose dates are before t and that are closest to t." It may be your english is at fault but the adjective 'closest' implies uniqueness. I.e. give the question 'which is closest to New York' there is only one answer from Washington, Madrid, Tokyo, London... |
|
Thanks for your help. Ulrich |
#9
| |||
| |||
|
|
Ulrich Scholz wrote: On 6 Sep., 22:31, Axel Schwenke <axel.schwe... (AT) gmx (DOT) de> wrote: Maybe you have more information than I have, but Ulrich did not say he wants *all rows* that are below and next to t. He speaks of columns and in the topic he asks to find "a date ..." which I read as "some row for that date" Sorry. Of course what I meant to say was "I want to select all *rows* whose dates are before t and that are closest to t." It may be your english is at fault but the adjective 'closest' implies uniqueness. I.e. give the question 'which is closest to New York' there is only one answer from Washington, Madrid, Tokyo, London... |
#10
| |||
| |||
|
|
How can I achieve the following: Suppose I have a table with a colum holding a date. Furthermore, I have a specific date t. Now I want to select all colums whose dates are before t and that are closest to t. Example: The table holds rows with entries t1, t2, t3 in the date column with t1< t2< t3. t is between t2 and t3. Therefore, I want to select t2. I can imagine two apporaches: 1) First, select all rows that have a date before t with<=. Then, select all rows of the former result that have the latest date with max. 2) Include another column that holds dates. The second date of a row holds the "next" date. The two dates then indicate the "time range" of the row. In the example above, one could then select the correct rows with BETWEEN in one step. Approach 1) should work fine. But somehow I feel like it wastes performance. The number of rows can become very large. Approach 2) works in my case because new data will always have dates "after" old data. Therefore, only the second date entry of the "latest" old rows have to be updated. Is there a better way to achive my goal? Thanks, Ulrich |
|
id | eventdate | event | +----+------------+--------------+ 6 | 2011-09-03 | Clean Garage | 7 | 2011-09-03 | Wash car | 8 | 2011-09-03 | Mow lawn | +----+------------+--------------+ |
![]() |
| Thread Tools | |
| Display Modes | |
| |