日期:2014-05-18  浏览次数:20394 次

再想了解多点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