Subquery on timestampdiff only allows for 1 result, how do I scale this for all results? I'm currently writing up a query which is meant to output data to show curves of power profiles, and I'm facing a slight problem. So far, I've managed to do this:

select datetime, PLANT_STATUS, INCOMER_PWR, PLANT_DEMAND, PLANT_OUTPUT
from d909 
where 
    (PLANT_STATUS &gt;=400 AND PLANT_STATUS &lt; 500) 
    OR 
    (timestampdiff(minute, 
                   (select min(datetime) 
                    from d909 
                    where PLANT_STATUS &gt;= 400 AND PLANT_STATUS &lt; 500
                   ), 
                   datetime) 
         &lt;= -1
     ) 
     OR 
     (timestampdiff(minute, 
                    (select max(datetime) 
                     from d909 
                     where PLANT_STATUS &gt;= 400 AND PLANT_STATUS &lt; 500
                    ), 
                    datetime) 
         &gt;= 1
     );


How do I get the timestampdiff functions to resolve for ANY data, not just the min and max? I realise subqueries normally have 1 row, but I would like to have multiple rows output from it.
Based on your comment try something like this I think

select datetime, PLANT_STATUS, INCOMER_PWR, PLANT_DEMAND, PLANT_OUTPUT
from d909 as d1
where 
    (PLANT_STATUS &gt;=400 AND PLANT_STATUS &lt; 500) 
UNION ALL
select datetime, PLANT_STATUS, INCOMER_PWR, PLANT_DEMAND, PLANT_OUTPUT
from d909 as d1
where (select COUNT(*) 
        from d909 as d2
        -- find all records within a minute of the first set of records
        where abs(DATEDIFF(MI,d2.datetime,d1.datetime)) &lt;= 1
        -- not sure if this status check required
        AND (PLANT_STATUS &gt;=400 AND PLANT_STATUS &lt; 500) 
        ) &gt; 1
AND (PLANT_STATUS &gt;=400 AND PLANT_STATUS &lt; 500)