是否可以使用不在 GROUP BY 中的 ORDER BY 列?
就像标题所说,这是我的代码:
SELECT
material,
SUM([Amount]) AS [Amount],
RIGHT(CONVERT(varchar(50), [date_in], 106), 8)
FROM
[rec_stats]
GROUP BY
material,
RIGHT(CONVERT(varchar(50), [date_in], 106), 8)
ORDER BY
material,date_in
由于 date_in,代码不会运行,有什么方法可以解决这个问题吗?
like the title said, here is my code:
SELECT
material,
SUM([Amount]) AS [Amount],
RIGHT(CONVERT(varchar(50), [date_in], 106), 8)
FROM
[rec_stats]
GROUP BY
material,
RIGHT(CONVERT(varchar(50), [date_in], 106), 8)
ORDER BY
material,date_in
the code wont run because of the date_in, is there anyway to get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
应用另一个聚合,怎么样;
Apply another aggregate, so how about;
按分组所依据的相同表达式进行排序。
最好根据月份的
DATE
表示进行分组和排序。在 SQL Server 2008 中,这将是:
Order by the same expression you're grouping by.
It is better to group and order on the
DATE
representation of the month.In
SQL Server 2008
that would be:使用这个
use this
您只能按 select 语句中可能使用的内容进行排序。 Date_in 本身在选择语句中是不可能的,因此您不能使用它。根据您的 RDBMS,您可以执行以下操作:
You can only order by what is possible to use in a select statement. Date_in by itself is not possible in the selection statement so you can't use it. Depending on your RDBMS you can do the following:
您是否想按月汇总?如果是这样,请尝试:
注意:您可能必须转换“MonthYearCombined”列中的值 - 我实际上建议您在代码而不是 SQL 中执行此操作,但希望包含对它的一些引用。
Are you trying to aggregate by month? If so, try:
Note: You may have to convert the values in the "MonthYearCombined" column - I would actually recommend you do that in code rather than SQL, but wanted to include some reference to it.
您无法像原始查询一样处理 date_in 列。相反,您可以嵌套查询并像这样阅读它:
我无法在我的开发环境中测试它,但我相信这给了您这个想法。
You cannot deal with the date_in column like your original query. Rather, you can NEST the query and read it like this:
I cannot test it in my dev environment but I believe this gives you the idea.