?
Solved

How can I create an Access Query that reads a table column based on other column condtions

Posted on 2014-04-30
6
Medium Priority
?
297 Views
Last Modified: 2014-06-25
I have a table in MS Access that has time related data and I need to select only the pressure at the minimum depth and maximum depth for each well in each time.

Please find the MS Access table attached and I am expecting to have the following results from the query.

Well   Date           Pressure_Min_depth          Pressure_Max_depth
1       1/1/2013                  2333                         46784
1       3/22/2014                 6768                        54654
2       4/1/2014                   2333                        6768
test123.mdb
0
Comment
Question by:Mohammed Dallag
  • 3
  • 2
6 Comments
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 total points
ID: 40031614
Probably a more elegant way of getting this, but define one query as:

SELECT Table1.well, Table1.xdate, Min(Table1.depth) AS MinOfdepth, Max(Table1.depth) AS MaxOfdepth
FROM Table1
GROUP BY Table1.well, Table1.xdate;

Then a second query of:

SELECT qryWellDepths.well, qryWellDepths.xdate, qryWellDepths.MinOfdepth, Table1.pressure, qryWellDepths.MaxOfdepth, Table1_1.pressure
FROM (qryWellDepths INNER JOIN Table1 ON (qryWellDepths.MinOfdepth = Table1.depth) AND (qryWellDepths.well = Table1.well) AND (qryWellDepths.xdate = Table1.xdate)) INNER JOIN Table1 AS Table1_1 ON (qryWellDepths.MaxOfdepth = Table1_1.depth) AND (qryWellDepths.xdate = Table1_1.xdate) AND (qryWellDepths.well = Table1_1.well);

Jim.
0
 
LVL 58
ID: 40031617
Here's the DB by the way..

Jim.
test123.mdb
0
 

Author Comment

by:Mohammed Dallag
ID: 40031680
Jim,

I need the pressure at the max depth and min depth not the depth.

Regards,

Dallag
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 58
ID: 40031722
That's what I gave you.  For each well, for each day, the min depth, pressure at that depth, and the max depth and the pressure at that depth.

Jim.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40031896
try this query


Select T.well,T.xdate As [Date], Min(T.Pressure) as Pressure_Min_depth,  Max(T.Pressure) as Pressure_Max_depth
From
(
SELECT A.Well, A.xdate, A.MinOfdepth AS Depth, B.Pressure
FROM (SELECT Table1.well, Table1.xdate, Min(Table1.depth) AS MinOfdepth
FROM Table1
GROUP BY Table1.well, Table1.xdate
UNION ALL
SELECT Table1.well, Table1.xdate, Max(Table1.depth) AS MinOfdepth
FROM Table1
GROUP BY Table1.well, Table1.xdate
Order By 1,2,3
)  AS A INNER JOIN Table1 AS B ON (A.Well = B.Well) AND (A.xDate = B.xDate) AND (A.MinOfdepth = B.Depth)
ORDER BY A.Well, A.xdate, A.MinOfdepth, B.Pressure
) As T
Group By  T.well,T.xdate
0
 

Author Closing Comment

by:Mohammed Dallag
ID: 40156841
Thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question