SQL 按年份分组
这是我的查询。
SELECT CONVERT(varchar, cast(date as datetime), 3)
FROM shoptransfer
GROUP BY year (date)
我想按日期 (varchar
) 列的年份部分进行分组,但是出现以下错误:
列“shoptransfer.Date”在选择列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中。
如何按日期列的年份部分进行分组?
This is my query.
SELECT CONVERT(varchar, cast(date as datetime), 3)
FROM shoptransfer
GROUP BY year (date)
I want to group by the year part of the date (varchar
) column, however I get the following error:
Column 'shoptransfer.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How do I group by the year part of the date column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
怎么样:
或者:
这是基于OP的命令:“我想按日期(varchar)列的年份部分进行分组”
How about:
Or:
This is based on OP's command: "I want to group by year part of date (varchar) column"
使用 MariaDB:
With MariaDB:
如果您想同时选择日期和年份,则不应使用 GROUP BY 子句,因为它将所有具有相似年份的行合并为一行。如果您想要将所有具有相似年份的日期放在一起,您可以使用 ORDER BY:
OR
If you want to select both the date and the year you should not use a GROUP BY clause as it combines all of the rows with similar years into one row. If you want all of the dates with similar years together you can use an ORDER BY:
OR
我不了解 T-SQL,但在一般 SQL 中,group by 子句中的内容必须与 select 子句中的每个非聚合函数列完全匹配。 也尝试
一下,
其中 SUBSTRING(productcode, 5, 3) 像 '%'
没有过滤掉太多 - 也许将其删除。I don't know about T-SQL, but in SQL in general, what is in the group by clause must exactly match each non-aggregate function column in the select clause. Try
also,
where SUBSTRING(productcode, 5, 3) like '%'
is not filtering out much - maybe remove it.您应该将
shoptransfer.Date
列添加到 group by 子句中。You should add column
shoptransfer.Date
into group by clause.年份
2004年
2005年
2006年
year
2004
2005
2006