麻烦正确地排序列表

时间:2017-01-27 01:47:03

标签: sql

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在特定月份的行程数。

1 个答案:

答案 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