0
SELECT     dbo.Team.ID, dbo.Team.Comment, dbo.Team.Date, dbo.TeamName.Name, dbo.Contacts.ContactName 
FROM       dbo.Team 
INNER JOIN dbo.TeamName ON dbo.Team.ID = dbo.TeamName.id 
INNER JOIN dbo.Contacts ON dbo.Team.ContactID = dbo.Contacts.ContactID   
WHERE      dbo.TeamName.idstatus = 'Active'

我试图以一种只给我最近日期的记录的方式查询它,但它变得有点混乱,因为我已经从 3 个表中提取数据并且不知道如何在MAX(date)此处使用。

4

3 回答 3

1
SELECT     dbo.Team.ID, dbo.Team.Comment, MAX(dbo.Team.Date) LatestDate, dbo.TeamName.Name, dbo.Contacts.ContactName 
FROM       dbo.Team 
INNER JOIN dbo.TeamName ON dbo.Team.ID = dbo.TeamName.id 
INNER JOIN dbo.Contacts ON dbo.Team.ContactID = dbo.Contacts.ContactID   
WHERE      dbo.TeamName.idstatus = 'Active'
GROUP BY   dbo.Team.ID, dbo.Team.Comment, dbo.TeamName.Name, dbo.Contacts.ContactName 

将 MAX() 函数添加到您的日期列,并按所有列分组,即所有其他列中的值组合的最新日期。

于 2013-10-08T21:57:06.913 回答
0

我假设你想过滤dbo.Team.Date.

WHERE将您的条款更改为:

WHERE (dbo.TeamName.idstatus = 'Active') 
and (dbo.Team.Date=(select max(Date) from dbo.Team))
于 2013-10-08T21:26:16.853 回答
0

使用 GroupBy 并拥有:

SELECT     dbo.Team.ID, dbo.Team.Comment, MAX(dbo.Team.Date), dbo.TeamName.Name,   dbo.Contacts.ContactName, 
FROM         dbo.Team 
INNER JOIN
dbo.TeamName ON dbo.Team.ID = dbo.TeamName.id 
INNER JOIN
dbo.Contacts ON dbo.Team.ContactID = dbo.Contacts.ContactID
WHERE     (dbo.TeamName.idstatus = 'Active')
GROUP BY dbo.Team.ID, dbo.Team.Comment, dbo.Team.Date, dbo.TeamName.Name, dbo.Contacts.ContactName
HAVING dbo.Team.Date = MAX(dbo.Team.Date)
于 2013-10-08T21:31:37.893 回答