很急,求大神!
目前有CTYPE,CCOMMEN,CFIELD,CFIELDID,COFFSE这几烈数据
检查数据是否合法,即以CTYPE分组,在每组CTYPE中如果CCOMMEN相同则CFIELD,CFIELDID,COFFSET的值必须也相同。也就是要查找出不合法的行。最好用firebird数据库存储过程语法描述,用其他语法也可以。
存储过程? SQL 数据库
------解决方案--------------------
我假设你CTYPE CCOMMEN这两个字段是主键,如果主键是ctype那么on后面只需要一个关联条件即可
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-05-23 16:50:15
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([CTYPE] int,[CCOMMEN] int,[CFIELD] int,[CFIELDID] int,[COFFSE] int)
insert [huang]
select 1,1,1,1,1
UNION ALL
select 1,1,1,1,1
UNION ALL
select 2,1,1,1,1
UNION ALL
select 1,2,1,1,1
--------------开始查询--------------------------
SELECT DISTINCT * FROM [huang] a
WHERE EXISTS (SELECT 1 FROM (
select [CTYPE],[CCOMMEN],ROW_NUMBER()OVER(PARTITION BY CTYPE,CCOMMEN,CFIELD,CFIELDID,COFFSE ORDER BY CTYPE,CCOMMEN)[count]
from [huang])b WHERE a.[CTYPE]=b.[CTYPE] AND a.[CCOMMEN]=b.[CCOMMEN] AND b.[count]>1)
----------------结果----------------------------
/*
CTYPE CCOMMEN CFIELD CFIELDID COFFSE
----------- ----------- ----------- ----------- -----------
1 1 1 1 1
*/
------解决方案--------------------
select * from tb t
where exists(select * from tb
where CTYPE=t.CTYPE and CCOMMEN=t.CCOMMEN
and (CFIELD<>t.CFIELD or CFIELDID<>t.CFIELDID or COFFSE<>t.COFFSE))