先日、SQL Server でクエリを作成していて、クエリに追加された列の合計を取得する必要がありました。
ここで、合計列を追加する必要があったクエリには、既に約 20 の選択された列がありました。
合計 (集計関数) を機能させるには、GROUP BY
事前に選択した列ごとに追加する必要がありました。
これは、非常に退屈で退屈な作業になりました (以下のクエリを参照してください)。1 つの列の SUM を機能させるためだけに、クエリに約 50 行の新しい行を追加する必要がありました。
SQL Server Management Studio を使用して、このようなクエリの作成を自動化する方法はありますか?
これは、他のデータベース (PL/SQL、Oracle、MySQL など) と同じですか?
これがクエリの外観です。
SELECT
mvLogisticContracts.[mvLogisticContract_id]
,mvLogisticContracts.[CONTRACTNUMBER]
,mvLogisticContracts.[CUSTNMBR]
,mvLogisticContracts.[DateCreated]
,mvLogisticContracts.[TruckAllocatedTotal] ,mvLogisticContracts.[mvLOgisticAddress_id_StartOriginal]
,mvLogisticContracts.[mvLogisticAddress_id_StartOriginal] ,mvLogisticContractLineItems.[LineItemQuantity] ,mvLogisticContractLineItems.[LineItemNo]
,mvLogisticContractLineItems.[OffloadRequired]
,mvLogisticContractLineItems.[CarrierRequested] ,mvLogisticContractLineItems.[RequestedSerialNo]
,mvLogisticContractLineItems.[mvLogisticAddress_id_Start]
,mvLogisticContractLineItems.[DateReadyBy] ,mvLogisticContractLineItems.[DateCustomerRequested],
mvLogisticContractLineItems.[mvLogisticContractLineItem_id]
,mvLogisticSalespersons.[FirstName],mvLogisticSalespersons.[LastName],
mvLogisticServiceTypes.ServiceType,
mvLogisticStatuses.Description,
mvLogisticSKUs.[SKU], mvLogisticSKUs.[Length],
a1.CITY as \"CityStart\", a1.AddressName as \"AddressNameStart\", a1.Address1 as \"Address1Start\", a1.STATE as \"StateStart\", a1.ZIP as \"ZipStart\",
a2.CITY as \"CityEnd\", a2.AddressName as \"AddressNameEnd\", a2.Address1 as \"Address1End\", a2.STATE as \"StateEnd\", a2.ZIP as \"ZipEnd\",
mvLogisticContracts.[mvLogisticAddress_id_Start] AS aa1,
mvLogisticContracts.[mvLogisticAddress_id_End] AS aa2,
SUM(mvLogisticReleases.ReleaseQuantity) as ReleaseQtySum
FROM mvLogisticContractLineItems
LEFT JOIN mvLogisticContracts ON mvLogisticContractLineItems.mvLogisticContract_id = mvLogisticContracts.mvLogisticContract_id
LEFT JOIN mvLogisticSalespersons ON mvLogisticContracts.[mvLogisticSalesperson_id] = mvLogisticSalespersons.[mvLogisticSalesperson_id]
LEFT JOIN mvLogisticServiceTypes ON mvLogisticContractLineItems.mvLogisticServiceType_id = mvLogisticServiceTypes.mvLogisticServiceType_id
LEFT JOIN mvLogisticStatuses ON mvLogisticContractLineItems.mvLogisticStatus_id = mvLogisticStatuses.mvLogisticStatus_id
LEFT JOIN mvLogisticSKUs ON mvLogisticContractLineItems.[SKU_id] = mvLogisticSKUs.[SKU_id]
LEFT JOIN mvLogisticAddresses a1 ON a1.[mvLogisticAddress_id] = mvLogisticContractLineItems.[mvLogisticAddress_id_Start]
LEFT JOIN mvLogisticAddresses a2 ON a2.[mvLogisticAddress_id] = mvLogisticContractLineItems.[mvLogisticAddress_id_End]
/*LEFT JOIN mvLogisticAddresses a1 ON a1.[mvLogisticAddress_id] = mvLogisticContracts.[mvLogisticAddress_id_Start]
LEFT JOIN mvLogisticAddresses a2 ON a2.[mvLogisticAddress_id] = mvLogisticContracts.[mvLogisticAddress_id_End]*/
LEFT JOIN mvLogisticReleases ON mvLogisticContractLineItems.mvLogisticContractLineItem_id = mvLogisticReleases.mvLogisticContractLineItem_id
GROUP BY
mvLogisticContracts.[mvLogisticContract_id]
,mvLogisticContracts.[CONTRACTNUMBER]
,mvLogisticContracts.[CUSTNMBR]
,mvLogisticContracts.[DateCreated]
,mvLogisticContracts.[TruckAllocatedTotal] ,mvLogisticContracts.[mvLOgisticAddress_id_StartOriginal]
,mvLogisticContracts.[mvLogisticAddress_id_StartOriginal] ,mvLogisticContractLineItems.[LineItemQuantity] ,mvLogisticContractLineItems.[LineItemNo]
,mvLogisticContractLineItems.[OffloadRequired]
,mvLogisticContractLineItems.[CarrierRequested] ,mvLogisticContractLineItems.[RequestedSerialNo]
,mvLogisticContractLineItems.[mvLogisticAddress_id_Start]
,mvLogisticContractLineItems.[DateReadyBy] ,mvLogisticContractLineItems.[DateCustomerRequested],
mvLogisticContractLineItems.[mvLogisticContractLineItem_id]
,mvLogisticSalespersons.[FirstName],mvLogisticSalespersons.[LastName],
mvLogisticServiceTypes.ServiceType,
mvLogisticStatuses.Description,
mvLogisticSKUs.[SKU],
mvLogisticSKUs.[Length],
a1.CITY, a1.AddressName, a1.Address1, a1.STATE, a1.ZIP,
a2.CITY, a2.AddressName, a2.Address1, a2.STATE, a2.ZIP,
mvLogisticContracts.[mvLogisticAddress_id_Start],
mvLogisticContracts.[mvLogisticAddress_id_End]