当使用sum情况时,000仅转换为0

时间:2017-11-16 01:22:59

标签: mysql sql

table1中有以下数据:

id  date        value
1   10/09       500
2   10/09       400
3   10/09       300
4   11/09       200
5   11/09       100
6   11/09       000

我想在show table中设置上面的数据:

date  | d1  |   d2   |  d3  |   d4  |   d5    | d6
----------------------------------------------------
10/09 | 500 |   400  |  300 |       |         |
----------------------------------------------------
11/09 |     |        |      |   200 |   100 |   000  
----------------------------------------------------

我正在使用以下sql查询。

select date,
       sum(case when id = 1 then value end) as d1,
       sum(case when id = 2 then value end) as d2,
       sum(case when id = 3 then value end) as d3,
       sum(case when id = 4 then value end) as d4,
       sum(case when id = 5 then value end) as d5,
       sum(case when id = 6 then value end) as d6
from table1 GROUP BY date;

当我使用上述查询时出现问题,在输出表中,对于d6列而不是000,该值仅变为0。如何克服这个问题?

Check SQL Fiddle here

1 个答案:

答案 0 :(得分:2)

如果这是一个字符串值,则// In the non-vectorized original code, A is an array of many floating-point // numbers, which are calculated one at a time. Now they're packed // into a vector and processed four at a time ...calculate A... if (A > 10.f) { A = A+5.f; } else { A = A+10.f; } 可能会执行:

max()

您似乎并不想要select date, max(case when id = 1 then value end) as d1, max(case when id = 2 then value end) as d2, max(case when id = 3 then value end) as d3, max(case when id = 4 then value end) as d4, max(case when id = 5 then value end) as d5, max(case when id = 6 then value end) as d6 from table1 group by date;

Here是一个SQL小提琴。请注意,必须将值作为字符串插入到列中。