关于SQL语句查询多个值的问题 急。。。。在线等

tangcheng0 2011-11-08 04:29:40
客服名 评分
李四 3
张三 3
李四 2
老五 1
张三 3
老五 3
张三 1

现在我需要查询 每个客服的3分有几个,2分有几个,1分有几个,按每个客服的评分总和排名从高到低循环出来。


这个数据希望出的的结果是:

3分 2分 1分 总分
张三 2 0 0 6
李四 1 1 0 5
老五 1 0 1 4

不知道这样的查询语句应该怎么写,我现在只能写出每个客服的评分总和,可是没办法再查询每个客服的3分2分1分各有几个,,拜托各位大侠帮帮忙,小弟感激不敬啊!!!!


<%Sql= "select top 10 客服名,sum(评分) as 评分 from dingdan group by 客服名 order by sum(评分) desc "

我现在只能写出按评分总和排名的语句出来。没办法查询3分2分1分各几个的语句出来。。。
...全文
330 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 tangcheng0 的回复:]
引用 13 楼 roy_88 的回复:
SQL code

use Tempdb
go
--> -->

if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([客服名] nvarchar(2),[评分] int)
Insert #T
select N'李四',3 union all……
[/Quote]

ASP的写成存储过程 在你的程序中去调用
邹夫子 2011-11-08
  • 打赏
  • 举报
回复

create table tb(客服名 varchar(10),评分 int)
insert into tb select '李四',3
insert into tb select '张三',3
insert into tb select '李四',2
insert into tb select '老五',1
insert into tb select '张三',3
insert into tb select '老五',3
insert into tb select '张三',1
go

select a.*,b.总分 from
(select * from tb pivot(count(评分) for 评分 in ([3],[2],[1])) as p)a
join
(select 客服名,sum(评分) as 总分 from tb group by 客服名) b
on a.客服名=b.客服名
go
/*
客服名 3 2 1 总分
---------- ----------- ----------- ----------- -----------
老五 1 0 1 4
李四 1 1 0 5
张三 2 0 1 7

(3 行受影响)
*/
go
drop table tb



  • 打赏
  • 举报
回复
有些回答是存储过程形式的,需要你在数据库里建立存储过程,把代码复制过去(其中建表添数据的部分就不用了)。

有的是可以整理成一个SELECT语句的,可以直接试用。少数情况下由于不同数据库支持的函数不同,需要转换,自己查一下对应的函数即可。
中国风 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 tangcheng0 的回复:]

引用 13 楼 roy_88 的回复:
SQL code

use Tempdb
go
--> -->

if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([客服名] nvarchar(2),[评分] int)
Insert #T
select N'李四',3 union al……
[/Quote]
可把語句寫成存儲過程,程序調用就行了

程序直接調用語句,需要寫成固定格式
這樣
select 客服名,
[1分]=sum(case when 评分=N'1' then 1 else '' end),
[2分]=sum(case when 评分=N'2' then 1 else '' end),
[3分]=sum(case when 评分=N'3' then 1 else '' end),
sum(评分) as 总分
from 表名
group by 客服名
order by 总分 desc
tangcheng0 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 roy_88 的回复:]
SQL code

use Tempdb
go
--> -->

if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([客服名] nvarchar(2),[评分] int)
Insert #T
select N'李四',3 union all
select N'张三',……
[/Quote]

请问大哥,我的是ASP页面,这个能用吗?
中国风 2011-11-08
  • 打赏
  • 举报
回复
use Tempdb
go
--> -->

if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([客服名] nvarchar(2),[评分] int)
Insert #T
select N'李四',3 union all
select N'张三',3 union all
select N'李四',2 union all
select N'老五',1 union all
select N'张三',3 union all
select N'老五',3 union all
select N'张三',1
Go

