SELECT CaptainID, MONTH(TripStart) AS TripMonth,COUNT(LaunchID) AS TripCnt
FROM slTrip
GROUP BY CaptainID, TripStart, LaunchID
ORDER BY CaptainID, TripMonth, TripCnt ASC
我需要三列,其中列出了CaptainID,TripMonth是通过TripStart上的MONTH()函数获得的,TripCnt应该计算每个CaptainID在特定月份的行程数。
答案 0 :(得分:0)
我想你打算这样:
SELECT CaptainID, MONTH(TripStart) AS TripMonth, COUNT(LaunchID) AS TripCnt
FROM slTrip
GROUP BY CaptainID, MONTH(TripStart)
ORDER BY CaptainID, TripMonth, TripCnt ASC;
GROUP BY
应仅包含SELECT
中的非聚合列。许多数据库不允许GROUP BY
中的列别名。他们确实允许他们使用ORDER BY
。