日期:2014-05-18  浏览次数:20545 次

关于这个结果怎么得到?请大家帮忙,前来探讨。
有这么一张表:
手机产品销售客户反馈记录的:
 产品类型 产品跟单状态id 客户反馈满意度id 客户回访方式id  
  苹果3代 1 5 2
  三星5830 3 5 1
  三星9100 3 3 3
  苹果4代 2 2 3
  诺基亚L800 1 2 2
  苹果4代 3 4 1
  三星5830 3 3 1
  苹果3代 5 1 2
  type status result huifu
根据这张表 我想得到

每一个产品类型 产品跟单状态为3 的行数,客户满意度的行数(4,5都表示满意),客户的回复次数,满意度的百分比值。

比如:三星5830 跟单状态为3的行数有2行,客户满意度有 1行,客户回复次数由2行,满意度百分比值为(1/8)(对应手机型号满意行数除以整个客户反馈满意度)。

产品类型 在结果表中只能是唯一的。也就是说三星5830这个手机型号的数据 在结果中只能出现一条数据。
 怎么去得到每一个手机型号下的各项值的行数统计,包括百分比计算,要用到循环吗?望大家指教,我会努力给分的。
 

------解决方案--------------------
SQL code
select   产品类型, sum(产品跟单状态id), sum(case when 客户反馈满意度id ='4' or 客户反馈满意度id ='5' then 客户反馈满意度id  else 0 end ), sum(客户回访方式id)
from  表
group by 产品类型

------解决方案--------------------
SQL code

declare @t table (t nvarchar(16),id1 tinyint,id2 tinyint,id3 tinyint)
insert into @t
select '苹果3代', 1, 5, 2 union all
select '三星5830',3 ,5 ,1 union all
select '三星9100', 3, 3, 3 union all
select '苹果4代', 2 ,2 ,3 union all
select '诺基亚L800', 1 ,2, 2 union all
select '苹果4代', 3 ,4, 1 union all
select '三星5830', 3 ,3 ,1 union all
select '苹果3代', 5 ,1, 2

select t as [产品类型],
    sum(case when id1 = 3 then 1 else 0 end)  as [跟单状态],
    sum(case when id2 >= 4 then 1 else 0 end) as [客户满意度] ,
    count(1) as [客户的回复次数],
    sum(case when id2 >= 4 then 1 else 0 end)*100.0/(select count(1) from @t) as [满意度百分比值]
from @t
group by t

/*
(8 行受影响)
产品类型             跟单状态        客户满意度       客户的回复次数     满意度百分比值
---------------- ----------- ----------- ----------- ---------------------------------------
诺基亚L800          0           0           1           0.000000000000
苹果3代             0           1           2           12.500000000000
苹果4代             1           1           2           12.500000000000
三星5830           2           1           2           12.500000000000
三星9100           1           0           1           0.000000000000

(5 行受影响)
*/

------解决方案--------------------
探讨

感谢哦 现在我的表中有数据,不用插入测试语句,那上面的那些地方可以去掉不要写啊? 还有个问题 我那几个id不是int类型的,是varchar类型的,这样在后面的查询 case语句中 会出现 转换报错么?百分比 可以留短点么?2位就可以了

------解决方案--------------------
哪家有哥的,把楼上的带走吧

------解决方案--------------------
插入测试语句 可以给其他人
------解决方案--------------------
探讨

SQL code

declare @t table (t nvarchar(16),id1 tinyint,id2 tinyint,id3 tinyint)
insert into @t
select '苹果3代', 1, 5, 2 union all
select '三星5830',3 ,5 ,1 union all
select '三星9100', 3, 3, 3 union all
s……