declare @s nvarchar(4000)
set @s=''
Select @s=@s+N','+quotename(RTRIM(评分)+N'分')+N'=sum(case when 评分=N'+quotename(评分,'''')+N' then 1 else '''' end)'

from #T group by 评分
--顯示生成語句
print N'select 客服名'+@s+N',sum(评分) as 总分 from #T group by 客服名 order by 总分 desc'

exec(N'select 客服名'+@s+N',sum(评分) as 总分 from #T group by 客服名 order by 总分 desc')

go

/*
客服名 1分 2分 3分 总分
张三 1 0 2 7
李四 0 1 1 5
老五 1 0 1 4
*/
tangcheng0 2011-11-08
  • 打赏
  • 举报
回复
<%Sql= "select top 10 ddkf,sum(pjpf) as pjpf from dingdan  group by ddkf  order by sum(pjpf) desc "

Rs2.Open Sql,Conn,1,1
ddkff=rs2("ddkf")


s=0
Do While Not Rs2.Eof


s=s+1 %>
<tr>
<td bgcolor="#FFFFFF"><div align="center">第<%=s%>名</div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("ddkf")%></div></td>
<td bgcolor="#FFFFFF"><div align="center">这里放3分的数量</div></td>
<td bgcolor="#FFFFFF"><div align="center">这里放2分的数量/div></td>
<td bgcolor="#FFFFFF"><div align="center">这里放1分的数量</div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("pjpf")%></div></td>
</tr>
<%
ss=ss+1
if ss>=10 then exit do
Rs2.movenext
Loop
rs2.close


%>


我目前是这样写的。但是没办法得到3分2分1分的数量,,。。。。。。。。。。
tangcheng0 2011-11-08
  • 打赏
  • 举报
回复
                   <%Sql= "select top 10 ddkf,3分=sum(case when pjpf=3 then 1 else 0 end),2分=sum(case when pjpf=2 then 1 else 0 end),1分=sum(case when pjpf=1 then 1 else 0 end) from ddkf group by ddkf order by sum(pjpf) desc "

Rs2.Open Sql,Conn,1,1



s=0
Do While Not Rs2.Eof


s=s+1 %>
<tr>
<td bgcolor="#FFFFFF"><div align="center">第<%=s%>名</div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("ddkf")%></div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("3分")%></div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("2分")%></div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("1分")%></div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("pjpf")%></div></td>
</tr>
<%
ss=ss+1
if ss>=10 then exit do
Rs2.movenext
Loop
rs2.close


%>



像这样的情况语句具体应该怎么写呢??
tangcheng0 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ssp2009 的回复:]
引用 7 楼 tangcheng0 的回复:
引用 2 楼 ssp2009 的回复:
SQL code

select top 10 客服名,评分3=sum(case when 评分=3 then 1 else 0 end),
评分2=sum(case when 评分=2 then 1 else 0 end),
评分1=sum(case when 评分=1 then 1 else 0 ……
[/Quote]



<%Sql= "select top 10 ddkf,3分=sum(case when pjpf=3 then 1 else 0 end),2分=sum(case when pjpf=2 then 1 else 0 end),1分=sum(case when pjpf=1 then 1 else 0 end) from ddkf group by ddkf order by sum(pjpf) desc "

Rs2.Open Sql,Conn,1,1
ddkff=rs2("ddkf")


s=0
Do While Not Rs2.Eof


s=s+1 %>
<tr>
<td bgcolor="#FFFFFF"><div align="center">第<%=s%>名</div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("ddkf")%></div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("3分")%></div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("2分")%></div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("1分")%></div></td>
<td bgcolor="#FFFFFF"><div align="center"><%=rs2("pjpf")%></div></td>
</tr>
<%
ss=ss+1
if ss>=10 then exit do
Rs2.movenext
Loop
rs2.close


%>

我这样写能行吗?》获取不了值。
快溜 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tangcheng0 的回复:]
引用 2 楼 ssp2009 的回复:
SQL code

select top 10 客服名,评分3=sum(case when 评分=3 then 1 else 0 end),
评分2=sum(case when 评分=2 then 1 else 0 end),
评分1=sum(case when 评分=1 then 1 else 0 end)
from 客服名 group by ……
[/Quote]你可以把评分3改为3分
AcHerat 2011-11-08
  • 打赏
  • 举报
回复
别名,你可以随便取!
tangcheng0 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ssp2009 的回复:]
SQL code

select top 10 客服名,评分3=sum(case when 评分=3 then 1 else 0 end),
评分2=sum(case when 评分=2 then 1 else 0 end),
评分1=sum(case when 评分=1 then 1 else 0 end)
from 客服名 group by 客服名 order by sum(评分) ……
[/Quote]

