我想将booking_class_rcd
的数量记为NoOfPax
,将FareAmount
的总数记为Total Fare
,将commission_amount
的总数记为TotalCommission
特定的航班日期。我找不到正确的查询。
Booking_Class_rcd
– 'N'
,'B'
,'C'
,'D'
Select agency_code,departure_date, booking_class_rcd, COUNT(booking_class_rcd)
AS NoOfPax,origin_rcd, destination_rcd,SUM (fare_amount) as TotalFare,Sum(commission_amount) as TotalCommissionAmount
from passenger_segment_mapping
WHERE agency_code=’TEST’
Group By booking_class_rcd
Order by departure_date desc
我的结果看起来像
AgencyCode TEST Departure Date Booking_Class_rcd NoOfPax, origin, destination, TotalFareAmount TotalCommissionAmount
TEST 2018-06-03 B 10 KTM PKR 30000 3000
TEST 201806-03 C 20 KTM PKR 20000 2000
答案 0 :(得分:0)
您似乎正在使用允许非标准group by
查询的MySQL。 SQL标准要求所有“非聚合”列均用于定义group by
子句中的组。
SELECT
/* non-aggregating columns */
agency_code
, departure_date
, booking_class_rcd
, origin_rcd
, destination_rcd
/* aggregating columns */
, COUNT(booking_class_rcd) AS noofpax
, SUM(fare_amount) AS totalfare
, SUM(commission_amount) AS totalcommissionamount
FROM passenger_segment_mapping
WHERE agency_code = 'TEST'
GROUP BY
/* repeat the non-aggregating columns here (without any column aliases) */
agency_code
, departure_date
, booking_class_rcd
, origin_rcd
, destination_rcd
ORDER BY
departure_date DESC
非聚合列是不使用聚合函数(例如sum()min()max()count()等)的任何列。
答案 1 :(得分:0)
在group by中添加所有聚合和非聚集字段,我得到了解决方案。
Select departure_date,agency_code, origin_rcd +'-'+destination_rcd as Sector,booking_class_rcd, count(departure_date) AS NoOfPax, SUM(fare_amount) as TotalFare, SUM(commission_amount) as TotalCommission
from passenger_segment_mapping
WHERE agency_code='TEST'
Group BY departure_date, agency_code, origin_rcd, passenger_check_in_status_rcd, destination_rcd,booking_class_rcd, fare_amount, commission_amount
ORDER BY departure_date asc