我有3個表:
Corridors (CorridorID int Primary)
Locations(LocationID int Primary, LocationMax int, CorridorID int)
Inventory(Inventory ID, SerialNo varchar)
我想這樣的查詢:
select C.ID, Count(Distinct(SerialNo)), Sum(L.Max) as TotalCapacity from Corridors as C
Inner Join Locations L on L.CorridorID=C.ID
Inner Join Inventory I on I.LocationID=L.ID
Group By C.ID
但總得到LocationMax超過一次
我的真實結果是(30 + 40 + 10 + 20)我是指每個地點的一次性總和。 ©它有可能做兩次「分組」?我怎樣才能得到這樣的結果? (30 + 40 + 10 + 20)
你可以使用'MIN (L.LocationMax)OVER(PARTITION BY LocationID)過濾器的任何可能的重複' –