dbTalk Databases Forums  

Help with a query

comp.databases.mysql comp.databases.mysql


Discuss Help with a query in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Joseph Hesse
 
Posts: n/a

Default Help with a query - 08-08-2011 , 10:37 AM






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;
+------------+-----------------+--------+
Quote:
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

Reply With Quote
  #2  
Old   
sheldonlg
 
Posts: n/a

Default Re: Help with a query - 08-08-2011 , 10:46 AM






On 8/8/2011 11:37 AM, Joseph Hesse wrote:
Quote:
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

Reply With Quote
  #3  
Old   
Joseph Hesse
 
Posts: n/a

Default Re: Help with a query - 08-08-2011 , 10:59 AM



On 08/08/2011 10:46 AM, sheldonlg wrote:
Quote:
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.

Reply With Quote
  #4  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: Help with a query - 08-08-2011 , 11:05 AM



Joseph Hesse:

Quote:
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/...group-row.html



--
Erick

Reply With Quote
  #5  
Old   
Joseph Hesse
 
Posts: n/a

Default Re: Help with a query - 08-08-2011 , 12:24 PM



On 08/08/2011 11:05 AM, Erick T. Barkhuis wrote:
Quote:
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/...group-row.html



Sorry, I have egg on my face.
Joe

Reply With Quote
  #6  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Help with a query - 08-08-2011 , 12:38 PM



On 2011-08-08 17:59, Joseph Hesse wrote:
Quote:
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

Reply With Quote
  #7  
Old   
Joseph Hesse
 
Posts: n/a

Default Re: Help with a query - 08-08-2011 , 07:15 PM



On 08/08/2011 12:38 PM, Lennart Jonsson wrote:
Quote:
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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.