帮助构建SQL查询:
Select Distinct ilc.itemnum, id.itemname, statename, realname
From hsi.itemlc ilc
INNER JOIN hsi.itemdata id ON
id.itemnum = ilc.itemnum
INNER JOIN hsi.lcstate lcs ON
lcs.statenum = ilc.statenum
INNER JOIN hsi.useraccount ua ON
ua.usernum = id.usernum
INNER JOIN hsi.wftransactionlog wft ON
id.itemnum = wft.itemnum
Where ilc.statenum not in ('143','144')
and ilc.lcnum in ('101','252')
and ilc.status <> 16
在我最后一次加入hsi.wftransactionlog时,我只想在id.itemnum = wft.itemnum时拉出最大的wft.logdate。完成此任务的最佳方法是什么? hsi.wftransactionlog中有几个“itemnum”,但我只想在最近的wft.logdate中获取表中的匹配项。
如果这没有意义,请告诉我。
答案 0 :(得分:0)
您可以对子查询使用CROSS APPLY:
SELECT DISTINCT ilc.itemnum, id.itemname, statename, realname
From hsi.itemlc ilc
INNER JOIN hsi.itemdata id ON
id.itemnum = ilc.itemnum
INNER JOIN hsi.lcstate lcs ON
lcs.statenum = ilc.statenum
INNER JOIN hsi.useraccount ua ON
ua.usernum = id.usernum
CROSS APPLY ( SELECT MAX(logdate) AS logdate
FROM hsi.wftransactionlog wft
WHERE id.itemnum = wft.itemnum
) wft1
答案 1 :(得分:0)
在黑暗中没有任何要测试的示例数据,但是使用子查询可能对你有效。
Select Distinct ilc.itemnum, id.itemname, statename, realname,
( select max(wft.logdate)
hsi.wftransactionlog wft
where wft.itemnum = id.itemnum
) as logdate
From hsi.itemlc ilc
INNER JOIN hsi.itemdata id ON id.itemnum = ilc.itemnum
INNER JOIN hsi.lcstate lcs ON lcs.statenum = ilc.statenum
INNER JOIN hsi.useraccount ua ON ua.usernum = id.usernum
Where ilc.statenum not in ('143','144')
and ilc.lcnum in ('101','252')
and ilc.status <> 16