当前位置:主页>销售管理软件> 列表

求一简单的SQL语句(SQL-SERVER)30分 找电脑蓝牙管理软件

仓库管理软件版1楼: 数据
KH_ID zg_id year cj
654 136 1997 称职
655 136 1998 优秀 称职
656 136 1999 优秀 优秀
657 136 2000 称职 优秀
658 136 2001 优秀 称职
659 136 2002 优秀 优秀
660 136 2003 优秀 优秀
8240 136 2004 优秀 86 优秀

3353 1605 1997 称职
3354 1605 1998 称职 称职
3355 1605 1999 称职 称职
3356 1605 2000 称职 称职
3357 1605 2001 称职 称职
9203 1605 2002 称职 24 称职
3359 1605 2003 优秀 24 称职
7453 1605 2004 优秀 24 优秀
9554 1605 2005 优秀 24 称职

要求得到 2002---2005年 成绩是连续优秀的
659 136 2002 优秀 优秀
660 136 2003 优秀 优秀
8240 136 2004 优秀 86 优秀

3359 1605 2003 优秀 24 称职
7453 1605 2004 优秀 24 优秀
9554 1605 2005 优秀 24 称职

2楼: 看不懂
1\四个字段,怎么5列值
2\连续是什么概念,2-5是四年,2年,3年叫连续吗?
3\优秀前边的数字又是干吗的 如用友客户管理软件

3楼: select *,count(*) from table


where ((year>=''2002'') or (year<=''2005''))
group by zg_id having count(*)=3

4楼: 不好意思太急了没有写清楚 ,最后一列前面的数字是没有用的(24和86两个数字都没有用),应该是 2002---2005年连续3年成绩优秀的
就是2002 2003 2004 这样 和2003 2004 2005 这些年优秀的

5楼: 先查年份在去计算

6楼: 试一试
select kh_id,zg_id,year,cj
from table where (year between ''2002'' and ''2004'') and cj=''优秀''
group by kh_id,zg_id,year,cj
having count(*)=3
union
select kh_id,zg_id,year,cj
from table where (year between ''2003'' and ''2005'') and cj=''优秀''
group by kh_id,zg_id,year,cj
having count(*)=3

仓库管理软件版7楼: 不对呀!查询不出来

8楼: select kh_id, zg_id, year, cj
from t a
where year between 2002 and 2005
and cj = ''优秀''
and ((exists (select 1 from t where year=a.year+1 and cj = ''优秀'')
and exists (select 1 from t where year=a.year+2 and cj = ''优秀''))
or (exists (select 1 from t where year=a.year-1 and cj = ''优秀'')


and exists (select 1 from t where year=a.year-2 and cj = ''优秀'')))
order by zg_id, year

9楼: 漏掉一个条件...应为

select kh_id, zg_id, year, cj
from t a
where year between 2002 and 2005
and cj = ''优秀''
and ((exists (select 1 from t where year=a.year+1 and cj = ''优秀'')
and exists (select 1 from t where year=a.year+2 and cj = ''优秀''))
or (exists (select 1 from t where year=a.year-1 and cj = ''优秀'')
and exists (select 1 from t where year=a.year-2 and cj = ''优秀''))
or (exists (select 1 from t where year=a.year-1 and cj = ''优秀'')
and exists (select 1 from t where year=a.year+1 and cj = ''优秀'')))
order by zg_id, year

10楼: @_@ 回答正确,而且比较通用。
我就不现丑了。哈

11楼: @_@ 按照你写的的出的结果还有不对的地方呀!比如单个年CJ优秀的也选出来了

12楼: 下面结果会得到2002---2005年连续3年成绩优秀的zg_id
其他的你自己衍生吧

select zg_id
from table1 where (year between ''2002'' and ''2004'') and cj=''优秀''
group by zg_id
having count(*)=3
union
select zg_id
from table1 where (year between ''2003'' and ''2005'') and cj=''优秀''
group by zg_id
having count(*)=3 如电脑蓝牙管理软件

13楼: 你要的是,连续三年的优秀必须在2002-2005年之间?
如果是,在那几个exists字句后加上个过滤条件year between 2002 and 2005就行了

select kh_id, zg_id, year, cj
from t a
where year between 2002 and 2005
and cj = ''优秀''
and ((exists (select 1 from t where year=a.year+1 and cj = ''优秀'' and year between 2002 and 2005)
and exists (select 1 from t where year=a.year+2 and cj = ''优秀'' and year between 2002 and 2005))
or (exists (select 1 from t where year=a.year-1 and cj = ''优秀'' and year between 2002 and 2005)
and exists (select 1 from t where year=a.year-2 and cj = ''优秀'' and year between 2002 and 2005))
or (exists (select 1 from t where year=a.year-1 and cj = ''优秀'' and year between 2002 and 2005)
and exists (select 1 from t where year=a.year+1 and cj = ''优秀'' and year between 2002 and 2005)))
order by zg_id, year

仓库管理软件版14楼: 多人接受答案了。