'strftime'不是公认的内置函数名称

时间:2018-08-14 15:45:07

标签: sql select strftime database-management

我正在使用Microsoft SQL Database Management Studio,它将不允许我使用strftime()函数来运行查询。我必须按月创建一个表,每个月都有新用户和退订用户。

这基本上就是造成错误的原因:

SELECT strftime('%m', createddate) AS 'Month', COUNT(createddate) AS 'Subscribers',
       COUNT(dateunsubscribed) AS 'UNsubscribers'
FROM subscriber
GROUP BY 1
ORDER BY 1;

在没有strftime()的情况下如何运行此查询,或者如何使strftime()正常工作?

1 个答案:

答案 0 :(得分:3)

strftime函数,在Microsoft的中不可用。

对于这个简单的用例(从日期中提取一个月),您可以使用month

SELECT   MONTH(createddate) AS [Month], 
         COUNT(createddate) AS [Subscribers],
         COUNT(dateunsubscribed) AS [UNsubscribers]
FROM     subscriber
GROUP BY 1
ORDER BY 1;

编辑:
为了解决注释中的问题,group by子句不像order by子句那样使用序数。您需要指定要分组的表达式:

SELECT   MONTH(createddate) AS [Month], 
         COUNT(createddate) AS [Subscribers],
         COUNT(dateunsubscribed) AS [UNsubscribers]
FROM     subscriber
GROUP BY 1
ORDER BY MONTH(createddate);