Hy! I'm a newbie in access and I'm trying to do a small linguistic analysis and I'm having problems with append and aggregate sub-query.
In a nutshell, I'm trying to hit tree rabbits with one stone.
I'm trying to make one query which is first an aggregate query with an append sub-query inside it. But this last append sub-query has another aggregate query inside it (triple nested queries! yeaks!)
Basically I have one database with one preexistent table that has just 4 fields (columns) (it is normalized). Never changing, always the same.
Code:
word|nrbooks|freq|distrib
Home|3|542|1626
Table|2|230|460
Garden|1|712|712
Chair|2|380|760
You read the table like this: the word 'home' is found in 3 books with a TOTAL freq of 542 hits in all 3 books, and the distrib field is a simple product of these two previous fields 3*542=1626. And so on. I need the last field 'distrib' so that for example the word garden, which has the greatest freq doesn't show up in my descending sorting as the first, because it is only found in one book, so when I do a descending sorting by "distrib" field, my table will look like this:
Code:
word|nrbooks|freq|distrib
Home|3|542|1626
Chair|2|380|760
Garden|1|712|712
Table|2|230|460
So, you've guessed it right, 'distrib' comes from how much that particular word is well distributed in books.
Step 1:
I import new data (a new book) from a text file which has only 1 field. It looks like this:
Grass
Chair
Fork
Knife
Grass
Spoon
Grass
Note that the word 'chair' is already in my database, and that the word 'grass' repeats 3 times.
Step 2: I aggregate the data from this one column data text file, and the duplicate entries show up in a new field called 'freq' like this:
Code:
Word|freq
Chair|1
Fork |1
Grass|3
Knife|1
Spoon|1
Step 3: I append this data to my database table.
But wait! I think I need to fill the second column(field) 'nrbooks' with value 1, so that when I do a aggregate query I sum up the entire 'nrbooks' field. I hope I'm doing it right. Please tell me if I'm wrong.
Code:
word|nrbooks|freq|distrib
Home|3|542|1626
Chair|2|380|760
Garden|1|712|712
Table|2|230|460
Chair|1|1|1
Fork|1|1|1
Grass|1|3|3
Knife|1|1|1
Spoon|1|1|1
Step 4:
As you have noticed the entry 'chair' is repeated, so I have to make another aggregate query that gives me my final table like this:
Code:
word|nrbooks|freq|distrib
Home|3|542|1626
Chair|3|381|1143
Garden|1|712|712
Table|2|230|460
Grass|1|3|3
Fork|1|1|1
Knife|1|1|1
Spoon|1|1|1
Note the entry 'chair' now appears in 3 books! It's freq changed from 380 to 381 (it was found only once in this last book) and the distribution value ('distrib') took a huge leap from 760 to 1143(3*381)
Why do I really need such a complicated triple nested aggregate-append-aggregate query? 
Well, if I do it by hand, one query at a time, with each new imported data, my final aggregate query should be a new make table query into another new table in the database, since the source and destination can't be the same. 
Then at the next import my destination will be this newly created table, then again I make the final aggregate to another make table so I constantly have to change my destination, and have 2 queries for each of these two tables.
So I have to bounce back and forth between 2 tables in my database.
But if I properly create this triple nested queries then I have the source table (imported one Colum entry text file) and my destination table will constantly be my preexistent table, each time and every time with only one constant never-changing query!
My query so far looks like this:
For simplicity
0 means my preexisting table in my database
9 is the table which contains the imported data (which at each import get's overwritten)
Code:
INSERT INTO 0 ( word, nrbooks, freq, distrib )
SELECT [0].word, sum([0].nrbooks) AS nrbooks, sum([0].freq) AS freq, [0]!nrbooks*[0]!freq AS distrib
FROM (SELECT [9].Field1 AS word, 1 AS nrbooks, Count([9]![Field1]) AS freq FROM 9 GROUP BY [9].Field1) AS 0
GROUP BY [0].word, [0].nrbooks, [0].freq;
This query works well but it doesn't do the last step 4 aggregate query. It's just a simple append query with a aggregate sub-query. I want this whole query INSIDE ANOTHER AGGREGATE QUERY! Can it be done? Or I approach this problem in a wrong way? Can it be done easier in another way? How?
Please, can someone give me a helping hand?