SQL 去掉重复的表记录

SELECT * FROM(
SELECT row_number() over(partition by wf.process_instance_id,formResourceName order by handle_time DESC) as rownum ,
mr.InstallInformNO,mr.ID,mr.RecordNumber,mr.BelongedTo, mr.PropertyNumber,mr.RemovalInformNO,mr.RemovalInformDate,mr.RemovalDate,mr.UnitID,
mi.UseBeginDate,mi.UseEndDate,mi.InstallDate,
wf.order_id AS orderId,wf.process_instance_id AS processInstanceId,'' AS taskId,wf.handle_time AS taskCreateTime,wf.formResourceName,
( CASE WHEN wf.handle_result ='1' THEN '通过' ELSE '不通过' END ) AS handleResult,'' AS handleComment ,
po.ProjectName,
mi.MechanicalName,mi.MachineEquipmentUnit,mi.ProdConformity,mi.EquipmentManufactureDate,mi.UsefulLifeBegin,mi.UsefulLifeEnd,mi.PropertyType,mi.Manufacturer,
mi.UseRegistrationNO,mi.UseRegistrationDate,
ir.UnitName,
(SELECT TOP 1 handle_time FROM dbo.workflow_trans
WHERE order_id =wf.order_id AND formResourceName='DaXingJiXie_007' AND handle_result='1'
ORDER BY handle_time ASC) AS GaoZhiTime,
'已处理' AS UnitType
FROM dbo.workflow_trans AS wf
INNER JOIN MechanicalRemovalInform AS mr ON wf.order_id= mr.BelongedTo+'&'+mr.RecordNumber+mr.InstallInformNO
LEFT JOIN dbo.ProjectOverview AS po ON po.BelongedTo = mr.BelongedTo AND po.RecordNumber = mr.RecordNumber
-- LEFT JOIN MechanicalProperty AS mp ON mp.PropertyNumber = mr.PropertyNumber
INNER JOIN MechanicalInstallInform AS mi ON mi.InstallInformNO = mr.InstallInformNO AND mi.BelongedTo=mr.BelongedTo
LEFT JOIN InstRemovalUnitSnapshot AS ir ON ir.RecordNumber=mr.RecordNumber AND ir.BelongedTo=mr.BelongedTo AND ir.PropertyNumber = mr.PropertyNumber AND ir.UnitType='2' AND mr.RemovalInformNO=ir.InstallInformNO
WHERE formResourceName='DaXingJiXie_008'
)AS t WHERE t.rownum=1

posted @ 2016-03-22 18:46  管学文  阅读(148)  评论(0编辑  收藏  举报