日期:2014-05-19  浏览次数:20871 次

一句sql查询
id date                         note

1 2001-1-1 test
2 2001-1-1 sjd
3 2002-3-3 fdfd
4 2002-3-3 fdfd
5 2003-3-3 fdfd
6 2004-3-3 asa
7 2004-3-3 fds

===>
date     note

2001-1-1   null
2002-3-3 fdfd
2003-3-3 fdfd
2004-3-3 null

------解决方案--------------------

Select
Distinct
A.[date],
(Case When Exists(Select id From 表 Where [date] = A.[date] And note != A.note) Then Null Else A.note End) As note
From
表 A
------解决方案--------------------
测试表Test
select distinct B.date, case when B.count> 1 then 'null '
else A.note
end note
from Test A,(select count(distinct note) count,date from Test group by date) B
where A.date=B.date

结果为
date note
2001-1-1 null
2001-1-2 fngd
2001-1-3 null