[英]How to Convert a Month from INT to VARCHAR when grouping the date by MONTH
我需要以一种可读的方式总结多年来所有月份中每个月的采访次数。
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形式返回“月”,而不是“月”函数的默认INT。
对于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
适用于大多数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;
SELECT count(1), format(dateColumn, 'yyyy-MM')
FROM tableName
GROUP BY format(dateColumn, 'yyyy-MM')
ORDER BY 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.