简体   繁体   中英

SQL to GROUP BY calculated date

I need some assistance in grouping data by date via SQL but seem to be running into errors.

Here's my current SQL statement used as a variable passed into Excel VBA macro.

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) & _
""

The field MidnightStartDate is a date & time field in a US format. The MakeUSSQLDate procedure converts this into a UK format. I then want to remove any time values from the field, and have date only, and group by this field to give me a total of [Offered] for each of the [Name] and [Reporting] values for each date. I have used the convert function to strip out the times, and this currently works as below.

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) & _
""

However I can't seem to get the thing to group by the date. I have added GROUP BY CONVERT(varchar, [MidnightStartDate], 103) ORDER BY CONVERT(varchar, [MidnightStartDate], 103) to the end of the SQL statement in an attempt to solve this but only get error messages back.

I'm waiting for your error but...

If you use GROUP BY as follow:

convert(varchar, [MidnightStartDate], 103)

and you have in the SELECT fields these fields:

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

It's logic the error, because you must add all fields in your group by clause.

When you use a GROUP BY in the SELECTE field list you can have only:

  • Constants
  • Grouped fields
  • Aggregate functions

So you must extend your GROUP BY clause like the following:

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

Assuming your error is to do with not including your other fields or wrapping them in an aggregate function try:

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)" & _
""

Cleaning up with 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)" & _
""

Thanks for the help - my final working code

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];"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM