日期:2014-05-17 浏览次数:20473 次
2012-09-05 2012-09-05 JD001 A中第六條 '','','' '','','' 2012-09-05 JD000 B中第三條 '','','' 2012-09-05 JD002 B中第六條
DECLARE @TAB TABLE ([col1] varchar(1),[col2] int,[col3] int) insert @TAB select 'a',1,2 union all select 'a',2,3 union all select 'a',3,4 union all select 'b',4,5 union all select 'b',5,6 union all select 'b',6,7 --------------开始查询-------------------------- select case px when 1 then col1 else '' end as col1, col2,col3 from (select px=row_number()over(partition by col1 order by getdate()),* from @TAB)t /* col1 col2 col3 a 1 2 2 3 3 4 b 4 5 5 6 6 7 */
------解决方案--------------------
SELECT * FROM TESTA a left join TESTB b on a.recdate= b.recdate AND NOT EXISTS(SELECT 1 FROM TESTB WHERE recdate=b.recdate AND rmk<>b.rmk) UNION ALL SELECT a.recdate,'','',b.* FROM TESTA a join TESTB b on a.recdate= b.recdate AND EXISTS(SELECT 1 FROM TESTB WHERE recdate=b.recdate AND rmk<>b.rmk) ORDER BY a.recdate