0

需要帮助以获得不同的记录:

位置表

posid   |    issuerid
----------------------

其他表

OtherID    |  issuerid   |   issue_date 
--------------------------------------

我想

OtherID    |  issuerid   |   Posid
--------------------------------------

对于某些发行者,Issue_date 可能为空。我想要那些其他 id、发行者 id 和 posid,如果发行者有任何发行日期,则发行日期为最大值,否则为 null

4

3 回答 3

0
SELECT otherId, issuerId, posId
FROM (
    SELECT issuerId, MAX(ISNULL(issueDate,'17530101')) maxDate
    FROM otherTable
    GROUP BY issuerId
    ) M
JOIN otherTable O ON O.issuerId = M.issuerId AND O.issueDate = M.maxDate
JOIN positionTable P ON P.issuerId = M.issuerId

根据 MS 的说法,日期 01/01/1753 是 SQL 2008 的日期时间字段中的最小值。当然,您可以使用任何值,只要它小于表中的最小值即可。

于 2013-10-04T10:35:18.083 回答
0
SELECT OT.OtherID,PT.issuerid,PT.posid FROM [Position table] PT INNER JOIN [Other Table] OT ON PT.issuerid=OT.issuerid WHERE OT.issue_date IN
(SELECT MAX(issue_date) FROM [Other Table])
于 2013-10-04T10:43:24.080 回答
0
select a.orderid, a.issueid, b.posid
from other a, position b
where a.issueid=b.issueid
and (a.issuedate = (select max(c.issuedate) from other c where c.issueid = a.issueid)
or a.issuedate is null)
于 2013-10-04T10:44:23.587 回答