谢谢大哥,请问这个 评分3 具体是什么意思? 因为我的数据表只有一个 评分字段 你写的这个我不知道具体怎么操作了。。
AcHerat 2011-11-08
  • 打赏
  • 举报
回复

create table tb(客服名 varchar(10),评分 int)
insert into tb select '李四',3
insert into tb select '张三',3
insert into tb select '李四',2
insert into tb select '老五',1
insert into tb select '张三',3
insert into tb select '老五',3
insert into tb select '张三',1
go

declare @sql varchar(max)
set @sql = 'select 客服名'
select @sql = @sql + ',sum(case when 评分 = '+ltrim(评分)+' then 1 else 0 end) ['+ltrim(评分)+'分]'
from tb
group by 评分
select @sql = @sql + ',sum(评分) as 总分 from tb group by 客服名'
exec(@sql)

go
drop table tb

/*************

客服名 1分 2分 3分 总分
---------- ----------- ----------- ----------- -----------
老五 1 0 1 4
李四 0 1 1 5
张三 1 0 2 7

(3 行受影响)
AcHerat 2011-11-08
  • 打赏
  • 举报
回复
create table tb(客服名 varchar(10),评分 int)
insert into tb select '李四',3
insert into tb select '张三',3
insert into tb select '李四',2
insert into tb select '老五',1
insert into tb select '张三',3
insert into tb select '老五',3
insert into tb select '张三',1
go

declare @sql varchar(max)
set @sql = 'select 客服名'
select @sql = @sql + ',sum(case when 评分 = '+ltrim(评分)+' then 1 else 0 end) ['+ltrim(评分)+'分]'
from tb
group by 评分
select @sql = @sql + ' from tb group by 客服名'
exec(@sql)

/*
客服名 3分 2分 1分 总分
---------- ----------- ----------- ----------- -----------
张三 2 0 1 7
李四 1 1 0 5
老五 1 0 1 4

(3 行受影响)
*/

go
drop table tb
Mr_Nice 2011-11-08
  • 打赏
  • 举报
回复
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (客服名 nvarchar(4),评分 int)
insert into [TB]
select '李四',3 union all
select '张三',3 union all
select '李四',2 union all
select '老五',1 union all
select '张三',3 union all
select '老五',3 union all
select '张三',1

select * from [TB]


select top 10 客服名,
评分3=sum(case when 评分=3 then 1 else 0 end),
评分2=sum(case when 评分=2 then 1 else 0 end),
评分1=sum(case when 评分=1 then 1 else 0 end)
from TB
group by 客服名
order by sum(评分) desc


/*
客服名 评分3 评分2 评分1
---- ----------- ----------- -----------
张三 2 0 1
李四 1 1 0
老五 1 0 1

(3 行受影响)

*/
-晴天 2011-11-08
  • 打赏
  • 举报
回复
create table tb(客服名 varchar(10),评分 int)
insert into tb select '李四',3
insert into tb select '张三',3
insert into tb select '李四',2
insert into tb select '老五',1
insert into tb select '张三',3
insert into tb select '老五',3
insert into tb select '张三',1
go
select 客服名,
sum(case when 评分=3 then 1 else 0 end)[3分],
sum(case when 评分=2 then 1 else 0 end)[2分],
sum(case when 评分=1 then 1 else 0 end)[1分],
SUM(评分)总分
from tb
group by 客服名
order by 5 desc
/*
客服名 3分 2分 1分 总分
---------- ----------- ----------- ----------- -----------
张三 2 0 1 7
李四 1 1 0 5
老五 1 0 1 4

(3 行受影响)
*/

go
drop table tb

快溜 2011-11-08
  • 打赏
  • 举报
回复
select top 10 客服名,评分3=sum(case when 评分=3 then 1 else 0 end),
评分2=sum(case when 评分=2 then 1 else 0 end),
评分1=sum(case when 评分=1 then 1 else 0 end)
from 客服名 group by 客服名 order by sum(评分) desc
快溜 2011-11-08
  • 打赏
  • 举报
回复
select top 10 客服名,评分3=sum(case when 评分=3 then 1 else 0 end),
评分2=sum(case when 评分=2 then 1 else 0 end),
评分1=sum(case when 评分=1 then 1 else 0 end)
from 客服名 group by 客服名 order by sum(评分)

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