我使用下面的子查询从我的查询中返回每个(文档编号)的最大数据,但是当我有很多记录时它只返回一条记录。
SELECT tblTransmittals.[Owner Document Number]
, tblTransmittals.[Int-Tr- NO]
, tbltransmittalNo.[Internal-Tr-Date]
FROM tblTransmittals
INNER JOIN tbltransmittalNo
ON tblTransmittals.[Int-Tr- NO] = tbltransmittalNo.[Int-Tr-NO]
WHERE (((tbltransmittalNo.[Internal-Tr-Date])=
(SELECT Max(A.[Internal-Tr- Date])
FROM (SELECT tblTransmittals.[Owner Document Number]
, tbltransmittalNo. [Internal-Tr-Date]
FROM tblTransmittals
INNER JOIN tbltransmittalNo
ON tblTransmittals.[Int-Tr-NO] = tbltransmittalNo.[Int-Tr-NO]
) AS A
WHERE A.[owner Document Number]=[tblTransmittals].[Owner Document Number])));
我的意思是如果查询数据如下:
Owner Document Number Int-Tr-NO Internal-Tr-Date
doc-0001 tt-0002 10-oct-2016
doc-0001 tt-0003 12-0ct-2017
doc-0005 tt-0100 18-sep-2015
我希望在使用子查询后返回:
Owner Document Number Int-Tr-NO Internal-Tr-Date
doc-0001 tt-0003 12-0ct-2017
doc-0005 tt-0100 18-sep-2015
我试过并检查了一切,但我不知道为什么不起作用?
答案 0 :(得分:0)
不完全清楚你想要什么,我假设这是sql server。认为你想要这样的东西。
select *
from
(
SELECT t.[Owner Document Number]
, t.[Int-Tr- NO]
, tn.[Internal-Tr-Date]
, RowNum = ROW_NUMBER() over (partition by t.[Owner Document Number] order by tbltransmittalNo.[Internal-Tr-Date] desc)
FROM tblTransmittals t
INNER JOIN tbltransmittalNo tn ON t.[Int-Tr- NO] = tn.[Int-Tr-NO]
) x
where x.RowNum = 1
答案 1 :(得分:0)
SELECT tblTransmittals.[Owner Document Number], tblTransmittals.[Int-Tr-NO], tbltransmittalNo.[Internal-Tr-Date]
FROM tblTransmittals INNER JOIN tbltransmittalNo ON tblTransmittals.[Int-Tr-NO] = tbltransmittalNo.[Int-Tr-NO]
WHERE (((tbltransmittalNo.[Internal-Tr-Date])=
(SELECT Max([Internal-Tr-Date])
FROM (SELECT T.[Owner Document Number], T.[Int-Tr-NO], TN.[Internal-Tr-Date]
FROM tblTransmittals AS T LEFT JOIN tbltransmittalNo AS TN ON T.[Int-Tr-NO] = TN.[Int-Tr-NO]) AS A
WHERE A.[owner Document Number]=[tblTransmittals].[Owner Document Number])));
答案 2 :(得分:0)
您的查询完全不正确,因为它只返回一行,因为您的子查询中有max():
(SELECT Max(A.[Internal-Tr- Date])
FROM (SELECT tblTransmittals.[Owner Document Number], tbltransmittalNo. [Internal-Tr-Date]
FROM tblTransmittals INNER JOIN tbltransmittalNo
ON tblTransmittals.[Int-Tr-NO] = tbltransmittalNo.[Int-Tr-NO]) AS A
WHERE A.[owner Document Number]=[tblTransmittals].[Owner Document Number]);
##this query only returns 1 value
在上面的场景中,max()查询将返回“12-0ct-2017”,您的最终查询将如下所示:
SELECT tblTransmittals。[Owner Document Number],tblTransmittals。[Int-Tr-NO],tbltransmittalNo。[Internal-Tr-Date] FROM tblTransmittals INNER JOIN tbltransmittalNo ON tblTransmittals。[Int-Tr-NO] = tbltransmittalNo。[Int-Tr-NO] WHERE(((tbltransmittalNo。[Internal-Tr-Date])='20171012'
我猜你想要在每个[所有者文件编号]上返回最大(日期),所以我会这样做:
SELECT A. [所有者文件编号],A。[Int-Tr-NO],B。[Internal-Tr-Date] FROM tblTransmittals as INNER JOIN tbltransmittalNo as B ON A. [Int-Tr-NO] = B. [Int-Tr-NO] 加入 (选择C. [所有者文件编号],最大(D. [内部 - 日期]) FROM tblTransmittals as C INNER JOIN tbltransmittalNo as D ON C. [Int-Tr-NO] = D. [Int-Tr-NO] GROUP BY C. [所有者文件编号])AS E. ON A. [所有者文档编号] = E. [所有者文档编号] 和B. [Internal-Tr-Date] = E. [Internal-Tr-Date]
祝你好运。