Help with a query #5

  • Follow


I have a table where I keep track of my employee's weight and the date 
their weight was taken.  Here is the table.

mysql> select * from Employees;
+------------+-----------------+--------+
| EmployeeID | DateWeightTaken | Weight |
+------------+-----------------+--------+
|          1 | 2011-01-01      |    100 |
|          1 | 2011-02-01      |    110 |
|          1 | 2011-03-01      |    120 |
|          2 | 2011-01-01      |    200 |
|          2 | 2011-02-01      |    210 |
|          2 | 2011-03-01      |    220 |
|          2 | 2011-04-01      |    230 |
+------------+-----------------+--------+

I would like to query the table to select the most recent weights for 
all my employees.

I have not been able to get a Google solution.  Any references or 
pointers would be appreciated.

Thank you,
Joe

0
Reply joeh8556 (19) 8/8/2011 3:37:24 PM

On 8/8/2011 11:37 AM, Joseph Hesse wrote:
> I have a table where I keep track of my employee's weight and the date
> their weight was taken. Here is the table.
>
> mysql> select * from Employees;
> +------------+-----------------+--------+
> | EmployeeID | DateWeightTaken | Weight |
> +------------+-----------------+--------+
> | 1 | 2011-01-01 | 100 |
> | 1 | 2011-02-01 | 110 |
> | 1 | 2011-03-01 | 120 |
> | 2 | 2011-01-01 | 200 |
> | 2 | 2011-02-01 | 210 |
> | 2 | 2011-03-01 | 220 |
> | 2 | 2011-04-01 | 230 |
> +------------+-----------------+--------+
>
> I would like to query the table to select the most recent weights for
> all my employees.
>
> I have not been able to get a Google solution. Any references or
> pointers would be appreciated.
>
> Thank you,
> Joe
>

add "ORDER BY DateWeightTaken DESC LIMIT HowManyYouWantReturned" to the 
end of your query.

-- 
Shelly
0
Reply sheldonlg1 (118) 8/8/2011 3:46:30 PM


On 08/08/2011 10:46 AM, sheldonlg wrote:
> On 8/8/2011 11:37 AM, Joseph Hesse wrote:
>> I have a table where I keep track of my employee's weight and the date
>> their weight was taken. Here is the table.
>>
>> mysql> select * from Employees;
>> +------------+-----------------+--------+
>> | EmployeeID | DateWeightTaken | Weight |
>> +------------+-----------------+--------+
>> | 1 | 2011-01-01 | 100 |
>> | 1 | 2011-02-01 | 110 |
>> | 1 | 2011-03-01 | 120 |
>> | 2 | 2011-01-01 | 200 |
>> | 2 | 2011-02-01 | 210 |
>> | 2 | 2011-03-01 | 220 |
>> | 2 | 2011-04-01 | 230 |
>> +------------+-----------------+--------+
>>
>> I would like to query the table to select the most recent weights for
>> all my employees.
>>
>> I have not been able to get a Google solution. Any references or
>> pointers would be appreciated.
>>
>> Thank you,
>> Joe
>>
>
> add "ORDER BY DateWeightTaken DESC LIMIT HowManyYouWantReturned" to the
> end of your query.
>
This doesn't quite work.  The query
SELECT * from Employees ORDER BY DateWeightTaken DESC LIMIT 1;
returns only one item.  I want a query that would produce:
1 2011-03-01 120
2 2011-04-01 230
Thank you.
0
Reply joeh8556 (19) 8/8/2011 3:59:21 PM

Joseph Hesse:

>I would like to query the table to select the most recent weights for
>all my employees.

You've been asking almost the same question in June, and been given:
http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html



-- 
Erick
0
Reply Erick 8/8/2011 4:05:28 PM

On 08/08/2011 11:05 AM, Erick T. Barkhuis wrote:
> Joseph Hesse:
>
>> I would like to query the table to select the most recent weights for
>> all my employees.
>
> You've been asking almost the same question in June, and been given:
> http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html
>
>
>
Sorry, I have egg on my face.
Joe
0
Reply joeh8556 (19) 8/8/2011 5:24:28 PM

