Combine queries

Roger9

New member
Local time
Today, 05:19
Joined
Aug 7, 2019
Messages
7
I have several queries for each gaging stations that are run to update the data table. All of these queries have a similar structure except for range values, site serials, and CountyID. How do I combine these queries to form a single query that would update records for all the gages?
For Gage1:
Code:
INSERT INTO [LRN-1705_WLs] ( [Day], CompWL, CountWL, site_serial, CountyID )
SELECT Format([Date_time],"mm/dd/yyyy") AS Expr1, Avg([MPC]-[MPH]-([ch1_data_P]-2.307*(IIf((([ch2_data_P]>=13.71) And ([ch2_data_P]<=15.06)),[ch2_data_P],Null)))) AS CompWL, Count(logger_data.ch1_data_P) AS Expr2, logger_data.site_serial, MPCable.CountyID
FROM logger_data INNER JOIN MPCable ON logger_data.site_serial = MPCable.site_serial
GROUP BY Format([Date_time],"mm/dd/yyyy"), logger_data.site_serial, MPCable.CountyID
HAVING (((logger_data.site_serial)=9100346));

For Gage2:
Code:
INSERT INTO [GRV-3342_WLs] ( [Day], CompWL, CountWL, site_serial, CountyID )
SELECT Format([Date_time],"mm/dd/yyyy") AS Expr1, Avg([MPC]-[MPH]-([ch1_data_P]-2.307*(IIf((([ch2_data_P]>=13.5) And ([ch2_data_P]<=14.84)),[ch2_data_P],Null)))) AS CompWL, Count(logger_data.ch1_data_P) AS Expr2, logger_data.site_serial, MPCable.CountyID
FROM logger_data INNER JOIN MPCable ON logger_data.site_serial = MPCable.site_serial
GROUP BY Format([Date_time],"mm/dd/yyyy"), logger_data.site_serial, MPCable.CountyID
HAVING (((logger_data.site_serial)=9100399));
 
Hi. I could be wrong but I'm not sure you can combine those queries into one because I think an INSERT query can only add records to one table at a time.
 
Ah! Good to know
 
Ah! Good to know
I guess what you could try is to create a query joining those two tables and see if you can add records to both tables at the same time through that query. If you can, then you might be able to combine your queries after all. I just can't guarantee it since I haven't tried it before. Good luck!
 
Agree with DBGs original response - not sure about the suggestion in the second reply.
However you could easily create a procedure to run each query or SQL statement in turn

Also, welcome to AWF
 
Last edited:
that are run to update the data table.

That's not correct. Those queries you posted are adding data to different tables. Why?

Code:
INSERT INTO [LRN-1705_WLs] ( [Day], CompWL, CountWL, site_serial, CountyID )
...
INSERT INTO [GRV-3342_WLs] ( [Day], CompWL, CountWL, site_serial, CountyID )
...

Your data doesn't seem normalized to me. You seem to be storing data in your table names. Tables should have generic names (e.g. tblGages, tblReadings, etc.). Your table names seem to be data themselves.

Tables with similar structures (both your tables in above have the same 6 fields) but different names which help differentiate the data means you've done it wrong. Instead of have the table name differentiate them, you need all the data in the same table with a field to differentiate them. That means 'LRN-1705_WLS' and 'GRV-3342_WLs' go into the data not the table name and you make the table name generic.

Do that and the INSERT queries can become an INSERT query pretty easily.
 
Why do you have multiple identical tables to begin with? Have 1 table with another field for gage identifier.

Why saving aggregate data?
 
Thank you all for your input and apologies for late response! I looked at the data and as most of the comments pointed out, there's no need to have an individual query for each gage station. I worked on it and created a single query using switch and IIf function. My approach is very basic and I am sure there's better ways of getting the desired output. I would like to get feedback on ways of improving my query structure. However, I should probably start a new discussion as I would like to add details on what I am trying to work on. Once again, thank you all for the comments!
 
Thank you all for your input and apologies for late response! I looked at the data and as most of the comments pointed out, there's no need to have an individual query for each gage station. I worked on it and created a single query using switch and IIf function. My approach is very basic and I am sure there's better ways of getting the desired output. I would like to get feedback on ways of improving my query structure. However, I should probably start a new discussion as I would like to add details on what I am trying to work on. Once again, thank you all for the comments!
Hi. Thanks for the update. Glad to hear you got it sorted out. Good luck with your project.
 

Users who are viewing this thread

Back
Top Bottom