Selecting two sets of data individually grouped
Using the following query to select data from a table in 15 minute intervals where a point has state=1:
SELECT * , ROUND( UNIX_TIMESTAMP( log_date ) / ( 15 *60 ) ) AS period
FROM `site_node_point_states`
WHERE state =1
AND site_node_point_id = "11"
AND log_date
BETWEEN '2011-09-12 13:00:00'
AND '2011-09-12 13:00:00'
GROUP BY period
ORDER BY period ASC
This query does exactly what I want based on a single site_node_point_id
, however I would like to be able to add another site_node_point_id
and have that grouped by period
individually of the other site_node_point_id
data.
I am using PHP to make this query, and would like to try and limit it to a single query rather than looping through my current one with a different site_node_point_id
.
Obviously If I were to change AND site_node_point_id = "11"
to AND site_node_point_id = "11" OR site_node_point_id = "14"
it would order data from b开发者_开发百科oth site_node_point_id
14 and 11.
Is there a solution in SQL to do the GROUP BY
individually for each set of data based on the site_node_point_id
that I am selecting?
Use GROUP BY site_node_point_id, period
. That should group your results first by site_node_point_id, then period.
This might be what you are looking for:
SELECT * , ROUND( UNIX_TIMESTAMP( log_date ) / ( 15 *60 ) ) AS period
FROM `site_node_point_states`
WHERE state =1
AND site_node_point_id in ("11", "12")
AND log_date
BETWEEN '2011-09-12 13:00:00'
AND '2011-09-12 13:00:00'
GROUP BY site_node_point_id, period
ORDER BY site_node_point_id, period ASC
精彩评论