有两个表结构如下:
表a如下:
id 产品名称
1 aa
2 bb
表b如下:
id 总体满意度 质量满意度 aid
1 1 1 1
2 3 2 1
3 1 1 1
4 1 4 2其中总体满意度和质量满意度两个字段含义是:1表示优,2表示良,3表示中,4表示差
表a的id与表b的aid关联我想求sql语句实现统计表a中所有产品的满意度统计,以上表的结果统计应该如下面所示:产品名称 总体满意度 质量满意度
aa 优:2 良:0中:1 差:0 优:2 良:1中:0 差:0
bb 优:1 良:0中:0 差:0 优:0 良:0中:0 差:1
合计 优:3 良:0中:1 差:0 优:2 良:1中:0 差:1谢谢
表a如下:
id 产品名称
1 aa
2 bb
表b如下:
id 总体满意度 质量满意度 aid
1 1 1 1
2 3 2 1
3 1 1 1
4 1 4 2其中总体满意度和质量满意度两个字段含义是:1表示优,2表示良,3表示中,4表示差
表a的id与表b的aid关联我想求sql语句实现统计表a中所有产品的满意度统计,以上表的结果统计应该如下面所示:产品名称 总体满意度 质量满意度
aa 优:2 良:0中:1 差:0 优:2 良:1中:0 差:0
bb 优:1 良:0中:0 差:0 优:0 良:0中:0 差:1
合计 优:3 良:0中:1 差:0 优:2 良:1中:0 差:1谢谢
a.产品名称
,'优:' + cast(sum(case 总体满意度 when 1 then 1 else 0 end) as nvarchar(20)) +
' 良:' + cast(sum(case 总体满意度 when 1 then 2 else 0 end) as nvarchar(20)) +
' 中:' + cast(sum(case 总体满意度 when 1 then 3 else 0 end) as nvarchar(20)) +
' 差:' + cast(sum(case 总体满意度 when 1 then 4 else 0 end) as nvarchar(20)) as 总体满意度
,'优:' + cast(sum(case 质量满意度 when 1 then 1 else 0 end) as nvarchar(20)) +
' 良:' + cast(sum(case 质量满意度 when 1 then 2 else 0 end) as nvarchar(20)) +
' 中:' + cast(sum(case 质量满意度 when 1 then 3 else 0 end) as nvarchar(20)) +
' 差:' + cast(sum(case 质量满意度 when 1 then 4 else 0 end) as nvarchar(20)) as 质量满意度
from a,b
where a.id=b.aid
group by a.产品名称
a.产品名称
,'优:' + cast(sum(case 总体满意度 when 1 then 1 else 0 end) as nvarchar(20)) +
' 良:' + cast(sum(case 总体满意度 when 1 then 2 else 0 end) as nvarchar(20)) +
' 中:' + cast(sum(case 总体满意度 when 1 then 3 else 0 end) as nvarchar(20)) +
' 差:' + cast(sum(case 总体满意度 when 1 then 4 else 0 end) as nvarchar(20)) as 总体满意度
,'优:' + cast(sum(case 质量满意度 when 1 then 1 else 0 end) as nvarchar(20)) +
' 良:' + cast(sum(case 质量满意度 when 1 then 2 else 0 end) as nvarchar(20)) +
' 中:' + cast(sum(case 质量满意度 when 1 then 3 else 0 end) as nvarchar(20)) +
' 差:' + cast(sum(case 质量满意度 when 1 then 4 else 0 end) as nvarchar(20)) as 质量满意度
from a,b
where a.id=b.aid
group by a.产品名称
union all
select
产品名称 = '合计'
,'优:' + cast(sum(case 总体满意度 when 1 then 1 else 0 end) as nvarchar(20)) +
' 良:' + cast(sum(case 总体满意度 when 1 then 2 else 0 end) as nvarchar(20)) +
' 中:' + cast(sum(case 总体满意度 when 1 then 3 else 0 end) as nvarchar(20)) +
' 差:' + cast(sum(case 总体满意度 when 1 then 4 else 0 end) as nvarchar(20)) as 总体满意度
,'优:' + cast(sum(case 质量满意度 when 1 then 1 else 0 end) as nvarchar(20)) +
' 良:' + cast(sum(case 质量满意度 when 1 then 2 else 0 end) as nvarchar(20)) +
' 中:' + cast(sum(case 质量满意度 when 1 then 3 else 0 end) as nvarchar(20)) +
' 差:' + cast(sum(case 质量满意度 when 1 then 4 else 0 end) as nvarchar(20)) as 质量满意度
from b
if OBJECT_ID('ta') is not null
drop table ta
go
create table ta(id int,产品名称 varchar(4))
insert into ta
select 1, 'aa' union all
select 2, 'bb'
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb(id int,总体满意度 int,质量满意度 int,aid int)
insert into tb
select 1, 1, 1, 1 union all
select 2, 3, 2, 1 union all
select 3, 1, 1, 1 union all
select 4, 1, 4, 2
;with t as
(
select a.产品名称,总体满意度= case 总体满意度 when 1 then '优'
when 2 then '良'
when 3 then '中'
when 4 then '差'
end ,
质量满意度= case 质量满意度 when 1 then '优'
when 2 then '良'
when 3 then '中'
when 4 then '差'
end
from ta a join tb b on a.id=b.aid
)
select ISNULL( 产品名称,'合计') as 产品名称,
总体满意度= ' 优:'+ LTRIM(sum(case 总体满意度 when '优' then 1 else 0 end )) +
' 良:'+ LTRIM(sum(case 总体满意度 when '良' then 1 else 0 end )) +
' 中:'+ LTRIM(sum(case 总体满意度 when '中' then 1 else 0 end )) +
' 差:'+ LTRIM(sum(case 总体满意度 when '差' then 1 else 0 end )),
质量满意度= ' 优:'+ LTRIM(sum(case 质量满意度 when '优' then 1 else 0 end )) +
' 良:'+ LTRIM(sum(case 质量满意度 when '良' then 1 else 0 end )) +
' 中:'+ LTRIM(sum(case 质量满意度 when '中' then 1 else 0 end )) +
' 差:'+ LTRIM(sum(case 质量满意度 when '差' then 1 else 0 end ))
from t
group by 产品名称 with rollup
产品名称 总体满意度 质量满意度
aa 优:2 良:0 中:1 差:0 优:2 良:1 中:0 差:0
bb 优:1 良:0 中:0 差:0 优:0 良:0 中:0 差:1
合计 优:3 良:0 中:1 差:0 优:2 良:1 中:0 差:1