Sql 在按月分组日期时,如何将月份从INT转换为VARCHAR
我需要以可读的方式总结历年来每个月的采访次数Sql 在按月分组日期时,如何将月份从INT转换为VARCHAR,sql,sql-server,group-by,date-conversion,sql-date-functions,Sql,Sql Server,Group By,Date Conversion,Sql Date Functions,我需要以可读的方式总结历年来每个月的采访次数 SELECT month(i.Date) AS Month, Year(i.Date) AS Year, Count(i.Id) AS' Number of Interviews' FROM Interviews i GROUP BY month(i.Date), year(i.Date) ORDER BY year(i.Date) DESC, month(Date) ASC 我希望查询以VARCHAR形式返回Month,比如'一月',而不是'Mo
SELECT month(i.Date) AS Month, Year(i.Date) AS Year, Count(i.Id) AS' Number of Interviews'
FROM Interviews i
GROUP BY month(i.Date), year(i.Date)
ORDER BY year(i.Date) DESC, month(Date) ASC
我希望查询以VARCHAR形式返回Month,比如'一月',而不是'Month'函数中的默认INT。一个解决方案,适用于大多数DBMS,是一个
CASE
表达式
SELECT CASE month(i.date)
WHEN 1 THEN
'January'
...
WHEN 12 THEN
'December'
END month,
year(i.date) year,
count(i.id) number_of_interviews
FROM interviews i
GROUP BY month(i.date),
year(i.date)
ORDER BY year(i.date) DESC,
month(i.date) ASC;
对于MySql,它将是:
SELECT
monthname(i.Date) AS Month,
Year(i.Date) AS Year,
Count(i.Id) AS `Number of Interviews`
FROM Interviews i
GROUP BY month(i.Date), monthname(i.Date), year(i.Date)
ORDER BY year(i.Date) DESC, month(i.Date) ASC
对于SQL Server:
SELECT
datename(month, i.date) AS Month,
Year(i.Date) AS Year,
Count(i.Id) AS [Number of Interviews]
FROM Interviews i
GROUP BY month(i.Date), datename(month, i.date), year(i.Date)
ORDER BY year(i.Date) DESC, month(i.Date) ASC
SELECT count(1), format(dateColumn, 'yyyy-MM')
FROM tableName
GROUP BY format(dateColumn, 'yyyy-MM')
ORDER BY 2