我收到此错误:无法在GROUP BY子句列表中使用的表达式中使用聚合或子查询。
我环顾四周,但在我的情况下找不到任何可以帮助我的东西。
我正在使用SQL Server 2012
SELECT DISTINCT
CommodityCode.CommodityCode AS ComCode,
Facility.FacilityAcctNum,
Vessel.VesselType,
Facility.FacilityNm,
Case
When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End AS RecF,
Case
When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'D' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End AS RecD,
Case
When Commodities.ShippedReceivedId = 'S' And Commodities.ForeignDomesticId = 'D' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End AS Shipped,
Case
When Commodities.ShippedReceivedId = 'S' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End AS ShippedF,
Commodities.Destination,
Commodities.Origin,
Commodities.Specific,
CommodityCode.Descr,
Facility.FacilityID,
TransactionCharge.ArrivalDt,
TransactionCharge.DepartDt,
TransactionCharge.DtDocked,
TransactionCharge.DtSailed
FROM Vessel
INNER JOIN Commodities
INNER JOIN CommodityCode ON Commodities.CommodityCode = CommodityCode.CommodityCode
INNER JOIN TransactionCharge ON Commodities.TransactionID = TransactionCharge.TransactionID ON Vessel.VesselID = TransactionCharge.VesselId
INNER JOIN Facility ON TransactionCharge.FacilityID = Facility.FacilityID
INNER JOIN Unit ON Commodities.Unit = Unit.Unit
INNER JOIN TransactionFee ON TransactionCharge.TransactionID = TransactionFee.TransactionID
GROUP BY
CommodityCode.CommodityCode,
Facility.FacilityAcctNum,
Vessel.VesselType,
Facility.FacilityNm,
Commodities.Destination,
Commodities.Origin,
Commodities.Specific,
CommodityCode.Descr,
Facility.FacilityID,
Case
When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
Case
When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'D' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
Case
When Commodities.ShippedReceivedId = 'S' And Commodities.ForeignDomesticId = 'D' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
Case
When Commodities.ShippedReceivedId = 'S' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
TransactionCharge.ArrivalDt,
TransactionCharge.DepartDt,
TransactionCharge.DtDocked,
TransactionCharge.DtSailed
HAVING (Vessel.VesselType='V' Or Vessel.VesselType='O')
答案 0 :(得分:0)
您的{1}}中的案例陈述中有子查询。例如,查看案例陈述的第一部分:
GROUP BY
有两个开始,从When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
和Select BushelFactor...
开始,所以你必须删除它们。
答案 1 :(得分:0)
我快速重新格式化您的查询并删除/评论其中的一些问题:
SELECT --DISTINCT - do you really want DISTINCT?
cc.CommodityCode AS ComCode,
f.FacilityAcctNum,
v.VesselType,
f.FacilityNm,
CASE
WHEN Commodities.ShippedReceivedId = 'R' AND Commodities.ForeignDomesticId = 'F' THEN
CASE
WHEN Commodities.Amount IS NULL OR Commodities.Unit IS NULL THEN NULL
ELSE ROUND(Commodities.Amount /
CASE
WHEN Commodities.Unit = 'B' THEN cc.BushelFactor
ELSE u.ShortTonFactor
END, 2)
END
ELSE 0
END AS RecF,
CASE
WHEN Commodities.ShippedReceivedId = 'R' AND Commodities.ForeignDomesticId = 'D' THEN
CASE
WHEN Commodities.Amount IS NULL OR Commodities.Unit IS NULL THEN NULL
ELSE
ROUND(Commodities.Amount /
CASE
WHEN Commodities.Unit = 'B' THEN cc.BushelFactor
ELSE u.ShortTonFactor END, 2)
END
ELSE 0
END AS RecD,
CASE
WHEN Commodities.ShippedReceivedId = 'S' AND Commodities.ForeignDomesticId = 'D' THEN
CASE
WHEN Commodities.Amount IS NULL OR Commodities.Unit IS NULL THEN NULL
ELSE
ROUND(Commodities.Amount /
CASE
WHEN Commodities.Unit = 'B' THEN cc.BushelFactor
ELSE u.ShortTonFactor
END, 2)
END
ELSE 0
END AS Shipped,
CASE
WHEN Commodities.ShippedReceivedId = 'S' AND Commodities.ForeignDomesticId = 'F' THEN
CASE
WHEN Commodities.Amount IS NULL OR Commodities.Unit IS NULL THEN NULL
ELSE
ROUND(Commodities.Amount/
CASE
WHEN Commodities.Unit = 'B' THEN cc.BushelFactor
ELSE u.ShortTonFactor
END, 2)
END
ELSE 0
END AS ShippedF,
c.Destination,
c.Origin,
c.Specific,
cc.Descr,
f.FacilityID,
tc.ArrivalDt,
tc.DepartDt,
tc.DtDocked,
tc.DtSailed
FROM
Vessel v
INNER JOIN Commodities c ON --some_condtion, OR is this a cross join?
INNER JOIN CommodityCode cc ON c.CommodityCode = cc.CommodityCode
INNER JOIN TransactionCharge tc ON c.TransactionID = tc.TransactionID AND v.VesselID = tc.VesselId
INNER JOIN Facility f ON tc.FacilityID = f.FacilityID
INNER JOIN Unit u ON c.Unit = u.Unit
INNER JOIN TransactionFee td ON tc.TransactionID = tf.TransactionID
WHERE
v.VesselType IN ('V', 'O');
我不希望这会产生你想要的答案,但是现在逻辑有点简化可能会更容易发现原始查询的错误?