日期:2014-05-17 浏览次数:20437 次
;WITH MyTable AS
(
select '2013.08' AS a ,'LDF' AS b,NULL AS c union all
select '2013.07','B','AA' union all
select '2013.07','B','BB' union all
select '2013.09','B',NULL union all --b=‘B’,保留C非NULL的条目
select '2013.01','C','AA' union all
select '2013.08','D',NULL union all --b=‘D’,只保留一条Null
select '2013.08','D',NULL union all
select '2013.07','E','AA' union all
select '2013.07','E','BB' --b=‘E’,保留所有条目
), C1 AS
(
SELECT *,first_value(rn) OVER(PARTITION BY b ORDER BY rn DESC) mrn
FROM
(
SELECT b,c,rn=ROW_NUMBER() OVER(PARTITION BY b ORDER BY c)
FROM MyTable
) T
)
SELECT b,c
FROM C1
WHERE (c IS NULL AND rn=mrn) OR c IS NOT NULL
;WITH MyTable AS
(
select '2013.08' AS a ,'LDF' AS b,NULL AS c union all
select '2013.07','B','AA' union all
select '2013.07','B','BB' union all
select '2013.09','B',NULL union all
select '2013.01','C','AA' union all
select '2013.08','D',NULL union all
select '2013.08','D',NULL union all
select '2013.07','E','AA' union all
select '2013.07','E','BB'
)
,tb as
(
select ROW_NUMBER() over(order by b) as RN,* from mytable
)
select a.b,a.c from tb a where not exists
(
select 1 from