Solved

How can make MS Access Query that read a table column based on conidtion

Posted on 2014-04-02
4
418 Views
Last Modified: 2014-04-22
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 follwing results from the query.

Well   Date       Depth   Pressure
1     1/1/2013     23        2333
1     1/1/2013    666       46784
1     3/22/2014    2         6768
1     3/22/2014  777       54654
2     4/1/2014    11         2333
2     4/1/2014    888       6768
test123.mdb
0
Comment
Question by:dallagmm
  • 2
4 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
You would start out with an aggregate query:

qryWellDates:
SELECT Well, [Date] as PressDate, Min([Depth]) as MinDepth, Max([Depth]) as MaxDepth
FROM yourTable
GROUP BY Well, [Date]

Then, you would join this to your table to get the pressure for those Well/Date combinations

SELECT qryWellDates.Well, qryWellDates.PressDate, qryWellDates.MinDepth as Depth,
             yourTable.Pressure
FROM qryWellDates
INNER JOIN yourTable
ON qryWellDates.Well = yourTable.Well
AND qryWellDates.PressDate = yourTable.[Date]
AND qryWellDates.MinDepth = yourTable.[Depth]
UNION ALL
SELECT qryWellDates.Well, qryWellDates.PressDate, qryWellDates.MaxDepth as Depth,
             yourTable.Pressure
FROM qryWellDates
INNER JOIN yourTable
ON qryWellDates.Well = yourTable.Well
AND qryWellDates.PressDate = yourTable.[Date]
AND qryWellDates.MaxDepth = yourTable.[Depth]
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
try this query


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;



see query1 on your sample db
test123.mdb
0
 

Author Closing Comment

by:dallagmm
Comment Utility
very good
0
 

Author Comment

by:dallagmm
Comment Utility
I need to change the output of the MS Query to be like the attached file
4-22-2014-12-26-08-PM.jpg
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now