动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   数据库 → 阅读文章
  

 如何用SQL语句实现分类

作者来源: 
阅读 数 295 人次 , 2006-4-14 10:04:00 


   
如何用SQL语句实现如下目的?
数据库:MS SQL 2000:
drop table mytest
go

create table mytest
(
sn int,
code varchar(8),
ins_no varchar(6),
)
go

insert mytest values(1, '01', 'A')
insert mytest values(1, '01', 'B')
insert mytest values(2, '02', 'A')
insert mytest values(2, '02', 'D')
insert mytest values(3, '01', 'A')
insert mytest values(3, '01', 'B')
insert mytest values(4, '03', 'A')
insert mytest values(4, '03', 'B')
insert mytest values(4, '03', 'C')
insert mytest values(5, '03', 'B')
insert mytest values(5, '03', 'C')
insert mytest values(6, '02', 'A')
insert mytest values(6, '02', 'C')
insert mytest values(6, '02', 'D')
go

select * from mytest
go

sn code ins_no
--- -------- ------
1 01 A
1 01 B
2 02 A
2 02 D
4 03 A
4 03 B
4 03 C
5 03 B
5 03 C
6 02 A
6 02 C
6 02 D

能否用一个查询语句得到?先谢了

我发现如果用很多NOT EXITS去写,将来代码的可读性不高,毕竟,要达到象LODGE这样对SQL熟悉的程度,并非易事,因此,我还是用SP实现了。
代码如下:
drop table mytest
go

create table mytest
(
sn int,
code varchar(8),
ins_no varchar(6),
)
go

insert mytest values(1, '01', 'A')
insert mytest values(1, '01', 'B')
insert mytest values(2, '02', 'A')
insert mytest values(2, '02', 'D')
insert mytest values(3, '01', 'A')
insert mytest values(3, '01', 'D')
insert mytest values(4, '03', 'A')
insert mytest values(4, '03', 'B')
insert mytest values(4, '03', 'C')
insert mytest values(5, '03', 'B')
insert mytest values(5, '03', 'C')
insert mytest values(6, '02', 'A')
insert mytest values(6, '02', 'C')
insert mytest values(6, '02', 'D')
insert mytest values(7, '01', 'A')
insert mytest values(7, '01', 'B')
insert mytest values(8, '03', 'C')
insert mytest values(8, '03', 'B')
go

drop proc Filter
go

create proc Filter
as
declare @iCurrentGroup int, @iMaxGroupNum int, @iRowCount int, @iTemp int
create table #temp
(
code varchar(8),
ins_no varchar(6),
)

select @iCurrentGroup = 1
select @iMaxGroupNum = max(sn) from mytest
while (@iCurrentGroup <= @iMaxGroupNum)
begin
-- 先将要判断的分组放到临时表
insert #temp (code, ins_no)
select code, ins_no
from mytest
where sn = @iCurrentGroup
select @iRowCount = @@ROWCOUNT
while (@iCurrentGroup <= @iMaxGroupNum)
begin
if (@iRowCount != 0)
begin
-- 比较
select @iTemp = @iCurrentGroup + 1
while (@iTemp <= @iMaxGroupNum)
begin
-- 必须满足的条件:两个表的纪录数相等,纪录完全相同
if ((select count(*) from mytest where sn = @iTemp)
= @iRowCount)
begin
if ((select count(*) from #temp a, (select code, ins_no from mytest where sn = @iTemp) b
where a.code = b.code and a.ins_no = b.ins_no ) = @iRowCount
-- 另一分组的纪录数等于前一分组的
and (select count(*) from mytest where sn = @iTemp)
= @iRowCount)
begin
--
delete mytest where sn = @iTemp
end
end
select @iTemp = @iTemp + 1
end
end

truncate table #temp
select @iCurrentGroup = @iCurrentGroup + 1
insert #temp (code, ins_no)
select code, ins_no
from mytest
where sn = @iCurrentGroup
select @iRowCount = @@ROWCOUNT
end
drop table #temp
end
go

exec Filter

select * from mytest

 本文Tags数据库  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:MS SQL Server2k数据转换服务部署
· 下一篇:MSSQL生成整个数据库的SQL脚本的工具
· 当SQL Server数据库崩溃时如何恢复?
· 在服务器上安装、使用MySQL的注意事项(1)
· MySQL优化简明指南
· Mysql备份脚本
· 基于MySQL的数据库集群系统的实现(1)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077