我一直在使用SSMS进行SQL查询。我的查询与一对具有相关数据的表建立一对多关系,我使用子查询仅返回这些表中的单个行。
swl
。offhire
日期定义的Fixtures表中的最新Fixture。使用VesselId
表中的Vessel
将数据全部捆绑在一起。当我一直在处理第二个子查询时,应该返回我最近开始遇到的错误的那个子查询:
选择列表中的列“ dbo.Fixture.VesselId”无效,因为它 既不包含在汇总函数中,也不包含在GROUP BY中 条款。
这是我的查询。
SELECT v.VesselId,
v.Name,
v.DPClassId as DP,
crn.Crane,
cap.DeckAreaM2 as Deck,
fixture.FixtureId,
fixture.Offhire,
fixture.Onhire,
fixture.Scope,
fixture.Location,
fixture.Rate,
ss.Accomodation as Beds,
ss.RovHangar as ROV
FROM dbo.Vessel as v
LEFT JOIN dbo.DeckEquipment ON v.VesselId = dbo.DeckEquipment.VesselId
LEFT JOIN (
SELECT
dbo.Crane.DeckEquipmentId,
MAX(dbo.Crane.SWL) AS Crane
FROM dbo.Crane
GROUP BY dbo.Crane.DeckEquipmentId
) AS crn ON dbo.DeckEquipment.DeckEquipmentId = crn.DeckEquipmentId
LEFT JOIN dbo.Subsea AS ss on v.VesselId = ss.VesselId
LEFT JOIN dbo.Capacity AS cap on v.VesselId = cap.VesselId
LEFT JOIN (
SELECT
dbo.Fixture.FixtureId,
dbo.Fixture.VesselId,
dbo.Fixture.Onhire,
dbo.Fixture.Scope,
dbo.Fixture.Rate,
dbo.Fixture.Location,
dbo.Fixture.PrimaryChartererId,
MAX(dbo.Fixture.Offhire) as Offhire
FROM dbo.Fixture
GROUP BY dbo.fixture.FixtureId
) AS fixture on v.VesselId = fixture.VesselId
如您所见,那里的第二个子查询在Offhire日期使用MAX
返回灯具,以带回最新的灯具。当我运行查询,我得到上述错误。
我返回最近的灯具的方法是否不正确?我应该按所有领域分组吗?我不确定如何使用此子查询。任何帮助表示赞赏。