JimmyTheHand
01-31-2007, 02:01 AM
Hi All,
this is an SQL question, rather than Access related. As a matter of fact, it has nothing to do with Access, but I think this is the best choice of forums.
So the outline is this. In an analytical laboratory there's a database to store samples, tests and results. I need to retrieve the average time (called "transit time") that was needed to complete each individual tests over a year, and display this average value for each year, separately.
Individual transit times are calculated as the difference between the time of test completion and time of sample login. The current year is always the year of sample login, even if test completion is postponed until next year.
The twist is that there are two sets of DB tables, an active (sample, test) and an archived (c_sample, c_test) set. Their definitions are identical, but one stores recent data, and is used everyday, the other stores old data, and is used rarely. I need data from both, at the same time.
I figured, what I needed to do was making two selects, grouping them by the year value, and then making a union of them. Here's what I did:
SELECT avg(test.date_completed-sample.login_date) as transit, to_char(to_date(sample.login_date), 'YYYY') as wYear
FROM sample JOIN test
ON sample.id_numeric = test.sample
GROUP BY wYear
UNION
SELECT avg(c_test.date_completed-c_sample.login_date) as transit, to_char(to_date(c_sample.login_date), 'YYYY') as wYear
FROM c_sample JOIN c_test
ON c_sample.id_numeric = c_test.sample
GROUP BY wYear
In my best knowledge the above SELECT should work, but it doesn't. Anyone knows why? The error message is:
ORA - 00904: "WYEAR": invalid identifier
Remark: The query does work without the GROUP BY clauses. But, of course, it's not what I need.
Thanks,
Jimmy
EDIT:
The same problem (with almost identical description) has been cross-posted on TSG forum:
http://forums.techguy.org/development/539870-sql-how-use-group-union.html#post4405515
this is an SQL question, rather than Access related. As a matter of fact, it has nothing to do with Access, but I think this is the best choice of forums.
So the outline is this. In an analytical laboratory there's a database to store samples, tests and results. I need to retrieve the average time (called "transit time") that was needed to complete each individual tests over a year, and display this average value for each year, separately.
Individual transit times are calculated as the difference between the time of test completion and time of sample login. The current year is always the year of sample login, even if test completion is postponed until next year.
The twist is that there are two sets of DB tables, an active (sample, test) and an archived (c_sample, c_test) set. Their definitions are identical, but one stores recent data, and is used everyday, the other stores old data, and is used rarely. I need data from both, at the same time.
I figured, what I needed to do was making two selects, grouping them by the year value, and then making a union of them. Here's what I did:
SELECT avg(test.date_completed-sample.login_date) as transit, to_char(to_date(sample.login_date), 'YYYY') as wYear
FROM sample JOIN test
ON sample.id_numeric = test.sample
GROUP BY wYear
UNION
SELECT avg(c_test.date_completed-c_sample.login_date) as transit, to_char(to_date(c_sample.login_date), 'YYYY') as wYear
FROM c_sample JOIN c_test
ON c_sample.id_numeric = c_test.sample
GROUP BY wYear
In my best knowledge the above SELECT should work, but it doesn't. Anyone knows why? The error message is:
ORA - 00904: "WYEAR": invalid identifier
Remark: The query does work without the GROUP BY clauses. But, of course, it's not what I need.
Thanks,
Jimmy
EDIT:
The same problem (with almost identical description) has been cross-posted on TSG forum:
http://forums.techguy.org/development/539870-sql-how-use-group-union.html#post4405515