0

我有一个超级简单的查询SELECT TIME, VALUE, TYPE from mylog

我的查询显示下一个:

| TIME            | VALUE |      TYPE |
 -------------------------------------
| 2013-09-26 04:45|  18.7 |         1 |
| 2013-09-26 04:45|  84.5 |         2 |
| 2013-09-26 05:00|  18.6 |         1 |
| 2013-09-26 05:00|  84.7 |         2 |

但我希望查询显示下一个:

| TIME            | VALUE A |  VALUE B  |  
 ----------------------------------------
| 2013-09-26 04:45|  18.7   |  85.5     |  
| 2013-09-26 05:00|  18.6   |  84.7     |  

在哪里和VALUE AType = 1VALUE BType = 2

所有数据都存储在一个表中,并包含以下列:

TIME, VALUE, TYPE
4

2 回答 2

1
SELECT TIME,
MIN(CASE WHEN TYPE= '1' THEN VALUE ELSE NULL END)   AS VALUE_A,
MIN(CASE WHEN TYPE= '2' THEN VALUE ELSE NULL END)   AS VALUE_B From mylog GROUP BY time

SQL小提琴

于 2013-10-27T18:12:06.077 回答
1
SELECT
  `TIME`,
  MIN(IF(`TYPE`=1,`VALUE`,99999999)) AS `Value A`,
  MIN(IF(`TYPE`=2,`VALUE`,99999999)) AS `Value B`
FROM mylog
GROUP BY `TIME`

带有99999999“没有可用价值”的意思

编辑

我不使用NULL,因为它会拒绝我MIN(或MAX)。如果您想要 NULL 代替,则必须将其包裹在

SELECT `TIME`, 
  IF(`Value A`=99999999,NULL,`Value A`),
  IF(`Value B`=99999999,NULL,`Value B`)
FROM (
   -- ...
) AS baseview
于 2013-10-27T18:13:10.893 回答