目前,我的代码如下:
SELECT Volunteer.VolunteerID, Volunteer.LastName, Volunteer.FirstName, Observations.VolunteerID, Roost.RoostID, Roost.UTME, Roost.UTMN, Roost.DecimalDegrees, Observations.Date, Location.LocationName, Location.Address, Town.Town
FROM (((Volunteer INNER JOIN Observations ON Volunteer.[VolunteerID]= Observations.[VolunteeriD])
LEFT JOIN Roost ON Roost.[RoostID]= Observations.[RoostID])
LEFT JOIN Location ON Location.[LocationID]= Roost.[LocationID])
LEFT JOIN Town ON Location.[TownID]= Town.[TownID]
ORDER BY Volunteer.LastName, Volunteer.FirstName;
目前,我的表格的一部分如下所示:
Observations.VolunteerID | Observations.LocationID | Date
Bob | Main St | 2015-01-01
Bob | Main St | 2015-02-02
Sally | Fox St | 2015-02-02
Dave | Long St | 2015-02-02
Dave | Taylor St | 2015-02-05
Lindsay | New St | 2015-02-01
Lindsay | New St | 2015-02-08
Lindsay | Ray St | 2015-02-10
Lindsay | Main St | 2015-02-25
Lindsay | Taylor St | 2015-02-31
但是,我想添加一个由{em> VolunteerID 和 LocationID 分组的GROUP BY
语句(我认为),以便获得以下输出。我想知道志愿者所在的每个位置,如果他们多次访问同一地点,我希望看到最近的日期。我如何将其纳入我的原始代码?
Observations.VolunteerID | Observations.LocationID | Date
Bob | Main St | 2015-02-02
Sally | Fox St | 2015-02-02
Dave | Long St | 2015-02-02
Dave | Taylor St | 2015-02-05
Lindsay | New St | 2015-02-08
Lindsay | Ray St | 2015-02-10
Lindsay | Main St | 2015-02-25
Lindsay | Taylor St | 2015-02-31
更新了代码,但仍然出现语法错误(查询表达式中缺少运算符' Volunteer.FirstName GROUP BY Observations.VolunteerID'。
SELECT Volunteer.VolunteerID, Volunteer.LastName, Volunteer.FirstName, Observations.VolunteerID, Roost.RoostID, Roost.UTME, Roost.UTMN, Roost.DecimalDegrees, Max([Observations.Date]), Location.LocationName, Location.Address, Town.Town
FROM (((Volunteer INNER JOIN Observations ON Volunteer.[VolunteerID]= Observations.[VolunteeriD])
LEFT JOIN Roost ON Roost.[RoostID]= Observations.[RoostID])
LEFT JOIN Location ON Location.[LocationID]= Roost.[LocationID])
LEFT JOIN Town ON Location.[TownID]= Town.[TownID]
ORDER BY Volunteer.LastName, Volunteer.FirstName
GROUP BY Observations.VolunteerID, Observations.LocationID;
答案 0 :(得分:3)
max
子句的 group by
似乎已经足够了:
select Observations.VolunteerID, Observations.LocationID, max([Date])
from <...>
group by Observations.VolunteerID, Observations.LocationID
答案 1 :(得分:1)
您需要在日期使用public static string ExternalUrl(this PageData p, bool absoluteUrl, string languageBranch)
{
var result = ServiceLocator.Current.GetInstance<UrlResolver>().GetUrl(
p.ContentLink,
languageBranch,
new VirtualPathArguments
{
ContextMode = ContextMode.Default,
ForceCanonical = absoluteUrl
});
// HACK: Temprorary fix until GetUrl and ForceCanonical works as expected,
// i.e returning an absolute URL even if there is a HTTP context that matches the page's
// site definition and host.
if (absoluteUrl)
{
Uri relativeUri;
if (Uri.TryCreate(result, UriKind.RelativeOrAbsolute, out relativeUri))
{
if (!relativeUri.IsAbsoluteUri)
{
var siteDefinitionResolver = ServiceLocator.Current.GetInstance<SiteDefinitionResolver>();
var siteDefinition = siteDefinitionResolver.GetDefinitionForContent(p.ContentLink, true, true);
var hosts = siteDefinition.GetHosts(p.Language, true);
var host = hosts.FirstOrDefault(h => h.Type == HostDefinitionType.Primary) ?? hosts.FirstOrDefault(h => h.Type == HostDefinitionType.Undefined);
var basetUri = siteDefinition.SiteUrl;
if (host != null)
{
// Try to create a new base URI from the host with the site's URI scheme. Name should be a valid
// authority, i.e. have a port number if it differs from the URI scheme's default port number.
Uri.TryCreate(siteDefinition.SiteUrl.Scheme + "://" + host.Name, UriKind.Absolute, out basetUri);
}
var absoluteUri = new Uri(basetUri, relativeUri);
return absoluteUri.AbsoluteUri;
}
}
}
return result;
}
和汇总函数GROUP BY
。
MAX()
答案 2 :(得分:0)
将ORDER BY
子句置于GROUP BY
之后。
将Max([Observations.Date])
更改为Max(Observations.Date)
将SELECT
列表中不是聚合表达式的任何字段表达式添加到GROUP BY
。
SELECT
Volunteer.VolunteerID,
Volunteer.LastName,
Volunteer.FirstName,
Observations.VolunteerID,
Roost.RoostID,
Roost.UTME,
Roost.UTMN,
Roost.DecimalDegrees,
Max(Observations.Date) AS latest_observation,
Location.LocationName,
Location.Address,
Town.Town
FROM (((Volunteer INNER JOIN Observations ON Volunteer.[VolunteerID]= Observations.[VolunteeriD])
LEFT JOIN Roost ON Roost.[RoostID]= Observations.[RoostID])
LEFT JOIN Location ON Location.[LocationID]= Roost.[LocationID])
LEFT JOIN Town ON Location.[TownID]= Town.[TownID]
GROUP BY
Volunteer.VolunteerID,
Volunteer.LastName,
Volunteer.FirstName,
Observations.VolunteerID,
Roost.RoostID,
Roost.UTME,
Roost.UTMN,
Roost.DecimalDegrees,
Location.LocationName,
Location.Address,
Town.Town
ORDER BY Volunteer.LastName, Volunteer.FirstName;
但我不确定该分组是否合适。在这个问题中,您希望对 Observations.VolunteerID 和 Observations.LocationID 进行分组。较大的GROUP BY
可能不会给您相同的结果。
如果是这种情况,您可以对SELECT
字段使用聚合函数,然后从GROUP BY
中删除该字段。例如......
SELECT Max(Volunteer.LastName)
假设 LastName 对于给定的 VolunteerID 始终相同,Max(Volunteer.LastName)
将为您提供相同的值。