views:

45

answers:

3

Hi,

I am having some confusion with a mysql query. Any help appreciated.

My current query:

SELECT
  r.name
, r.network
, i.name
, d.dtime
, d.input
FROM router AS r
INNER JOIN interface AS i
ON r.rid = i.rid
INNER JOIN 1273118400_1_60 AS d
ON i.id = d.id
AND dtime BETWEEN 1273152000 AND 1273153800 WHERE i.status = "active"

Now I am using Unix_Timestamp for the time interval between which I am querying. Right now I am getting results for : 1273152000, 1273152001, 1273152002 and so on.

I want to get at an interval of 60 seconds, like : 1273152000, 1273152060, 1273152120 and so on.

Any idea how I could go about it????

Please help. Thank you,

Jillika

+2  A: 

I'm not sure if it can be simplified further, but you may want to try the following:

SELECT       r.name,
             r.network,
             i.name,
             d.dtime,
             d.input
FROM         router AS r
INNER JOIN   interface AS i ON (r.rid = i.rid)
INNER JOIN   (
                 SELECT      t.dtime, dt.input, dt.id
                 FROM        1273118400_1_60 AS t
                 INNER JOIN  (
                                SELECT dtime, id, input
                                FROM   1273118400_1_60
                             ) AS dt ON (dt.dtime = t.dtime AND dt.id = t.id)
                 GROUP BY    t.dtime DIV 60
             ) AS d ON (i.id = d.id)
WHERE        (d.dtime BETWEEN 1273152000 AND 1273153800) AND 
             (i.status = 'active');

Test case:

CREATE TABLE router (rid int, name varchar(20), network int);
CREATE TABLE interface (id int, rid int, status varchar(20), name varchar(10));
CREATE TABLE 1273118400_1_60 (id int, dtime int, input int);

INSERT INTO router VALUES (1, 'router A', 0);
INSERT INTO interface VALUES (1, 1, 'active', 'interface A');
INSERT INTO 1273118400_1_60 VALUES (1, 1273151999, 1);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152000, 2);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152001, 3);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152002, 4);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152059, 5);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152060, 6);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152061, 7);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152062, 8);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152119, 9);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152120, 10);
INSERT INTO 1273118400_1_60 VALUES (1, 1273152121, 11);

Result:

+----------+---------+-------------+------------+-------+
| name     | network | name        | dtime      | input |
+----------+---------+-------------+------------+-------+
| router A |       0 | interface A | 1273152000 |     2 |
| router A |       0 | interface A | 1273152060 |     6 |
| router A |       0 | interface A | 1273152120 |    10 |
+----------+---------+-------------+------------+-------+
3 rows in set (0.00 sec)
Daniel Vassallo
A: 

You're question is not overly clear, but try this anyhow:

SELECT r.name, r.network, i.name, d.dtime, d.input
FROM router r
INNER JOIN interface i
ON r.rid = i.rid
INNER JOIN 1273118400_1_60 AS d
ON i.id = d.id
WHERE d.time > 1273152000 AND d.time < 1273153800 
AND i.status = "active";
Evernoob
Hi,Thanks for the quick response.Will try to make it clearer:1273152000 = unixtimestamp for: 05/07/2010 09:201273153800 = unixtimestamp for: 05/07/2010 09:50I want to query for every minute interval (60 secs) as my table contains values for every second.dtime = time in unix_timestamp.any idea how I could do it?
jillika iyer
Hmm, I would consider perhaps creating a stored function to handle it if it's getting more complicated than that.
Evernoob
A: 
SELECT
r.name
,r.network
,i.name
, d.dtime
, 60*(floor(d.dtime/60)) dtime2
, d.input
FROM router AS r
INNER JOIN interface AS i
ON r.rid = i.rid
INNER JOIN 1273118400_1_60 AS d
ON i.id = d.id
AND dtime BETWEEN 1273152000 AND 1273153800 WHERE i.status = "active"
ceteras