Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-22-2004, 12:36 PM   #1 (permalink)
creed
Registered User
 
Join Date: Aug 2004
Posts: 23
creed is on a distinguished road
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.
creed is offline   Reply With Quote
Old 08-22-2004, 04:26 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Have you tried parenthesis?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-22-2004, 05:38 PM   #3 (permalink)
creed
Registered User
 
Join Date: Aug 2004
Posts: 23
creed is on a distinguished road
Yes I have, still keeps givign me the same error message
creed is offline   Reply With Quote
Old 08-23-2004, 06:28 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
You've got me stumped then. I only have used Access briefly, and I rarely had to write my own queries in it.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-23-2004, 07:14 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-23-2004, 08:14 AM   #6 (permalink)
creed
Registered User
 
Join Date: Aug 2004
Posts: 23
creed is on a distinguished road
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.
creed is offline   Reply With Quote
Old 08-23-2004, 10:24 AM   #7 (permalink)
creed
Registered User
 
Join Date: Aug 2004
Posts: 23
creed is on a distinguished road
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
creed is offline   Reply With Quote
Old 08-23-2004, 10:55 AM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
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
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating temporary tables in Access creed Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 0 08-23-2004 03:17 PM
MySQL vs MS Access Epsilon PHP 5 05-24-2004 03:28 PM
Access denied for user: '@192.168.0.71' (Using password: NO) infinite_root PHP 11 04-28-2004 06:30 PM
can't access bios sde Windows 4 08-10-2003 09:41 PM


All times are GMT -8. The time now is 03:05 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting