SELECT REPAIRBILLNO AS RepairNo,WishNO AS WishNO,WorkNO as WorkNO,Id AS Id,WORKITEMCODE AS JobNo,WORKITEMNAME AS JobName,REPAIRKFEE AS OutLay,WORKERNAME AS Executor1 INTO CAR_RT_RepairJob_New FROM
(SELECT *,ROW_NUMBER() OVER(PARTITION BY REPAIRBILLNO,WishNO,WorkNo ORDER BY REPAIRBILLNO) Id
FROM (SELECT *,ROW_NUMBER() OVER(PARTITION BY REPAIRBILLNO,WishNO ORDER BY REPAIRBILLNO) WorkNO
FROM (SELECT REPAIRBILLNO,ROW_NUMBER() OVER(PARTITION BY REPAIRBILLNO,CUSTNOTICE ORDER BY REPAIRBILLNO) WishNO,WORKITEMCODE,WORKITEMNAME,REPAIRKFEE,WORKERNAME FROM dbo.V_IT_INTERFACE_RF
WHERE MODIFYTIME >(SELECT CASE(ISNULL(MAX(UpdateDateTime),'')) WHEN '' THEN '' ELSE MAX(UpdateDateTime) END FROM CAR_RT_RepairJob)
)t
)tt
)ttt
打完这纠结的一串SQL后,在Sqlserver2005执行,还好,效率并没有想象中的那么低。大概的需求是这样的:
将表A中的a、b、c、字段同步到(新增或更新)表B对应的字段中,但是B中有四个联合主键O、P、Q、M,当O相同时P依次加1,O、P相同时Q加1,依次类推....... 敲了下,还好,大致的思维,只是多了几层嵌套而已。