日期:2014-05-17  浏览次数:20473 次

求一条拆分SQL语句

如何把下面的语句变成:
2012-09-05 2012-09-05 JD001 A中第六條 2012-09-05 JD000 B中第三條
2012-09-05 2012-09-05 JD001 A中第六條 2012-09-05 JD002 B中第六條

SQL code

2012-09-05 2012-09-05     JD001    A中第六條    '','',''
 '','',''                                       2012-09-05     JD000    B中第三條
 '','',''                                    2012-09-05     JD002    B中第六條



日期空格为1900-01-01 00:00:00也可以

------解决方案--------------------
这个真不知道,帮你顶一下吧
------解决方案--------------------
没太看明白 但是可以给你个类似的案例
SQL code

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
    */

------解决方案--------------------
SQL code
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