Query combining data and SQL-calculations (1 Viewer)

EonsTimE

Registered User.
Local time
Today, 08:46
Joined
May 8, 2016
Messages
13
Hi,
I need a single SQL query string so that I can use it as a Graph's RowSource in my VBA code.
My SQL query combines two sub-Queries: 1) data 2) SQL-calculations performed on that data (Please don't judge me for doing it this way :) ).

Queries 1 and 2 work separately, if I refer to Query1 as an object inside Query2, but not when I use Query1 as SQL statement inside Query2.

Please see attached DB example (Query2 works, Query3 fails).

Query1
Code:
SELECT 
tbl_Data1.rec_ID, 
tbl_Data1.rec_ItemNo, 
tbl_Data1.rec_Value1, 
tbl_Data1.rec_Value2

FROM tbl_Data1

WHERE 

(tbl_Data1.rec_ID<

(SELECT tbl_Data1.rec_ID
FROM tbl_Data1
WHERE (((tbl_Data1.rec_ItemNo)="1045/16"))
ORDER BY tbl_Data1.rec_ID)

)
ORDER BY tbl_Data1.rec_ID;

Query2 - Pulling Query1 as object
Code:
SELECT 
Alias1.rec_ItemNo, 
Alias1.rec_Value1, 
Alias1.rec_Value2, 

Sum(Query1.rec_Value1) AS Sum_rec_Value1, 
StDev(Query1.rec_Value2) AS StDev_rec_Value2

FROM Query1, 

Query1 AS Alias1

GROUP BY Alias1.rec_ID, Alias1.rec_ItemNo, Alias1.rec_Value1, Alias1.rec_Value2

ORDER BY Alias1.rec_ID;

Query3 - using Query1 as SQL statement
Code:
SELECT 
Alias1.rec_ItemNo, 
Alias1.rec_Value1, 
Alias1.rec_Value2, 
Sum(QueryData.rec_Value1) AS Sum_rec_Value1, 
StDev(QueryData.rec_Value2) AS StDev_rec_Value2

FROM 
(SELECT tbl_Data1.rec_ID, tbl_Data1.rec_ItemNo, tbl_Data1.rec_Value1, tbl_Data1.rec_Value2 
FROM tbl_Data1 
WHERE (tbl_Data1.rec_ID<

(SELECT tbl_Data1.rec_ID
FROM tbl_Data1
WHERE (((tbl_Data1.rec_ItemNo)="1045/16"))
ORDER BY tbl_Data1.rec_ID)

) 

ORDER BY tbl_Data1.rec_ID)  AS QueryData, 

QueryData AS Alias1

GROUP BY Alias1.rec_ID, Alias1.rec_ItemNo, Alias1.rec_Value1, Alias1.rec_Value2
ORDER BY Alias1.rec_ID;

What fails in Query3 is assigning Alias1 to the Query1 [QueryData AS Alias1] so that data and calculations (SUM, StDev etc) can be put out as output together from Query2.

As you can see in sample DB, assigning Alias1 [Query1 AS Alias1] in Query2 works out.

Anyone would have advice on how to accomplish the above through pure SQL statement? I'd rather avoid saving Queries in the database, as I'd want to extensively modify the SQL query in VBA.

Or, alternatively, anyone know of a way to execute the above queries sequentially in VBA (for example ending up with a Recordset object) and a way to apply the Recordset object to an AccessGraph Object?

Thank you.
 

Attachments

  • SQL Demo2.accdb
    704 KB · Views: 70
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:46
Joined
Feb 28, 2001
Messages
27,140
We need to break that up into English to discuss it.

Query 1 is destined to return all data from tbl_Data1 where the rec_ID field is less than the rec_ID field of a specific other tbl_Data1 element with a specific rec_ItemNo. Correct?

To better understand query 1, we need to know whether rec_ID is a prime key or not, and also whether there is a mathematical dependency between rec_ID and rec_ItemNo. (If there is not, then query 1 might already be flawed.)

Query 2 then takes sums and standard deviations of the records returned from query 1. I'll comment in passing that having both GROUP BY and ORDER BY on the same field in the same query is overkill because GROUP BY implies ordering, and the odds are that the last three items in the four-item GROUP BY clause are insignificant. There is another implication here, though. Taking standard deviations implies that rec_ID is NOT a prime key (which is OK here, but we need to know more about data structure to help you reach your goal).

Query 3 then appears to somehow pull the individual values of the data table that is the source of query 2's aggregate functions and publish the aggregates next to the raw values. Again, having the multiple layers of GROUP BY will probably obviate the later members of the grouping.

Queries 1 and 2 work separately, if I refer to Query1 as an object inside Query2, but not when I use Query1 as SQL statement inside Query2.

Please explain the symptoms of this failure.

Please don't judge me for doing it this way

OK, this is a non-judgmental statement about you - but I have to pass judgment about the SQL. The confusing nature of the SQL betrays your own confusion about what you are trying to do.

Ask us the question in ENGLISH to tell us your goal. It appears that you are somehow attempting to tie together raw and aggregated data. To a very large degree that is an apples-to-oranges relationship.

Posting confusing code that we have to somehow decipher and deconvolute is not a good way to get help. Ask us questions in common language. Often, if you can post the question in verbiage rather than formulas or code, we will understand you better.
 

Users who are viewing this thread

Top Bottom