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

在线求一SQL语句,关于分组的有关问题

2012-01-10 
在线求一SQL语句,关于分组的问题假如有一张表,字段为,编号,序号,数量内容为:001,1,1001,2,2001,3,3001,4,4

在线求一SQL语句,关于分组的问题
假如有一张表,字段为,编号,序号,数量  
内容为: 001,1,1  
  001,2,2  
  001,3,3  
  001,4,4  
  001,5,5 
  002,1,1  
  002,2,2  
  002,3,3  
  002,4,4  
  002,5,5 
怎么样将这个表中的内容分为两部分,一部分是编号不同的前两项,一部分是剩余的项,即一个查询出来的表是
001,1,1  
001,2,2 
002,1,1  
002,2,2 
剩余的是一个查询中的内容

[解决办法]

SQL code
create table tb(id char(3),a int,b int)insert tb select  '001',1,1insert tb select  '001',2,2   insert tb select  '001',3,3   insert tb select  '001',4,4   insert tb select  '001',5,5 insert tb select  '002',1,1   insert tb select  '002',2,2   insert tb select  '002',4,4   insert tb select  '002',5,5 select a.id,a.a,a.bfrom tb a,tb bwhere a.id=b.idgroup by a.id,a.a,a.bhaving count(case when a.a>=b.a and a.b>=b.b then 1 else null end)<=2drop table tb/*id   a           b           ---- ----------- ----------- 001  1           1001  2           2002  1           1002  2           2(所影响的行数为 4 行)*/
[解决办法]
SQL code
create table tb(编号 varchar(10),序号 int,数量 int)insert into tb values('001',1,1)       insert into tb values('001',2,2)       insert into tb values('001',3,3)       insert into tb values('001',4,4)       insert into tb values('001',5,5)   insert into tb values('002',1,1)       insert into tb values('002',2,2)       insert into tb values('002',3,3)       insert into tb values('002',4,4)       insert into tb values('002',5,5)goselect id=identity(int,1,1),* into # from tbselect  编号 ,序号 , 数量   from # awhere (select count(1) from # where a.编号=编号 and id<a.id)<2/*编号         序号          数量          ---------- ----------- ----------- 001        1           1001        2           2002        1           1002        2           2(4 row(s) affected)*/
[解决办法]
SQL code
--1create table tb(编号 varchar(10),序号 int,数量 int)insert into tb values('001bl 

热点排行