SQL到GROUP BY计算的日期

时间:2016-03-03 11:35:40

标签: sql-server vba excel-vba group-by access-vba

我需要一些帮助,通过SQL按日期对数据进行分组,但似乎遇到了错误。

这是我当前的SQL语句,用作传递给Excel VBA宏的变量。

SQLtext = "SELECT [Offered], [MidnightStartDate], tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]" & _
"FROM tblData_QueuePerformanceByPeriod INNER JOIN tblConfig_Queue on tblConfig_Queue.Pkey = tblData_QueuePerformanceByPeriod.FKQueue " & _
"Where [Offered]>0 AND[MidnightStartDate]>" & MakeUSSQLDate(Sheet13.Range("A1").Value) & " AND [MidnightStartDate]<" & MakeUSSQLDate(Sheet13.Range("A2").Value + 1) & _
""

字段MidnightStartDate是一个日期&amp;美国格式的时间字段。 MakeUSSQLDate过程将其转换为英国格式。然后,我想从字段中删除任何时间值,并且只有日期,并按此字段分组,以便为​​每个日期的每个[名称]和[报告]值提供[总计]。我已经使用了convert函数来去除时间,这个目前的工作原理如下。

SQLtext = "SELECT [Offered], convert(varchar, [MidnightStartDate], 103) AS [Date2], tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]" & _
"FROM tblData_QueuePerformanceByPeriod INNER JOIN tblConfig_Queue on tblConfig_Queue.Pkey = tblData_QueuePerformanceByPeriod.FKQueue " & _
"Where [Offered]>0 AND[MidnightStartDate]>" & MakeUSSQLDate(Sheet13.Range("A1").Value) & " AND [MidnightStartDate]<" & MakeUSSQLDate(Sheet13.Range("A2").Value + 1) & _
""

但是我似乎无法按日期分组。我已经在SQL语句的末尾添加了GROUP BY CONVERT(varchar, [MidnightStartDate], 103) ORDER BY CONVERT(varchar, [MidnightStartDate], 103)以尝试解决此问题但只返回错误消息。

3 个答案:

答案 0 :(得分:1)

我在等你的错误但是......

如果您使用GROUP BY如下:

convert(varchar, [MidnightStartDate], 103)

你在SELECT字段中有这些字段:

[Offered], convert(varchar, [MidnightStartDate], 103),
tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]

这是错误的逻辑,因为您必须在group by子句中添加所有字段。

在SELECTE字段列表中使用GROUP BY时,您只能拥有:

  • 常量
  • 分组字段
  • 汇总功能

因此,您必须扩展GROUP BY子句,如下所示:

GROUP BY [Offered], convert(varchar, [MidnightStartDate], 103) AS [Date2],
tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]

答案 1 :(得分:1)

假设您的错误与不包括其他字段或将它们包装在聚合函数中有关,请尝试:

SQLtext = "SELECT [Offered], convert(varchar, [MidnightStartDate], 103) AS [Date2], tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]" & _
"FROM tblData_QueuePerformanceByPeriod INNER JOIN tblConfig_Queue ON tblConfig_Queue.Pkey = tblData_QueuePerformanceByPeriod.FKQueue " & _
"WHERE [Offered]>0 AND [MidnightStartDate]>" & MakeUSSQLDate(Sheet13.Range("A1").Value) & " AND [MidnightStartDate]<" & MakeUSSQLDate(Sheet13.Range("A2").Value + 1) & _
"GROUP BY convert(varchar, [MidnightStartDate], 103), [Offered], tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]" & _
"ORDER BY convert(varchar, [MidnightStartDate], 103)" & _
""

使用BETWEEN

进行清理
SQLtext = "SELECT [Offered], convert(varchar, [MidnightStartDate], 103) AS [Date2], tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]" & _
"FROM tblData_QueuePerformanceByPeriod INNER JOIN tblConfig_Queue ON tblConfig_Queue.Pkey = tblData_QueuePerformanceByPeriod.FKQueue " & _
"WHERE [Offered]>0 AND [MidnightStartDate] BETWEEN" & MakeUSSQLDate(Sheet13.Range("A1").Value) & " AND " & MakeUSSQLDate(Sheet13.Range("A2").Value + 1) & _
"GROUP BY convert(varchar, [MidnightStartDate], 103), [Offered], tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]" & _
"ORDER BY convert(varchar, [MidnightStartDate], 103)" & _
""

答案 2 :(得分:1)

感谢您的帮助 - 我的最终工作代码

SQLtext = "SELECT SUM([Offered]), CAST([MidnightStartDate] AS INT), tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]" & _
"FROM tblData_QueuePerformanceByPeriod INNER JOIN tblConfig_Queue on tblConfig_Queue.Pkey = tblData_QueuePerformanceByPeriod.FKQueue " & _
"Where [Offered]>0 AND[MidnightStartDate]>" & MakeUSSQLDate(Sheet13.Range("A1").Value) & " AND [MidnightStartDate]<" & MakeUSSQLDate(Sheet13.Range("A2").Value + 1) & _
"GROUP BY CAST([MidnightStartDate] AS INT), tblConfig_Queue.[Name], tblConfig_Queue.[Reporting]" & _
"ORDER BY CAST([MidnightStartDate] AS INT), tblConfig_Queue.[Reporting];"