首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

再想了解多点SQL ~该怎么处理

2012-01-19 
再想了解多点SQL ~~~~有一表ttt,字段如下:-----------------------idnum1K3S4J5U6K7K8P10S11S13S我怎样才

再想了解多点SQL ~~~~
有一表ttt,字段如下:
-----------------------
id           num
1               K
3               S
4               J
5               U
6               K
7               K
8               P
10             S
11             S
13             S

我怎样才能判断出最后一个字母S还在不在这个ttt表从id=1到id=8里面出现!

如果不在就显示第一种情况:   n=7为id=1到id=8的个数,na=S为num=S
n       na
7       S

否则显示第二种情况:  

第二种情况显示结果:
n           na          
0           S

-------------------
declare   @t   table(cxid   int,cxdh5   char(10))
insert   into   @t   select   1, 'K '
union   all   select   3, 'S '
union   all   select   4, 'J '
union   all   select   5, 'U '
union   all   select   6, 'K '
union   all   select   7, 'K '
union   all   select   8, 'P '
union   all   select   10, 'S '
union   all   select   11, 'S '
union   all   select   13, 'S '

[解决办法]
select count(*),y.cxdh5
from cxtbl x,(select top 1 cxdh5 order by cxid desc) y
where x.cxdh5 not in (select top 1 cxdh5 order by cxid desc)
group by y.cxdh5

热点排行
Bad Request.