计数/分组
Count/ Group BY
我有以下查询:
SELECT
CONVERT(varchar(20),GetDate(),103) as 'Today''s Date'
,(D.Forename + ' ' + D.Surname) as 'Driver Name'
,V.Registration as 'Allocated Truck Reg'
,R.CollectionCompanyTown as 'Collection Location'
,R.DeliveryCompanyTown as 'Delivery Location'
,R.[Collection Date]
,R.[Delivery Date]
,R.[Fee Charge] as 'Revenue'
,R.Quantity
,R.ReservationsID
,C.[Trading Name] as 'Customer'
,Count([Customers Order Ref]) as count
,R.[Customers Order Ref]
FROM
tblReservation R
--LEFT JOIN tblDeliveryStages ds ON ds.ReservationsID = R.ReservationsID
LEFT JOIN tblDriver D ON D.DriverID = R.DriverID
LEFT JOIN tblVehicle V ON V.VehicleID = R.VehicleID
LEFT JOIN tblCustomer C ON C.CustomerID = R.CustomerID
LEFT JOIN tblReservationItems RI on RI.ReservationsID = r.ReservationsID and RI.Deleted != 1
LEFT JOIN tblManifestReservation MR ON MR.ReservationsID = R.ReservationsID
LEFT JOIN tblLoadManifest LM ON LM.LoadManifestID = MR .LoadManifestID
GROUP BY
R.[Customers Order Ref]
,R.ReservationsID
,D.Forename
,D.Surname
,V.Registration
,R.CollectionCompanyTown
,R.DeliveryCompanyTown
,R.[Collection Date]
,R.[Delivery Date]
,R.[Fee Charge]
,R.Quantity
,R.[Customers Order Ref]
,C.[Trading Name]
ORDER BY ReservationsID desc
两个主要部分是reservationsID 和Customers Order Ref。 ReservationsID 将始终是唯一的,客户订单编号不会,一些预订将共享相同的客户订单编号。
我正在尝试统计有多少预订共享客户订单编号,但当前计数字段只是 returns 以下内容:
您可以使用 COUNT()
的 windowed variant 并完全删除 GROUP BY
子句。类似于:
Count(*) OVER (PARTITION BY [Customers Order Ref])
如果我理解正确的话。
我有以下查询:
SELECT
CONVERT(varchar(20),GetDate(),103) as 'Today''s Date'
,(D.Forename + ' ' + D.Surname) as 'Driver Name'
,V.Registration as 'Allocated Truck Reg'
,R.CollectionCompanyTown as 'Collection Location'
,R.DeliveryCompanyTown as 'Delivery Location'
,R.[Collection Date]
,R.[Delivery Date]
,R.[Fee Charge] as 'Revenue'
,R.Quantity
,R.ReservationsID
,C.[Trading Name] as 'Customer'
,Count([Customers Order Ref]) as count
,R.[Customers Order Ref]
FROM
tblReservation R
--LEFT JOIN tblDeliveryStages ds ON ds.ReservationsID = R.ReservationsID
LEFT JOIN tblDriver D ON D.DriverID = R.DriverID
LEFT JOIN tblVehicle V ON V.VehicleID = R.VehicleID
LEFT JOIN tblCustomer C ON C.CustomerID = R.CustomerID
LEFT JOIN tblReservationItems RI on RI.ReservationsID = r.ReservationsID and RI.Deleted != 1
LEFT JOIN tblManifestReservation MR ON MR.ReservationsID = R.ReservationsID
LEFT JOIN tblLoadManifest LM ON LM.LoadManifestID = MR .LoadManifestID
GROUP BY
R.[Customers Order Ref]
,R.ReservationsID
,D.Forename
,D.Surname
,V.Registration
,R.CollectionCompanyTown
,R.DeliveryCompanyTown
,R.[Collection Date]
,R.[Delivery Date]
,R.[Fee Charge]
,R.Quantity
,R.[Customers Order Ref]
,C.[Trading Name]
ORDER BY ReservationsID desc
两个主要部分是reservationsID 和Customers Order Ref。 ReservationsID 将始终是唯一的,客户订单编号不会,一些预订将共享相同的客户订单编号。
我正在尝试统计有多少预订共享客户订单编号,但当前计数字段只是 returns 以下内容:
您可以使用 COUNT()
的 windowed variant 并完全删除 GROUP BY
子句。类似于:
Count(*) OVER (PARTITION BY [Customers Order Ref])
如果我理解正确的话。