我正在尝试编写以下SQL Server查询,其中我还需要获取子表中引用的vehicleId
计数,
select
BV.[BaseVehicleId], BV.[MakeId], MK.[MakeName], BV.[ModelId],
MD.[ModelName], V.[VehicleId], V.[SubModelId], SubMD.[SubModelName],
V.[RegionId], V.PublicationStageId,
V.[LastUpdateDate], V.[InsertDate], V2BedCount, V2BodyCount,
V2BrakeCount, V2DriveCount, V2EngineCount,V2MfrCount, V2SpringCount,
V2SteeringCount, V2TransmissionCount, V2WheelCount
from
[dbo].[BaseVehicle] BV,
[dbo].[Make] MK,
[dbo].[Model] MD,
[dbo].[SubModel] SubMD,
[dbo].[VehicleToBedConfig] V2Bed,
[dbo].[VehicleToBodyStyleConfig] V2Body,
[dbo].[VehicleToBrakeConfig] V2Brake,
[dbo].[VehicleToDriveType] V2Drive,
[dbo].[VehicleToEngineConfig] V2Engine,
[dbo].[VehicleToMfrBodyCode] V2Mfr,
[dbo].[VehicleToSpringTypeConfig] V2Spring,
[dbo].[VehicleToSteeringConfig] V2Steering,
[dbo].[VehicleToTransmission] V2Transmission,
[dbo].[VehicleToWheelBase] V2Wheel,
[dbo].[Vehicle] V
where
V.[PublicationStageId] = '4'
and V.[DeleteDate] IS NULL
and BV.[BaseVehicleId] = V.[BaseVehicleId]
and MK.[MakeId] = BV.[MakeId]
and MD.[ModelId] = BV.[ModelId]
and V.[SubModelId] = SubMD.[SubModelId]
and V.[VehicleId] = V2Bed.[VehicleId]
and V.[VehicleId] = V2Body.[VehicleId]
and V.[VehicleId] = V2Brake.[VehicleId]
and V.[VehicleId] = V2Drive.[VehicleId]
and V.[VehicleId] = V2Engine.[VehicleId]
and V.[VehicleId] = V2Mfr.[VehicleId]
and V.[VehicleId] = V2Spring.[VehicleId]
and V.[VehicleId] = V2Steering.[VehicleId]
and V.[VehicleId] = V2Transmission.[VehicleId]
and V.[VehicleId] = V2Wheel.[VehicleId]
我正在寻找一种方法来推送上述查询中这些列的详细信息:
V2BedCount, V2BodyCount, V2BrakeCount,
V2DriveCount, V2EngineCount,
V2MfrCount, V2SpringCount, V2SteeringCount, V2TransmissionCount,
V2WheelCount
此处V2BedCount是使用VehicleToBedConfig表映射的车辆ID的计数,如
select COUNT(VehicleId) V2BedCount
from VehicleToBedConfig
group by VehicleId
请让我知道如何在第一个查询中插入第二个查询,让一个查询填充所有这些列的计数详细信息
V2BedCount, V2BodyCount, V2BrakeCount,
V2DriveCount, V2EngineCount,
V2MfrCount, V2SpringCount, V2SteeringCount, V2TransmissionCount,
V2WheelCount
答案 0 :(得分:0)
假设Id
是VehicleToBedConfig
尝试
COUNT(DISTINCT V2Bed.Id) OVER(PARTITION BY V.VehicleId) V2BedCount,
...
我建议您使用明确的JOIN语法。