--DROP TABLE test
CREATE TABLE test(a INT,b INT, c VARCHAR(10))
INSERT INTO dbo.test
( a, b, c )
SELECT 5038,1,'ss030001'
UNION ALL
SELECT
5038,1,'ss030001'
UNION ALL
SELECT
5038,2,''
UNION ALL
SELECT
5038,3,''
UNION ALL
SELECT
5038,2,'444'
UNION ALL
SELECT
5121,1,''
UNION ALL
SELECT
5038,3,'123456'
SELECT * FROM test WHERE isnull(c,'')<>''
UNION all
SELECT a,b,c
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY a ORDER BY a) id ,*
FROM test) t1
WHERE id=1 AND a NOT IN (SELECT a FROM test WHERE isnull(c,'')<>'')
结果:
5038 1 ss030001
5038 1 ss030001
5038 2 444
5038 3 123456
5121 1
可以这样编写SQL语句(对不起之前用了*号,现予以改正):
select A,B,C from 表名 where C is not null
union all
select A,B,C from 表名 where C is null
Group by A,B,C Having Count(*)=1
Order By A,B