修改我的查询以使用最新的" logcreateddate"提取数据。

时间:2015-11-04 09:38:43

标签: sql-server ssms

我使用的是Sql Server Management Studio 2012,我创建了一个sql查询,可以在下面找到:

b

我已经运行了查询。输出表包含针对不同SELECT workorder.wonum as "WorkOrderNo", Workorder.description as "WorkorderDescription", Workorder.status as "WOstatus", Workorder.reportdate as "WOReportedDate", workorder.plusgmatrixprior as "WOPriority", wfassignment.startdate as "WorkflowStartDate", wfassignment.assigncode as "AssignedToLoginID", wfassignment.description as "AssignmentDescription", worklog.createby as "LogEnteredby", worklog.createdate as "LogCreatedDate", worklog.eventcode as "EventCode", worklog.logtype as "LogType", worklog.delayed as "ReasonforDelay", worklog.description as "LogDescription", worklog.description_long as "LoglongDescription" FROM wfassignment, workorder, worklog WHERE wfassignment.ownertable = 'workorder' AND wfassignment.ownerid = workorder.workorderid AND worklog.recordkey = workorder.wonum AND worklog.class = workorder.woclass AND workorder.siteid = 'adrd' AND wfassignment.assignstatus = 'active' AND wfassignment.assigncode = 'TRW4074' AND worklog.createby = 'TRW4074' ORDER BY workorder.wonum desc; 具有相同WorkOrderNo的一些行。

我希望使用最新或最大LogCreatedDate获取上述查询中的所有字段。

注意:我尝试使用LogCreatedDate函数和max()。但它没有用,因为其他领域并不是唯一的。因此输出包含group by的重复项。

请帮助修改上述查询或向我提供获取最新日期的所有字段的建议。

1 个答案:

答案 0 :(得分:1)

使用SELECT wo.wonum AS "WorkOrderNo", wo.description AS "WorkorderDescription", wo.status AS "WOstatus", wo.reportdate AS "WOReportedDate", wo.plusgmatrixprior AS "WOPriority", wa.startdate AS "WorkflowStartDate", wa.assigncode AS "AssignedToLoginID", wa.description AS "AssignmentDescription", wl.createby AS "LogEnteredby", wl.createdate AS "LogCreatedDate", wl.eventcode AS "EventCode", wl.logtype AS "LogType", wl.delayed AS "ReasonforDelay", wl.description AS "LogDescription", wl.description_long AS "LoglongDescription" FROM wfassignment wa JOIN workorder wo ON wa.ownerid = wo.workorderid OUTER APPLY(SELECT TOP 1 * FROM worklog wl WHERE wl.recordkey = wo.wonum AND wl.class = wo.woclass AND wl.createby = 'TRW4074' ORDER BY wl.createdate DESC) wl WHERE wa.ownertable = 'workorder' AND wo.siteid = 'adrd' AND wa.assignstatus = 'active' AND wa.assigncode = 'TRW4074'

linq