Group By problem (1 Viewer)

wgma

Registered User.
Local time
Today, 09:44
Joined
Nov 19, 2007
Messages
72
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

Wino Moderator
Staff member
Local time
Today, 07:44
Joined
Aug 30, 2003
Messages
36,132
Try grouping by the expression rather than the alias:

MID(WGMAHIST_TIMEDTY.TDYTDT,1,4)
 

boblarson

Smeghead
Local time
Today, 07:44
Joined
Jan 12, 2001
Messages
32,059
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)
 

wgma

Registered User.
Local time
Today, 09:44
Joined
Nov 19, 2007
Messages
72
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

Smeghead
Local time
Today, 07:44
Joined
Jan 12, 2001
Messages
32,059
Try using the query without the grouping and then add that into a new query and do the grouping there.
 

Users who are viewing this thread

Top Bottom