473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Selecting variables automatically in GROUP BY query

Hi All:

I'm using Access 2003 on Windows XP.

I'm creating a table where i'll be adding a new variable every week (based on the week number). This is a numeric variable. Once the variable is inserted, i need to run a GROUP BY query based on other character variables. For instance, i'm having a table as:

EMP_ID
Sales_week1
Sales_week2
Sales_week3,

my SQL query will be,
Expand|Select|Wrap|Line Numbers
  1. SELECT EMP_ID, SUM(sales_week1) as week1, SUM(sales_week2) as week2, SUM(sales_week3) as week3
  2. FROM table1
  3. group by EMP_ID;
next week, when i add another variable Sales_week4. The query should update to:
Expand|Select|Wrap|Line Numbers
  1. SELECT EMP_ID, SUM(sales_week1) as week1, SUM(sales_week2) as week2, SUM(sales_week3) as week3, SUM(sales_week4) as week4
  2. FROM table1
  3. group by EMP_ID;
I'm not sure how to automate this in Access.

Any help on this is appreciated.


Regards,
-Deepak
Sep 7 '09 #1

✓ answered by NeoPa

** WARNING - This is a very bad idea **

Never say I didn't warn you. Even when things get horribly messy for you down the line.

Having said that, it is possible.

QueryDefs have a .SQL property which can be accessed via VBA code in your database. They can even be updated in the same way (using the .SQL property). A SQL string can be run as a query (a limitation is that only action queries can be run this way) by using :
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL {SQL string value}
For a SELECT query you would need to update the QueryDef before running it.

5 4058
beacon
579 512MB
I'm just curious if there's a reason why you wouldn't include another field in the table for the date at the beginning of the week instead of applying the literal "Week 1" to each week's numbers.

I'm also not sure whether your setup is ideal for what you're trying to do. Is EMP_ID the name of the table? If so, why not call this the SALES_BY_WEEK table? Then your fields would be:

Emp_ID | Sales | Week
0001 $2,000 03/01/2009
0002 $3,000 03/01/2009
0001 $1,000 03/08/2009

Then, if you really wanted to have "Week 1", you could write an expression for a field in your query that recognizes 03/01/2009 as "Week 1" and then increments the number for each subsequent week.

I could be way off from what you are wanting, so forgive me if it's not.
Sep 7 '09 #2
NeoPa
32,556 Expert Mod 16PB
The difficulty you are experiencing is a good illustration of why the approach you're taking is never advised.

I suggest you consider redesigning the data organisation of you database. Here's a very useful link to help (Normalisation and Table structures).
Sep 7 '09 #3
To re-structure the table will be a problem as there are more 150,000 records in the table. So, if I add a variable as week and start appending the table, the size of the file will be really huge.

I was thinking if i could create a string using some kind of macros, the string would basically contain the SQL query and somehow run this string as query.

Is it possible??
Sep 8 '09 #4
NeoPa
32,556 Expert Mod 16PB
** WARNING - This is a very bad idea **

Never say I didn't warn you. Even when things get horribly messy for you down the line.

Having said that, it is possible.

QueryDefs have a .SQL property which can be accessed via VBA code in your database. They can even be updated in the same way (using the .SQL property). A SQL string can be run as a query (a limitation is that only action queries can be run this way) by using :
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL {SQL string value}
For a SELECT query you would need to update the QueryDef before running it.
Sep 8 '09 #5
FishVal
2,653 Expert 2GB
@deepakkumars
  • Records count doesn't matter.
  • File size will be of the same if not a smaller size.
  • If you could create "some kind of macros", then you certainly could create "some kind of queries" to transfer existing data to a normal table schema.
  • ??????
  • PROFIT!!!
Sep 8 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: sks | last post by:
Hi, I have a table which logs for sake of argument page hits: id, timestamp, ipaddress Now, I'm running a query to get an activity report and I want to group together all the hits from a...
3
by: chfran | last post by:
I have a form that is my criteria form. So, if a person enters a State into the State combobox (ex. UT) then everything from that State will appear when the user runs the query (based on the...
4
by: Iwan Petrow | last post by:
Hi, Are there any options to set local variables in methods automatically (in Visual Studio 2003)? Example: public void Method(){ int i; //I want i=0 ArrayList a; //I want a=null
2
by: Alex Scollay | last post by:
Let's say I have a column whose values are all 2-digit integers, e.g. 82 (though it's actually a varchar field). From now on, the column will be able to have 2-digit as well as 3-digit integers....
1
by: michael.martinek | last post by:
Greetings! I've recently been trying to do something, which apparently looks like it may be a little odd.. I'm not finding anything in the manuals or anywhere on the web where something similiar...
19
MSeda
by: MSeda | last post by:
I would like to use a Global variable as a criteria in an access query. in my first attempt to do so I wrote a Function that looks like this Public Function GloVar(ByVal VarName) As String ...
6
by: nbomike | last post by:
I am trying to integrate a custom page into a shopping cart app written in PHP. The custom page is basically a part finder widget that helps customers find a car part based on the car's make, model,...
4
by: daanoz | last post by:
hey I'm trying to make a query which selects all rows that are not linked to the a certain number, i already managed to do this with a subquery, but this slows down the application, mainly caused...
2
by: JaketheSnake27 | last post by:
I'm trying to define a field in a query using an expression. Part of that expression involves using public variables. First of all, is it possible to use variables in a query field? And if it is,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.