无法对包含SubQuery的查询进行交叉表查询

时间:2017-06-20 14:58:03

标签: sql ms-access

我的查询包含子查询:从我的表格中计算出发和到达时间之间的时间间隔"时间表"

这个查询工作得非常好,但是当尝试从交叉表执行它时,它会提示我一个错误,它无法找到表" a"这是我用于"时间表"

的别名
SELECT a.VesselID, a.MovementID, a.MovementTime, (SELECT TOP 1 
 Timetable.MovementTime
 FROM Timetable
 WHERE (((Timetable.MovementID)="Arrival") AND 
 ((Timetable.VesselID)=a.VesselID]) AND ((Timetable.MovementTime)>a.
 [MovementTime]))
 ORDER BY Timetable.MovementTime) AS Arrival1,
DateDiff('h',[a].[MovementTime],[Arrival1]) AS [Interval]
FROM Timetable AS a INNER JOIN Timetable ON a.ID = Timetable.ID
WHERE (((a.MovementID)="Departure"));

我认为这个Question非常相似,解决方案就是我将我的查询拆分为@DHW说,但我无法做到。

这是我尝试拆分:

[Departure_Query]
    SELECT Timetable.VesselID, Timetable.MovementTime AS mymov, 
    Timetable.MovementID
    FROM Timetable
    WHERE (((Timetable.MovementID)="Departure"));

[Main]
    SELECT Timetable.MovementTime, Timetable.MovementID, Timetable.VesselID, Departure_Query.mymov, DateDiff('h',[mymov],[MovementTime]) AS [Interval]
    FROM Timetable INNER JOIN Departure_Query ON Timetable.VesselID = Departure_Query.VesselID
    WHERE (((Timetable.MovementTime)>[Departure_Query].[mymov]) AND ((Timetable.MovementID)="Arrival") AND ((Timetable.VesselID)=[Departure_Query].[VesselID]))
    ORDER BY Timetable.MovementTime;

我认为问题是:
在工作查询中,我可以放SELECT TOP 1,但在拆分尝试中我不知道放在哪里。

更新实际上,现在我想拆分它,因为当我试图在其中构建报告时。它提示我Access无法对此字段进行分组。

但无论如何这是我的尝试

TRANSFORM DateDiff('h',[a].[MovementTime],[Arrival1]) AS [Interval]
SELECT a.MovementTime
FROM Timetable AS a INNER JOIN Timetable ON a.ID = Timetable.ID
WHERE (((a.MovementID)="Departure"))
GROUP BY a.MovementID, a.MovementTime, (SELECT TOP 1 Timetable.MovementTime
FROM Timetable
WHERE (((Timetable.MovementID)="Arrival") AND ((Timetable.VesselID)=a.[VesselID]) AND ((Timetable.MovementTime)>a.[MovementTime]))
ORDER BY Timetable.MovementTime)
PIVOT a.VesselID;

The results The Design View

2 个答案:

答案 0 :(得分:0)

考虑使用域聚合的交叉表,DMin()来替换子查询:

TRANSFORM DateDiff('h', main.[MovementTime], main.[Arrival1]) AS [Interval]
SELECT main.MovementID, main.MovementTime
FROM
   (SELECT t.VesselID, t.MovementID, t.MovementTime, 
           DMin("MovementTime", "Timetable", "MovementID = 'Arrival' 
                 AND VesselID = " & t.VesselID & " 
                 AND MovementTime > #" & t.MovementTime & "#") As Arrival1
    FROM Timetable AS t
    WHERE (((t.MovementID) = 'Departure'))
   ) As 
GROUP BY main.MovementID, main.MovementTime
PIVOT main.VesselID;

答案 1 :(得分:0)

谢谢@Parfait和@ June7,我正在添加这个答案,以便将来任何人都可以从这个问题中受益。

问题
我发现问题是:查询减去特定船只的所有较小的出发日期 即船舶1号出发6 / 1,6 / 3,6 / 6,到达6 / 2,6 / 2,6 / 8。所以最后一天减去 6 / 8-6 / 6 ,6 / 8-6 / 3,6 / 8-6 / 1。当然,唯一的第一个(粗体)是正确的。

解决方案

SELECT Min(Timetable.MovementTime) AS MinOfMovementTime, Departure_Query.mymov AS DeptDate, Min(DateDiff('h',[mymov],[MovementTime])) AS WorkingH, Timetable.MovementID, Timetable.VesselID
FROM Timetable LEFT JOIN Departure_Query ON Timetable.VesselID = Departure_Query.VesselID
WHERE (((Timetable.MovementID)="Arrival") AND ((Timetable.VesselID)=[Departure_Query].[VesselID]) AND ((Timetable.MovementTime)>[mymov]))
GROUP BY Departure_Query.mymov, Timetable.MovementID, Timetable.VesselID
ORDER BY Min(Timetable.MovementTime);

这里唯一的变化是Min(DateDiff('h',[mymov],[MovementTime])),它只给出最小的减法值,这意味着最大的出发日期。