我有这个案例查询,它将行转换为列。我这样的其他案例查询工作得很好,但在其他查询上 我得到了值的平均值,为此我不需要得到平均值,而是实际值。
这是我的疑问:
SELECT FROM_UNIXTIME(t_stamp/1000, '%Y-%m-%d %l:%i %p') as t_stamp,
ROUND((CASE WHEN id = '1' THEN value END),2) Col1,
ROUND((CASE WHEN id = '2' THEN value END),2) Col2
FROM table
WHERE id IN ("1", "2") and
DATE(from_unixtime(t_stamp/1000)) BETWEEN '2014-04-01'
AND '2014-04-01'
and value is not null
GROUP BY
DATE(from_unixtime(t_stamp/1000)), HOUR(from_unixtime(t_stamp/1000)) DIV 3
我首先使用第一个案例进行了测试
SELECT FROM_UNIXTIME(t_stamp/1000, '%Y-%m-%d %l:%i %p') as t_stamp,
ROUND((CASE WHEN id = '1' THEN value END),2) Col1
这工作并向我显示了值,但是当我尝试添加第二列时,
SELECT FROM_UNIXTIME(t_stamp/1000, '%Y-%m-%d %l:%i %p') as t_stamp,
ROUND((CASE WHEN id = '1' THEN value END),2) Col1,
ROUND((CASE WHEN id = '2' THEN value END),2) Col2
Col1上的输出都为空,并显示Col2的值。我需要它在两列上显示两个值。 我该如何解决这个问题?
谢谢
答案 0 :(得分:0)
在MAX
值上使用case
,因为您已在查询中使用group by
子句。
SELECT FROM_UNIXTIME(t_stamp/1000, '%Y-%m-%d %l:%i %p') as t_stamp,
ROUND( MAX( CASE WHEN id = '1' THEN value END ), 2 ) Col1,
ROUND( MAX( CASE WHEN id = '2' THEN value END ) ,2 ) Col2
FROM table
WHERE
id IN ( '1', '2' )
and DATE( from_unixtime( t_stamp / 1000 ) )
BETWEEN '2014-04-01' and '2014-04-01'
and value is not null
GROUP BY
DATE( from_unixtime( t_stamp / 1000 ) ),
HOUR( from_unixtime( t_stamp / 1000 ) )