是否可以使用不在 GROUP BY 中的 ORDER BY 列?

发布于 2024-11-04 07:51:32 字数 303 浏览 2 评论 0原文

就像标题所说,这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦里兽 2024-11-11 07:51:32

应用另一个聚合,怎么样;

order by min([date_in])

Apply another aggregate, so how about;

order by min([date_in])
妖妓 2024-11-11 07:51:32

按分组所依据的相同表达式进行排序。

最好根据月份的 DATE 表示进行分组和排序。

在 SQL Server 2008 中,这将是:

SELECT  material, SUM([Amount]) AS [Amount], DATEADD(d, 1 - DAY(GETDATE()), CAST(GETDATE() AS DATE))
FROM    [rec_stats]
GROUP BY
        material,
        DATEADD(d, 1 - DAY(GETDATE()), CAST(GETDATE() AS DATE))
ORDER BY
        material, DATEADD(d, 1 - DAY(GETDATE()), CAST(GETDATE() AS DATE))

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:

SELECT  material, SUM([Amount]) AS [Amount], DATEADD(d, 1 - DAY(GETDATE()), CAST(GETDATE() AS DATE))
FROM    [rec_stats]
GROUP BY
        material,
        DATEADD(d, 1 - DAY(GETDATE()), CAST(GETDATE() AS DATE))
ORDER BY
        material, DATEADD(d, 1 - DAY(GETDATE()), CAST(GETDATE() AS DATE))
喜你已久 2024-11-11 07:51:32

使用这个

排序依据
1,2,3,4,5

use this

order by
1,2,3,4,5
倦话 2024-11-11 07:51:32

您只能按 select 语句中可能使用的内容进行排序。 Date_in 本身在选择语句中是不可能的,因此您不能使用它。根据您的 RDBMS,您可以执行以下操作:

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,RIGHT(CONVERT(varchar(50), [date_in], 106), 8)

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:

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,RIGHT(CONVERT(varchar(50), [date_in], 106), 8)
瑕疵 2024-11-11 07:51:32

您是否想按月汇总?如果是这样,请尝试:

SELECT
  material,
  SUM([Amount]) AS [Amount],
  DATEPART(year, [date_in]) as y,
  DATEPART(month, [date_in]) as m,
  DATEPART(month, [date_in]) + ' ' + DATEPART(year, [date_in]) AS MonthYearCombined
FROM
  [rec_stats]
GROUP BY
  material,
  DATEPART(year, [date_in]),
  DATEPART(month, [date_in])
ORDER BY
  material,y,m

注意:您可能必须转换“MonthYearCombined”列中的值 - 我实际上建议您在代码而不是 SQL 中执行此操作,但希望包含对它的一些引用。

Are you trying to aggregate by month? If so, try:

SELECT
  material,
  SUM([Amount]) AS [Amount],
  DATEPART(year, [date_in]) as y,
  DATEPART(month, [date_in]) as m,
  DATEPART(month, [date_in]) + ' ' + DATEPART(year, [date_in]) AS MonthYearCombined
FROM
  [rec_stats]
GROUP BY
  material,
  DATEPART(year, [date_in]),
  DATEPART(month, [date_in])
ORDER BY
  material,y,m

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.

成熟稳重的好男人 2024-11-11 07:51:32

您无法像原始查询一样处理 date_in 列。相反,您可以嵌套查询并像这样阅读它:

SELECT
  material,
  SUM([Amount]) AS [Amount],
  RIGHT(CONVERT(varchar(50), [date_in], 106), 8)
FROM
(
Select material, Amount, RIGHT(CONVERT(varchar(50), [date_in], 106), 8) as DateIn
From [rec_stats]
) X
GROUP BY
  material,
  DateIn
ORDER BY
  material,DateIn

我无法在我的开发环境中测试它,但我相信这给了您这个想法。

You cannot deal with the date_in column like your original query. Rather, you can NEST the query and read it like this:

SELECT
  material,
  SUM([Amount]) AS [Amount],
  RIGHT(CONVERT(varchar(50), [date_in], 106), 8)
FROM
(
Select material, Amount, RIGHT(CONVERT(varchar(50), [date_in], 106), 8) as DateIn
From [rec_stats]
) X
GROUP BY
  material,
  DateIn
ORDER BY
  material,DateIn

I cannot test it in my dev environment but I believe this gives you the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文