有个表 table
id name year num
1 a 1992 3
2 b 1982 4
3 c 1985 5
4 a 1993 3
5 c 1995 5
6 a 1988 5
统计查询出 name 1980年至1990年 num 种和 1990至2000 num 种合 2000到2010 num中和 显示出的 a 5 6 0
b 4 0 0
c 5 5 0怎么实现
id name year num
1 a 1992 3
2 b 1982 4
3 c 1985 5
4 a 1993 3
5 c 1995 5
6 a 1988 5
统计查询出 name 1980年至1990年 num 种和 1990至2000 num 种合 2000到2010 num中和 显示出的 a 5 6 0
b 4 0 0
c 5 5 0怎么实现
解决方案 »
- 请教一个查询的问题
- 求:用一条SQL语句删除掉一张表里的name含有重复值的记录,只留一个
- 求一个 按月分类 统计的sql语句 谢谢
- 無法初始化管理理單元
- 请问新增、修改、删除记录触发新增、修改、删除表的触发器如何写?
- INSERT INTO 语法错误,实在看不出来。大家帮忙看看,嘿
- 请问如何将一个表从竖着转换成横的?
- SQL 2005 出现突然停止响应的情况
- 如何用变量表示字段名?
- 在vfp表里一字段内容为:]>R)m'+ 不知道怎么输入的,方框代表什么?因为没有源程序,所以看不到代码(该字符是用程序加入的)是控制字符?
- 烦劳帮助,在线待回答,谢谢[microsoft][0dbc sql sever driver][shared memory]sql server 不存在或访问被
- SQL Server2008如何设置主键
sum(case when year between 1980 and 1990 then num else 0 end) [1980-1990],
sum(case when year between 1991 and 2000 then num else 0 end) [1991-2000],
sum(case when year between 2001 and 2010 then num else 0 end) [2001-2010]
from tb
group by name
insert into tb select 1,'a',1992,3
insert into tb select 2,'b',1982,4
insert into tb select 3,'c',1985,5
insert into tb select 4,'a',1993,3
insert into tb select 5,'c',1995,5
insert into tb select 6,'a',1988,5
go
select name,sum(case when [year] between 1980 and 1989 then num else 0 end)[1980-1989],
sum(case when [year] between 1990 and 1999 then num else 0 end)[1990-1999],
sum(case when [year] between 2000 and 2009 then num else 0 end)[2000-2009]
from tb group by name
/*
name 1980-1989 1990-1999 2000-2009
---------- ----------- ----------- -----------
a 5 6 0
b 4 0 0
c 5 5 0(3 行受影响)*/
go
drop table tb
create table tb(id int,name varchar(10),year int,num int)
insert into tb values(1 ,'a', 1992 ,3)
insert into tb values(2 ,'b', 1982 ,4)
insert into tb values(3 ,'c', 1985 ,5)
insert into tb values(4 ,'a', 1993 ,3 )
insert into tb values(5 ,'c', 1995 ,5)
insert into tb values(6 ,'a', 1988 ,5)
goselect name ,
sum(case when year between 1980 and 1990 then num else 0 end) [1980-1990],
sum(case when year between 1991 and 2000 then num else 0 end) [1991-2000],
sum(case when year between 2001 and 2010 then num else 0 end) [2001-2010]
from tb
group by name
drop table tb/*
name 1980-1990 1991-2000 2001-2010
---------- ----------- ----------- -----------
a 5 6 0
b 4 0 0
c 5 5 0(所影响的行数为 3 行)
*/
这个year 是按时间的 你这个是按int型的 我回去一是 换成datetime 查询统计 是01号的时候不好使 我用的是
sql2000