On 2011-08-08 17:59, Joseph Hesse wrote:
> On 08/08/2011 10:46 AM, sheldonlg wrote:
>> On 8/8/2011 11:37 AM, Joseph Hesse wrote:
>>> I have a table where I keep track of my employee's weight and the date
>>> their weight was taken. Here is the table.
>>>
>>> mysql> select * from Employees;
>>> +------------+-----------------+--------+
>>> | EmployeeID | DateWeightTaken | Weight |
>>> +------------+-----------------+--------+
>>> | 1 | 2011-01-01 | 100 |
>>> | 1 | 2011-02-01 | 110 |
>>> | 1 | 2011-03-01 | 120 |
>>> | 2 | 2011-01-01 | 200 |
>>> | 2 | 2011-02-01 | 210 |
>>> | 2 | 2011-03-01 | 220 |
>>> | 2 | 2011-04-01 | 230 |
>>> +------------+-----------------+--------+
>>>
>>> I would like to query the table to select the most recent weights for
>>> all my employees.
>>>
>>> I have not been able to get a Google solution. Any references or
>>> pointers would be appreciated.
>>>
>>> Thank you,
>>> Joe
>>>
>>
>> add "ORDER BY DateWeightTaken DESC LIMIT HowManyYouWantReturned" to the
>> end of your query.
>>
> This doesn't quite work.  The query
> SELECT * from Employees ORDER BY DateWeightTaken DESC LIMIT 1;
> returns only one item.  I want a query that would produce:
> 1 2011-03-01 120
> 2 2011-04-01 230
> Thank you.

Assuming EmployeeID, DateWeightTaken is a candidate key

select EmployeeID, max(DateWeightTaken) as DateWeightTaken
from Employees
group by EmployeeID

will give you the last date per employee. You can join this with
Employees to determine the weight.


/Lennart
0
Reply Erik.Lennart.Jonsson (181) 8/8/2011 5:38:20 PM

On 08/08/2011 12:38 PM, Lennart Jonsson wrote:
> On 2011-08-08 17:59, Joseph Hesse wrote:
>> On 08/08/2011 10:46 AM, sheldonlg wrote:
>>> On 8/8/2011 11:37 AM, Joseph Hesse wrote:
>>>> I have a table where I keep track of my employee's weight and the date
>>>> their weight was taken. Here is the table.
>>>>
>>>> mysql>  select * from Employees;
>>>> +------------+-----------------+--------+
>>>> | EmployeeID | DateWeightTaken | Weight |
>>>> +------------+-----------------+--------+
>>>> | 1 | 2011-01-01 | 100 |
>>>> | 1 | 2011-02-01 | 110 |
>>>> | 1 | 2011-03-01 | 120 |
>>>> | 2 | 2011-01-01 | 200 |
>>>> | 2 | 2011-02-01 | 210 |
>>>> | 2 | 2011-03-01 | 220 |
>>>> | 2 | 2011-04-01 | 230 |
>>>> +------------+-----------------+--------+
>>>>
>>>> I would like to query the table to select the most recent weights for
>>>> all my employees.
>>>>
>>>> I have not been able to get a Google solution. Any references or
>>>> pointers would be appreciated.
>>>>
>>>> Thank you,
>>>> Joe
>>>>
>>>
>>> add "ORDER BY DateWeightTaken DESC LIMIT HowManyYouWantReturned" to the
>>> end of your query.
>>>
>> This doesn't quite work.  The query
>> SELECT * from Employees ORDER BY DateWeightTaken DESC LIMIT 1;
>> returns only one item.  I want a query that would produce:
>> 1 2011-03-01 120
>> 2 2011-04-01 230
>> Thank you.
>
> Assuming EmployeeID, DateWeightTaken is a candidate key
>
> select EmployeeID, max(DateWeightTaken) as DateWeightTaken
> from Employees
> group by EmployeeID
>
> will give you the last date per employee. You can join this with
> Employees to determine the weight.
>
>
> /Lennart
Thank you,
That was very helpful.
Joe

0
Reply joeh8556 (19) 8/9/2011 12:15:37 AM

6 Replies
46 Views

(page loaded in 0.084 seconds)

Similiar Articles:













7/10/2012 4:04:56 PM


Reply: