473,327 Members | 1,952 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,327 software developers and data experts.

Access SQL query help

Jim
Hi,

I have some basic SQL experience (more programming and EXCEL
experience) and I am having a hard time generating a particular query.
My table is a historical database with columns (station,name,value,
time) for each time stamp and station there may be up to 50 records
each with a different name. What I want to do is for each value of
station retrieve the values for names matching key1,key2, and key3 at
the time key0 is at it's maximum. The query below works correctly for
station JOHN, but I don't want to have to cut and paste this query for
each station. Essentially I'd like to loop through each station and
perform this same query with JOHN being the replaced by the looped
station name. I've tried several variants with no success any ideas?
SELECT DB.STATION, DB.NAME, DB.VALUE, DB.TIME
FROM DB
WHERE (((DB.TIME)=(SELECT TOP 1 DB.TIME
FROM DB
WHERE (((DB.TIME)>#11/30/2005 23:59:59#) AND
((DB.TIME)<#3/1/2006 23:59:59#) AND
((DB.STATION)='JOHN') AND
((DB.NAME)='KEY0')) ORDER BY DB.VALUE DESC)
)
AND
((DB.STATION)='JOHN' AND
(DB.NAME) LIKE 'KEY1' AND
(DB.NAME) LIKE 'KEY2' AND
(DB.NAME) LIKE 'KEY3'));

Thank you.

Jim

Apr 26 '06 #1
4 1900
Jim,

are all the Station values in a table somewhere? If so (or you could
put them into one), then you could just join to the filtered table. A
really simple example would be:

DB.STATION, DB.NAME, DB.VALUE, DB.TIME
FROM DB inner join tblStation ON DB.Station=Station.StationName
AND Station.StationName IN (...some subselect query here)

That way you could pretty easily filter for just the records you want
by building the list of stations you wanted (with a query) and then
including that in your second query.

eg
SELECT DB.*
FROM DB INNER JOIN qrySomeQuery ON DB.FieldX=qrySomeQuery.FieldY
WHERE...

Hope this helps.

Apr 26 '06 #2
I assume you are viewing the query results in a form. What you can do
is to make that a subform and place it in another form which would be
your main form (or whatever name you want). In the main form you can
have a combobox with a list of the stations. Then, in the query in the
criteria section of the stations field you can make a reference to the
combobox on the main form

field: Station
criteria: Forms!mainFrom!combo1

Then when you select a station from the combobox on the form, the query
will query on that station. You will need to add a teeny bit of code to
the combobox. Go to the Form design to the Combobox Properties sheet.
Scroll down to toward the bottom of the property sheet for the combobox.
When you see a listing that says "After Update" go to the right of that
and click on the elipse ... button. Select "Code". Here you will write
the following:

Me.yourSubformName.Form.Requery

Replace "yourSubformName" with the name of the form where you view your
query results (make sure that form is in datasheet view) which is now
your subform. Now you have a more dynamic query.
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Apr 26 '06 #3
Yeah, this was tricky. I broke it down into steps:

First, find the highest value of Key0 for each station:

SELECT db.Station, Max(db.Value) AS MaxOfValue
FROM db
WHERE (((db.Name)="KEY0"))
GROUP BY db.Station;

Save this one as Query1
Now, find the time that happened:

SELECT db.Station, db.Time, db.Name
FROM db INNER JOIN Query1
ON (db.Value = Query1.MaxOfValue) AND (db.Station = Query1.Station)
WHERE (((db.Name)="key0"));

Save this one as Query2

Finally, get all the keys for that time:

SELECT db.Station, db.Name, db.Value, db.Time
FROM db INNER JOIN
Query2 ON Query2.Time = db.Time AND db.Station = Query2.Station
WHERE db.Name in ("Key0","key1","key2","key3")

This Should work, but no promises. Let me know if you need more help.
Chris Nebinger

Apr 26 '06 #4
Jim
All,

Thanks... This gave me a new direction to go in designing my query.
My version of
ACCESS does not like using the JOIN syntax. I'm using the "from db,
query1" syntax.
Is there a difference between the two methods?

Chris, I was able to get steps 1 and 2 working with only a slight
modification for my needs. However, the final step is killing me....
I'm getting an error message of being out ot temporary
disk space. My DB is almost 2GB with 15M records! So I'm having to go
back to the drawing
board again.... :-)
Thanks,

Jim

Apr 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Morten Gulbrandsen | last post by:
Hi programmers, Good morning ;-) I think something is wrong, Running windows I believe first I have to set some password, Which creates some user, Then I can grant the required privileges, for...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
13
by: Peter James | last post by:
Access 97 If I select New on the Query tab of the db window, and go staight to sql view and type in the following for example: INSERT INTO tblMyTable ( dtDate, txtAny) VALUES (#2003-09-03#,...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
52
by: Neil | last post by:
We are running an Access 2000 MDB with a SQL 7 back end. Our network guy is upgrading to Windows Server 2003 and wants to upgrade Office and SQL Server at the same time. We're moving to SQL Server...
4
by: Takeadoe | last post by:
Hey Gang, I'm gearing up to retool for the upcoming deer season here in Ohio and I could use some help with very general questions about direction. I will be scanning nearly 210,000 forms that...
1
by: hardik | last post by:
hi friends i need help in this sql query i have table like, id fid __ _____ autonumber text and i am storing values like
8
by: Sid | last post by:
I hope someone could help me with this. I am trying to setup a criteria to decide when to allow/not allow user to click on the check box. logically it looks simple but I am not able to...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.