在Access 2010中的父级别汇总子记录

时间:2015-12-18 21:41:18

标签: sql ms-access ms-access-2010

上面的链接将提供一张excel表格,其中包含来自父表和子表的一些样本数据以及查询的预期结果。

好吧这应该很简单,但我出于某种原因无法绕过它。差不多,我有一个父表连接到子表。我想从子表中提取一些字段并将其与父字段合并。我想在Access中创建排序视图。

父记录可以有多个子记录(1 - 多关系)。我想只从孩子那里拿出一条记录并与父母合并。父表名为Tank,子表是Tank_Inspections。您在下面看到的IF语句是一个条件语句,有助于确定我应该提升哪个不合规日期。我遇到的问题是,不合规日期与检验类型有关。坦克可以有多种不同的检查类型。他们在下面查询将符合日期的检查与几个坦克(父)字段合并。但是,我希望能够添加更多子字段(除了符合日期之外的检查),但我不能在不将这些字段添加到group by子句的情况下执行此操作。如果我这样做,那么我就不会获得正确数量的记录。

正如您所看到的,左连接是从父表中获取所需的所有记录。如果我在查询中添加更多子表字段,我还需要将它们添加到group by子句中,然后我将获得比父表中的内容更多的记录。本质上,我只需要从父表中获取记录,然后合并子字段。我可能会遗漏几个子查询...有什么建议吗?这是我到目前为止所获得的,并且我获得了适量的记录。但是在select语句中添加更多的子字段会添加比我需要的更多的行...

SELECT parent.tankid, IIf(Min(Nz(child.[tank inspection out of compliance date], #1/1/1901#)) <> #1/1/1901#, Min(child.[tank inspection out of compliance date]), IIf(Min(Nz(child.[tank inspection out of compliance date],#1/1/1901#)) = #1/1/1901# And Max(child.[tank inspection out of compliance date])>Date(), NULL, Min(child.[tank inspection out of compliance date]))) AS [Tank Inspection Out of Compliance Date] 
FROM 
tank as parent
LEFT JOIN
(
SELECT * FROM tank_inspections WHERE tank_inspections.[actual inspection date] is null
)  AS child ON parent.tankid = child.tankid GROUP BY parent.tankid

我能够修改下面的Parfait建议查询来提出这个:

SELECT 
Site.[Manager] AS PM, Site.[DLA Site Code], Tank.[Name] AS [Name], Tank.[Local Name], 
Tank.RPID, Tank.[Fac Num], Tank.[Status], Tank.[Type], Tank.[Capacity], Tank.[Current Prod], IIf(main.[Inspection Out of Compliance Date]<Date() AND NOT IsNull(main.[Inspection Out of Compliance Date]), 'Out of Compliance',
IIf(isnull(main.[Inspection Out of Compliance Date]) OR main.[Inspection Out of Compliance Date]=#1/1/1901#,'Unknown Compliance Status')) AS [Compliance Status], Tank.[EA], Site.Serv, Site.[Name], Tank.Comments, main.[Type], main.[Inspection Out of Compliance Date], main.[Planned Prog Date], main.[Prog Date], main.[Prog Year], main.[Planned Inspection Date], IIf(main.[Inspection Out of Compliance Date]<DateAdd('m',12,Date()) And main.[Prog Date] Is Null,'Action Required') AS [Inspection Planning Action Required], main.[Inspection Comments], tank.TankID, main.inspectionid
FROM 
Site INNER JOIN 
(
(
(
SELECT ti.tankid, ti.inspectionid, ti.[Type], ti.[Inspection Out of Compliance Date], ti.[Planned Prog Date], ti.[Prog Date], ti.[Prog Year], ti.[Planned Inspection Date], ti.[Inspection Comments] FROM Tank_Inspections AS ti)  AS main INNER JOIN Tank ON main.TankID = Tank.TankID) INNER JOIN 
(
SELECT [TankID], dlookup("InspectionID", "Tank_Inspections", "[Tank Inspection Out of Compliance Date] " & IIf(Min(Nz([inspection out of compliance date], #1/1/1901#)) <> #1/1/1901#, "= #" & Min([inspection out of compliance date]) & "#", IIf(Min(Nz([inspection out of compliance date],#1/1/1901#)) = #1/1/1901# And Max([inspection out of compliance date])>Date(), "IS NULL", IIF(Min(Nz([inspection out of compliance date],#1/1/1901#)) = #1/1/1901# And Max([inspection out of compliance date])<Date(), "= #" & Min([inspection out of compliance date]) & "#", "IS NULL"))) & " AND TankID = " & TankID & " AND [Actual Inspection Date] is null") AS MinInspectionID FROM Tank_Inspections WHERE [Actual Inspection Date] is null GROUP BY [TankID]
)AS DT ON 
(
main.InspectionID = Cint(DT.MinInspectionID)
) AND (main.TankID = DT.TankID)
) ON Site.SiteID = Tank.SiteID
WHERE IIf(main.[Inspection Out of Compliance Date]<Date() And NOT IsNull(main.[Inspection Out of Compliance Date]),'Out of Compliance',IIf(isnull(main.[Inspection Out of Compliance Date]) OR main.[Inspection Out of Compliance Date]=#1/1/1901#,'Unknown Compliance Status'));

我已接近此查询,但是,我错过了一些记录。父记录没有一些子记录。例如,一些坦克记录没有任何检查记录,因此它没有被拉动。我需要做一个左连接,但似乎无法用这个查询搞清楚。我尝试的一切似乎都不起作用。建议?

0 个答案:

没有答案