Solved

MySQL Query Question

Posted on 2007-04-01
6
407 Views
Last Modified: 2008-02-01
I have a table that stores historical statistics.  I'm using it to query timeline data.

+----------------------+------------+------+-----+---------------------+----------------+
| Field                | Type       | Null | Key | Default             | Extra          |
+----------------------+------------+------+-----+---------------------+----------------+
| id      | bigint(20) | NO   | PRI | NULL                | auto_increment |
| person_id            | bigint(20) | NO   | MUL |                     |                |
| time_hack            | datetime   | NO   |     | 0000-00-00 00:00:00 |                |
| statA       | int(4)     | YES  |     | NULL                |                |
| statB | int(4)     | YES  |     | NULL                |                |
| statC        | int(4)     | YES  |     | NULL                |                |
| statD | int(4)     | YES  |     | NULL                |                |
| statE | int(4)     | YES  |     | NULL                |                |
+----------------------+------------+------+-----+---------------------+----------------+

The query I'm using:

            SELECT * FROM
            `performance_history`
            WHERE
            `performance_history`.`time_hack` >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY);

The problem I'm finding multiple records per person per day.  It appears the table records were inputted hourly rather than daily.    So I want to modify my query so I only retrieve one record per person per day.
The following table shows 4 records for person #1 generated on SEP, 15 2006.    Aside from the date, all the data is the same so I only need one of these records for this day.


|              15 |        1 | 2006-09-15 01:46:58 |                        2 |            38 |                   77 |                  100 |
|              16 |        1 | 2006-09-15 02:46:58 |                        2 |            38 |                   77 |                  100 |
|              17 |        1 | 2006-09-15 03:46:58 |                        2 |            38 |                   77 |                  100 |
|              18 |        1 | 2006-09-15 04:46:58 |                        2 |            38 |                   77 |                  100 |              


How do I modify my query so I retrieve only 1 record per person, per day?
0
Comment
Question by:tmonteit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 27

Assisted Solution

by:Cornelia Yoder
Cornelia Yoder earned 150 total points
ID: 18832658
           SELECT * FROM
            `performance_history`
            WHERE
            `performance_history`.`time_hack` >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) GROUP BY person_id;
0
 

Author Comment

by:tmonteit
ID: 18832994
yodercm,

your recommendation only produces one record per person for the entire 30 day interval.  I need 1 record per day per person for the interval.  

Assume the table has these values.  The result should produce 2 records. One for the 15th, and another for the 16th.

|              15 |        1 | 2006-09-15 01:46:58 |                        2 |            38 |                   77 |                  100 |
|              16 |        1 | 2006-09-15 02:46:58 |                        2 |            38 |                   77 |                  100 |
|              17 |        1 | 2006-09-15 03:46:58 |                        2 |            38 |                   77 |                  100 |
|              18 |        1 | 2006-09-15 04:46:58 |                        2 |            38 |                   77 |                  100 |  
|              19 |        1 | 2006-09-16 01:46:58 |                        3 |            20 |                   74 |                  100 |
|              20 |        1 | 2006-09-16 02:46:58 |                        3 |            20 |                   74 |                  100 |
|              21 |        1 | 2006-09-16 03:46:58 |                        3 |            20 |                   74 |                  100 |
|              22 |        1 | 2006-09-16 04:46:58 |                        3 |            20 |                   74 |                  100 |  


How do I do that?

0
 
LVL 35

Accepted Solution

by:
Raynard7 earned 350 total points
ID: 18833276
If you did,

SELECT ph.person_id, date(ph.`time_hack`), min(ph.id) mi
FROM
            `performance_history` ph
WHERE
            ph.`time_hack` >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY)
GROUP BY
            1, 2

it would give you the ids that you are after, if you did

select
     ph1.*
from
(
            SELECT ph.person_id, date(ph.`time_hack`), min(ph.id) mi
            FROM `performance_history` ph
            WHERE ph.`time_hack` >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY)
            GROUP BY 1,2
) x1 inner join `performance_history` ph1 on x1.mi = ph1.id

it would give you all the information, 1 per person per day
0
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 

Author Comment

by:tmonteit
ID: 18834111
Raynard, can you explain that more?  MySql Beginner here... is there another table I'm supposed to create?
0
 
LVL 1

Expert Comment

by:krisgrp
ID: 18834462
Hi,
I think u can use the group by cluase to get the resultant u want..
like this.
  SELECT person_id, date(`time_hack`)
            FROM `performance_history`
            WHERE `time_hack` >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY)
            GROUP BY person_id,`time_hack` order by id`;
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 18839758
hi, mine should be sufficient

they are all referring to the same table - just doing sub queries on it, (meaning a query within a query) and aliasing (calling a query by an easier to refer to name)

krisgrp - as you will notice that this is what my first query does, however this will not show all the records that the author wants to see - it will only show you the date and the person_id - if you want more you need an id corresponding to one of those records (using a min for example) then linking back onto the same table to pull the rows with that given id
0

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question