MSSQL Groupby排序依据

时间:2016-01-20 23:57:54

标签: sql-server

我想编写一个返回月份名称并计算该月份的查询。

    SELECT   DateName( month , DateAdd( month , MONTH(AE.BookingTimeStamp ) , -1 ))   monthnames, count (BookingTimeStamp)  totCount
FROM BookingsAEItems AE  WHERE BookingTimeStamp BETWEEN '1/12/2015' AND '1/12/2016' group by monthnames   order by totCount

我正在

  

无效的列名'monthnames'。

这是一个MSSQL查询。 我想念的是什么!!!

2 个答案:

答案 0 :(得分:1)

您不能在merge(file1["name"], file2, by="name", sort=FALSE) # name id #1 c two1 #2 a two2 #3 b two3 子句的SELECT子句中使用列别名。你必须重复整个表达式:

GROUP BY

答案 1 :(得分:0)

感谢您的反馈......我通过在评论中给出内部查询来实现它

 SELECT Months.monthnames, count(BookingTimeStamp)
    FROM BookingsAEItems AE, (SELECT distinct DateName(month, DateAdd(month, MONTH(AE.BookingTimeStamp ), -1 )) 'monthnames'
    FROM BookingsAEItems AE  WHERE BookingTimeStamp BETWEEN '1/10/2015' AND '12/12/2016') Months
    WHERE AE.BookingTimeStamp BETWEEN '1/10/2015' AND '12/12/2016' 
    And DateName(month, DateAdd(month, MONTH(AE.BookingTimeStamp ), -1 )) = Months.monthnames
    group by Months.monthnames