嵌套的MySQL查询

时间:2012-08-16 18:48:14

标签: mysql sql nested

我目前有以下查询:

select min(nw_lpdTEMP) AS Min_Lkpmp_Temp_C,
  max(nw_lpdPH) AS Max_Lkpmp_PH,
(select format(nw_uvttlflw,7) from tag_history 
  where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
  Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59' order by nw_bpdcl2 limit 1) as Min_GPM, 
  format(min(nw_bpdcl2),7) as Min_Chlorine_Residual,
(select format(nw_uvttlflw,7) from tag_history
  where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
  Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59' order by nw_bpdcl2 desc limit 1) as Max_GPM,
  format(max(nw_bpdcl2),7) as Max_Chlorine_Residual from tag_history 
  where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60) 
  Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59'

我正在寻找是否可以添加以下查询,该查询返回24小时之间每分钟加仑流量(nw_uvttlflw)的总和。我可以运行我想要添加到上述查询中的(求和查询)查询,它会返回我想要的内容。我当前的问题是尝试使用上面的查询嵌套它,因此它返回总和(nw_uvttlflw)作为原始较大查询中的最后一列。 (抱歉我的嵌套SQL查询缺乏质量格式,因为我不是最精通冗长的查询)

我想附加的查询在这里:

select format(sum(nw_uvttlflw),7) as Total_flow from tag_history
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60) 
Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59';

非常感谢您的时间和精力。我一直试图把它拼凑一段时间,但是我的语法不能正确。感谢

-Mark

1 个答案:

答案 0 :(得分:0)

这是你要找的吗?

select min(nw_lpdTEMP) AS Min_Lkpmp_Temp_C,
  max(nw_lpdPH) AS Max_Lkpmp_PH,
  (select format(nw_uvttlflw,7) from tag_history 
    where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
    Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59' order by nw_bpdcl2 limit 1) as Min_GPM, 
  format(min(nw_bpdcl2),7) as Min_Chlorine_Residual,
  (select format(nw_uvttlflw,7) from tag_history
    where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
    Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59' order by nw_bpdcl2 desc limit 1) as Max_GPM,
  format(max(nw_bpdcl2),7) as Max_Chlorine_Residual,
  (select format(sum(nw_uvttlflw),7) from tag_history
    where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60) 
    Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59') as Total_flow
from tag_history 
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60) 
  Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59'