|  | |  |
08-22-2004, 12:36 PM
|
#1 (permalink)
| | Registered User
Join Date: Aug 2004
Posts: 23
| Issues with Access Well couldnt find a Access forum so figured id' try here.
I'm trying to code a query for a client of mine, and everything works fine except for one field. The client watns to be able to show the previous years sales next to the current year sales. Anyways when I try to do so, it keeps gicving me a Syntax error/missing operator error. Now since I dont use Access everyday (thank god), i'm at a loss. Anyways here's the code: Code: SELECT monthsales.Unit, monthsales.Year, [Year]-1 AS LastYear, Sum(monthsales.GrossSales) AS ThisYearGrossSales, Sum(monthsales.GrossSales) WHERE monthsales.Year=monthsales.Year-1 AS LastYearGrossSales
FROM monthsales
GROUP BY monthsales.Unit, monthsales.Year, [Year]-1, [ThisYearGrossSales]-[ThisYearReturns], [ThisYearReturns]/[ThisYearGrossSales]*100, [ThisYearGrossSales]-[LastYearGrossSales]/[ThisYearGrossSales]*100
HAVING (((monthsales.Unit)=2019))
ORDER BY monthsales.Year DESC , [Year]-1 DESC; I've put in bold the area that is givign me grief. If anyone has any thoughs, I'd really appriciate it.
Last edited by creed; 08-23-2004 at 10:35 AM.
|
| |
08-22-2004, 04:26 PM
|
#2 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,175
| Have you tried parenthesis? |
| |
08-22-2004, 05:38 PM
|
#3 (permalink)
| | Registered User
Join Date: Aug 2004
Posts: 23
| Yes I have, still keeps givign me the same error message |
| |
08-23-2004, 06:28 AM
|
#4 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,175
| You've got me stumped then. I only have used Access briefly, and I rarely had to write my own queries in it. |
| |
08-23-2004, 07:14 AM
|
#5 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,532
| after looking at it some more, i have a couple questions:
1. "Sum(monthsales.GrossSales) AS ThisYearGrossSales," <-- how does that know to only select this year's gross sales?
2. can you really use the "where" clause in the select portion of a query?
3. if you want results showing by year, shouldn't you put that in the group clause first?
i wasn't aware that you could do all that logic within a sql statement.
i would have used this statement and then compared this year to last year int he programming language logic. Code: SELECT
monthsales.Unit,
monthsales.Year,
Sum(monthsales.GrossSales) AS YearGrossSales,
FROM monthsales
GROUP BY
monthsales.Year,
monthsales.Unit,
HAVING (((monthsales.Unit)=2019))
ORDER BY
monthsales.Year DESC i'd be really interested to know if you really can compute all that in the sql itself.
__________________ Mike |
| |
08-23-2004, 08:14 AM
|
#6 (permalink)
| | Registered User
Join Date: Aug 2004
Posts: 23
| Well I'll try to answer your Q's Quote: |
1. "Sum(monthsales.GrossSales) AS ThisYearGrossSales," <-- how does that know to only select this year's gross sales?
| Good question, I have checked it and it is only selecting the items valid for that table for the year that its' grouped into. Quote: |
2. can you really use the "where" clause in the select portion of a query?
| Probably not now that I think if it, if you want to keep adding items to the select statement. However that leaves the problem of how to make it select the items based on a previous year instead of the year that is in the column. Quote: |
3. if you want results showing by year, shouldn't you put that in the group clause first?
| Well I though that I had done that with GROUP BY monthsales.Year, but that only works fine if it's the same yer. For any calculations, I was always under the impression that you could have the database generate a temporary table, get the revelant information it needs, and throw it into the main table that you're working on. However thats' with other databases that I've used, this one seems to be very......I dont know hwo to describe it, but it's not flattering. |
| |
08-23-2004, 10:24 AM
|
#7 (permalink)
| | Registered User
Join Date: Aug 2004
Posts: 23
| Found it.
For anyone that is intrested in the answer, it was Code: DSum("GrossSales","monthsales","(monthsales.Year=" & monthsales.Year-1 & ") AND (monthsales.Unit=2019)") AS LastYearGrossSales Thanks all |
| |
08-23-2004, 10:55 AM
|
#8 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,532
| not sure that this will work for you, but check out the way i formatted the subquery for the year-1. that worked for me but with a completely different databse format. Code: SELECT
monthsales.Unit,
monthsales.Year,
[Year]-1 AS LastYear,
Sum(monthsales.GrossSales) AS ThisYearGrossSales,
(SELECT SUM(b.YEAR)
FROM monthsales AS b
WHERE a.YEAR = b.YEAR - 1) AS LastYearGrossSales
FROM monthsales as a
GROUP BY a.Unit, a.Year, [Year]-1, [ThisYearGrossSales]-[ThisYearReturns], [ThisYearReturns]/[ThisYearGrossSales]*100, [ThisYearGrossSales]-[LastYearGrossSales]/[ThisYearGrossSales]*100
HAVING (((a.Unit)=2019))
ORDER BY a.Year DESC , [Year]-1 DESC; anything past the group by line i'm not sure if it will work or not.
__________________ Mike |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 03:05 AM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |