你的第一个图是 是数据库的原始数据 还是你写查询语句进行的处理过的数据 如聚合函数?
如果数据库表的数据是这样的话 下面的sql语句
select showtime,isnull(sum(unknow),0) as unknow ,isnull(sum(noidea),0) as noidea ,
isnull(sum(soso),0) as soso,isnull(sum(understand),0) as understand
from
(select showtime,case when reason='不明白' then Amount end unknow,
case when reason='不清楚' then Amount end noidea,
case when reason='还好' then Amount end soso,
case when reason='了解' then Amount end understand
from test1 group by showtime,reason,Amount) as temp
group by showtime
--表test1 你可以替换为 你的查询语句
查询结果
showtime unknow noidea soso understand
----------------------- ----------- ----------- ----------- -----------
2012-01-12 00:00:00.000 0 0 5 4
2012-02-10 00:00:00.000 3 0 0 0
2012-11-18 00:00:00.000 2 3 0 3
2012-12-21 00:00:00.000 0 1 0 0
你应该把的表结构 给说出来 而不是你查出来的数据 我还纠结哪有这样建表的啊
嗯!对,是处理后的数据。
@求解=: 效率问题先不考虑 你要把你的标的结构给说清楚 我只是按照图片上表关系来的
@Fishboy: amount列是我统计出来的数据,表的本身没有该列。
select showtime,isnull(sum(unknow),0) as unknow ,isnull(sum(noidea),0) as noidea ,
isnull(sum(soso),0) as soso,isnull(sum(understand),0) as understand
from
(
select showtime,case when reason='不明白' then count(reason) end unknow,
case when reason='不清楚' then count(reason) end noidea,
case when reason='还好' then count(reason) end soso,
case when reason='了解' then count(reason) end understand
from test1 group by showtime,reason
) as temp
group by showtime
--这样行不行
@求解=:
@Fishboy: 不行
@求解=: 你数据表结构都不给 只给我个结果集 我也束手无策
@Fishboy: 谢谢啦!我用另外一种方式解决啦。