建立表结构
create table a
(sname int, smonth int, score int) /* 分别是姓名,月份,分数*/
insert into a values ( 1,7,80)
insert into a values ( 1,8,40)
insert into a values ( 1,9,80)
insert into a values ( 2,7,50)
insert into a values ( 2,8,40)
insert into a values ( 2,9,60)
insert into a values ( 3,7,50)
insert into a values ( 3,8,80)
insert into a values ( 3,9,60)
insert into a values ( 4,7,50)
insert into a values ( 4,8,40)
insert into a values ( 4,9,60).............................对名字排序的对应表 (排序表)
sort sname
1 3
2 4
3 1
4 2 对每人的季度总分排名 然后再排序 ,排名已经写出来了,但排序我写不出来
,排名如下:
select sname,
[4月]=sum(case when smonth=4 then score else 0 end),
[5月]=sum(case when smonth=5 then score else 0 end),
[6月]=sum(case when smonth=6 then score else 0 end),
[总分]=sum(score),
[排名]=( select count(*)+1 from(
select col=1 from a where smonth in(4,5,6) group by sname having sum(score)>sum(tmpA.score)
) tmp
)
from a tmpA
where smonth in(4,5,6)
group by sname
再在里面加上排序怎么写 ~~~
谢谢
create table a
(sname int, smonth int, score int) /* 分别是姓名,月份,分数*/
insert into a values ( 1,7,80)
insert into a values ( 1,8,40)
insert into a values ( 1,9,80)
insert into a values ( 2,7,50)
insert into a values ( 2,8,40)
insert into a values ( 2,9,60)
insert into a values ( 3,7,50)
insert into a values ( 3,8,80)
insert into a values ( 3,9,60)
insert into a values ( 4,7,50)
insert into a values ( 4,8,40)
insert into a values ( 4,9,60).............................对名字排序的对应表 (排序表)
sort sname
1 3
2 4
3 1
4 2 对每人的季度总分排名 然后再排序 ,排名已经写出来了,但排序我写不出来
,排名如下:
select sname,
[4月]=sum(case when smonth=4 then score else 0 end),
[5月]=sum(case when smonth=5 then score else 0 end),
[6月]=sum(case when smonth=6 then score else 0 end),
[总分]=sum(score),
[排名]=( select count(*)+1 from(
select col=1 from a where smonth in(4,5,6) group by sname having sum(score)>sum(tmpA.score)
) tmp
)
from a tmpA
where smonth in(4,5,6)
group by sname
再在里面加上排序怎么写 ~~~
谢谢
解决方案 »
- 使用sql,如何选择出没有子栏目,并且该栏目没有书本的行来
- table(id,laseupdate)查询最后修改的日期lastupdate
- [求助帖]缩减数据库的备份日志文件的方法,最好能简洁明了
- 加入一个触发器后,数据表就不再修改任何列的数据??
- sqlserver的输出,print、return还有select他们之间有什么区别啊?
- 开机问题
- SQL新手求帮助
- ms sql server2000 环境下cursor不能用吗?请您帮忙!
- 我自建了字段ID,如何得出最大的ID是哪个数字
- 高分求助:如何通过安装程序实现Ms sql server数据库的建立
- 哪位大虾帮帮小弟设计排课系统的存储过程
- 急~~~
from a tmpA
where smonth in(4,5,6)
group by sname
为
from a tmpA inner join 排序表 b on tmpA.sname=b.sname
where smonth in(4,5,6)
group by sname
order by b.sort
结果不正确 ,不能通过,把 排名 那列 去了可以通过,所以 错误在 排名列中 ,大家修改下,看怎么排序
首先大家 不看第二个排序表
我只对第一个表进行排名
select sname,
[4月]=sum(case when smonth=7 then score else 0 end),
[5月]=sum(case when smonth=8 then score else 0 end),
[6月]=sum(case when smonth=9 then score else 0 end),
[总分]=sum(score),
[排名]=( select count(*)+1 from(
select col=1 from a where smonth in(7,8,9) group by sname having sum(score)>sum(tmpA.score)
) tmp
)
from a tmpA
where smonth in(7,8,9)
group by sname
代码是正确的
我想在这个结果上再对sname 进行排序 ,对sname排序的对于关系就是 排序表.sname=a.sname
SELECT A.*,B.总分,IDENTITY(int,1,1) AS [排名] INTO #T FROM (
SELECT sname
,SUM(CASE smonth WHEN 7 THEN score ELSE 0 END) AS '7月'
,SUM(CASE smonth WHEN 8 THEN score ELSE 0 END) AS '8月'
,SUM(CASE smonth WHEN 9 THEN score ELSE 0 END) AS '9月'
FROM a GROUP BY sname
) AS A
RIGHT OUTER JOIN
(SELECT TOP 100 PERCENT sname,SUM(score) AS 总分 FROM a GROUP BY sname ORDER BY SUM(score) DESC) AS B
ON B.sname=A.sname
SELECT * FROM #T
DROP TABLE #T
结果:
sname 7月 8月 9月 总分 排名
------------------------------------------
1 80 40 80 200 1
3 50 80 60 190 2
4 50 40 60 150 3
2 50 40 60 150 4
*/
http://community.csdn.net/Expert/topic/5347/5347505.xml?temp=.6514856
说的很清楚
肯定要有 order by 啊
是按照 排序表(不是要排名的那个表)的sort字段 排序, 你的好象没有用到 排名我已经做出来了
select sname,
[4月]=sum(case when smonth=7 then score else 0 end),
[5月]=sum(case when smonth=8 then score else 0 end),
[6月]=sum(case when smonth=9 then score else 0 end),
[总分]=sum(score),
[排名]=( select count(*)+1 from(
select col=1 from a where smonth in(7,8,9) group by sname having sum(score)>sum(tmpA.score)
) tmp
)
from a tmpA
where smonth in(7,8,9)
group by sname order by 6结果如下:不知道是否是你要的答案。
sname 4月 5月 6月 总分 排名
----------- ----------- ----------- ----------- ----------- -----------
1 80 40 80 200 1
3 50 80 60 190 2
4 50 40 60 150 3
2 50 40 60 150 3
不是按照 排名 1 2 3 4 啊
我建立一个表吧 ~~~
create table b ( id int, sname int )
insert into b values ( 1,4)
insert into b values ( 2,2)
insert into b values ( 3,1)
insert into b values ( 4,3) 要求上面 zhfpoet() 的结果都按照 ID 排序,肯定要有对b表的连接啊~~
我试了 ,一般的连接不行, 出错在排名上面 ,要修改 排名那个地方
表脚本如下:
create table a
(sname int, smonth int, score int) /* 分别是姓名,月份,分数*/
insert into a values ( 1,7,80)
insert into a values ( 1,8,40)
insert into a values ( 1,9,80)
insert into a values ( 2,7,50)
insert into a values ( 2,8,40)
insert into a values ( 2,9,60)
insert into a values ( 3,7,50)
insert into a values ( 3,8,80)
insert into a values ( 3,9,60)
insert into a values ( 4,7,50)
insert into a values ( 4,8,40)
insert into a values ( 4,9,60)
gocreate table b
(
sort int,
name int
)
go
insert into b
select 1,3
union all
select 2,4
union all
select 3,1
union all
select 4,2go
运行查询的脚本如下:
select sname,
[4月]=sum(case when smonth=7 then score else 0 end),
[5月]=sum(case when smonth=8 then score else 0 end),
[6月]=sum(case when smonth=9 then score else 0 end),
[总分]=sum(score),
[排名]=( select count(*)+1 from
(
select col=1
from a
where smonth in(7,8,9)
group by sname
having sum(score)>sum(tmpA.score)
) tmp
)
from a tmpA
inner join b
on b.name = tmpA.sname
where smonth in(7,8,9)
group by sname
order by max(sort)结果如下:sname 4月 5月 6月 总分 排名
----------- ----------- ----------- ----------- ----------- -----------
3 50 80 60 190 2
4 50 40 60 150 3
1 80 40 80 200 1
2 50 40 60 150 3不知道符合要求不.
--新建的排序表:
CREATE TABLE T(sort int,sname int )
INSERT INTO T
SELECT 1 , 3 UNION ALL
SELECT 2 , 4 UNION ALL
SELECT 3 , 1 UNION ALL
SELECT 4 , 2
*/SELECT A.*,B.总分,IDENTITY(int,1,1) AS [排名] INTO #T FROM (
SELECT sname
,SUM(CASE smonth WHEN 7 THEN score ELSE 0 END) AS '7月'
,SUM(CASE smonth WHEN 8 THEN score ELSE 0 END) AS '8月'
,SUM(CASE smonth WHEN 9 THEN score ELSE 0 END) AS '9月'
FROM a GROUP BY sname
) AS A
RIGHT OUTER JOIN
(SELECT TOP 100 PERCENT sname,SUM(score) AS 总分 FROM a GROUP BY sname ORDER BY SUM(score) DESC) AS B
ON B.sname=A.snameSELECT #T.* FROM #T
LEFT OUTER JOIN T ON T.sname=#T.sname
ORDER BY T.sort
DROP TABLE #T
sname 4月 5月 6月 总分 排名
----------- ----------- ----------- ----------- ----------- ----------- 3 50 80 60 190 2
4 50 40 60 150 3
1 80 40 80 200 1
2 50 40 60 150 4
内部SQL 错误
第一个你的SQL 有点问题 我想要你第一个那样的 啊
是有错误 我这真的不行 ~~~
我开始也是那样做的 不行 ~~~
你的可以 ?? 怪了