View Full Version : Group By problem


wgma
08-03-2009, 02:07 PM
I need to run the query below:

ELECT WGMAHIST_TIMEDTY.TDYPT, MID(WGMAHIST_TIMEDTY.TDYTDT,1,4) AS Year, SUM(WGMAHIST_TIMEDTY.TDYFND) AS TotHrs
FROM WGMAHIST_TIMEDTY
Where WGMAHIST_TIMEDTY.TDYTDT Between 19981001 AND 20090930
Group By WGMAHIST_TIMEDTY.TDYPT, Year

But I am having a problem with the term Year in the group by clause. How do I group by just the year portion of the date field which is yyyymmdd?

Thanks.

pbaldy
08-03-2009, 02:14 PM
Try grouping by the expression rather than the alias:

MID(WGMAHIST_TIMEDTY.TDYTDT,1,4)

boblarson
08-03-2009, 02:15 PM
1. do not use YEAR as your column name as it is an Access Reserved Word.

2. You would use:

ELECT WGMAHIST_TIMEDTY.TDYPT, MID(WGMAHIST_TIMEDTY.TDYTDT,1,4) AS MyYear, SUM(WGMAHIST_TIMEDTY.TDYFND) AS TotHrs
FROM WGMAHIST_TIMEDTY
Where WGMAHIST_TIMEDTY.TDYTDT Between 19981001 AND 20090930
Group By WGMAHIST_TIMEDTY.TDYPT, MID(WGMAHIST_TIMEDTY.TDYTDT,1,4)

boblarson
08-03-2009, 02:16 PM
Try grouping by the expression rather than the alias:

MID(WGMAHIST_TIMEDTY.TDYTDT,1,4)

too quick for me. :D :p :D

wgma
08-03-2009, 02:19 PM
1. do not use YEAR as your column name as it is an Access Reserved Word.

2. You would use:

ELECT WGMAHIST_TIMEDTY.TDYPT, MID(WGMAHIST_TIMEDTY.TDYTDT,1,4) AS MyYear, SUM(WGMAHIST_TIMEDTY.TDYFND) AS TotHrs
FROM WGMAHIST_TIMEDTY
Where WGMAHIST_TIMEDTY.TDYTDT Between 19981001 AND 20090930
Group By WGMAHIST_TIMEDTY.TDYPT, MID(WGMAHIST_TIMEDTY.TDYTDT,1,4)

I have tried putting the expression into the group by but I get an ODBC error.

ODBC--call failed.
Argument 1 of function SUBSTRING not valid. (#-171)

Any suggestions?

boblarson
08-03-2009, 02:29 PM
Does the query run without the group by?

wgma
08-04-2009, 05:17 AM
Does the query run without the group by?

Yes, it runs.

boblarson
08-04-2009, 09:35 AM
Try using the query without the grouping and then add that into a new query and do the grouping there.