我正在使用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()
正常工作?
答案 0 :(得分:3)
strftime
是mysql函数,在Microsoft的sql-server中不可用。
对于这个简单的用例(从日期中提取一个月),您可以使用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);