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: Hibernate305: delete query fails with "must begin with SELECT or ...... 0.5, JDK 1.4.2, and Oracle 9i.I'm following the Hibernate 3.0.5 docs to build a query to ... but here is some code from my program> i am working on that might help you ... How to query to get records of 3 consecutive months of data in the ...... months I need all records that has any 3 consecutive months of data. Any help is ... Query to find all rows that has 3 consecutive months of data in ..... 3 consecutive ... Query with NULL - comp.databases.oracle.serverUnusual Query Help Request - comp.databases.mysql Let "for" loop treat "null" as empty List ... Do you wish to antagonize those from whom you request help? ... Query with ... Cannot Import data into SQL Server database from FileMaker Pro 5.5 ...... ve got some strange problem, I need to import file from Filemaker Pro 5.5 ... interesting thing that I've done this before using the sam file.with no problems,I used query ... [NETBEANS] How to display query result in a grid - comp.lang.java ...I'm using Netbeans 5.5. Ifsomeone of u can ... ... I've a data source, i've to make a query on this data ... Netbeans and importing Package - comp.lang.java.help ... Query to find all rows that has 3 consecutive months of data in ...Query to find all rows that has 3 consecutive months of data in the last 6 months ... months I need all records that has any 3 consecutive months of data. Any help is ... Visual Query Builder WHERE Clause - comp.soft-sys.matlab ...This command in the visual query builder it works no problem: SELECT ALL "Record ... just a record where some field matches a given string? Thanks for your help! How to see Storage ID (serial number) of DS4800 under aix? - comp ...> > Help will be much appreaciated. > Thanks Not sure if this helps you ... # pcmpath query device DEV#: 5 DEVICE NAME: hdisk5 TYPE: 1815 ALGORITHM: Load ... Efficient way to delete a large amount of records from a big table ...5) Running the query more often, and with LIMIT, divided the lockout into ... I expect the primary key will not help for this query. Actually, it will hurt ... SAS / SQL help with arrays & joins - comp.soft-sys.sasSAS / SQL help with arrays & joins - comp.soft-sys.sas ... I have what I think is a ... Proc SQL – A Primer for SAS Programmers The Structured Query Language ... Help w/Query: Filter by Date Published (Last 5 Days): filter, date ...I need to filter this query show that it shows published placments from the last 5 days The published date field is called published SELECT dbo.placements ... Help w/Query: Filter by Date Published (Last 5 Days): date, queryNot there yet sorry I had to setup a test environment: SELECT dbo.placements.id, dbo.placements.eventdate, dbo.placements.product, dbo.categories.account ... 7/10/2012 4:04:56 PM
|