我正在从子选择中选择一些东西,而这又是一个总和列表。现在我想选择base_unit列,其中包含度量单位。我似乎无法将base_unit添加到子选择中,因为它不适用于GROUP BY语句。
SELECT to_char(a.pow * f_unit_converter(base_unit, '[W]'), '000.00')
FROM (
SELECT sum (time_value) AS pow
FROM v_value_quarter_hour
WHERE
mp_id IN (SELECT mp_id FROM t_mp WHERE mp_name = 'AC') AND
(now() - time_stamp < '5 day')
GROUP BY time_stamp
ORDER BY time_stamp DESC
) a
LIMIT 1
我在哪里/如何从t_mp表中为每个总和选择base_unit,以便我可以将它传递给f_unit_converter函数?
非常感谢, MRB
答案 0 :(得分:1)
假设您选择的所有行都具有相同的base_unit
,您应该可以将其添加到您的SELECT
和 GROUP BY
-query。
答案 1 :(得分:1)
SELECT to_char(a.pow * f_unit_converter(a.base_unit, '[W]'), '000.00')
FROM (
SELECT sum (time_value) AS pow, t_mp.base_unit
FROM v_value_quarter_hour
inner join t_mp on (v_value_quarter_hour.mp_id = t_mp.mp_id)
WHERE
t_mp.mp_name = 'AC' AND
(now() - time_stamp < '5 day')
GROUP BY time_stamp, base_unit
ORDER BY time_stamp DESC
) a
LIMIT 1
答案 2 :(得分:0)
使用INNER JOIN
代替IN
。像这样的东西
SELECT to_char(a.pow * f_unit_converter(base_unit, '[W]'), '000.00') FROM (
SELECT sum (time_value), base_unit AS pow
FROM v_value_quarter_hour
INNER JOIN t_mp ON v_value_quarter_hour.mp_id = t_mp.mp_id
WHERE mp_name = 'AC' AND
now() - time_stamp < '5 day'
GROUP BY time_stamp, base_unit
ORDER BY time_stamp DESC ) a LIMIT 1