Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to identify the data in an interval of time between two days

Status
Not open for further replies.

leeamra

Vendor
Mar 5, 2016
2
EG
I have two tables, the first is the data table about the stock price per minute. It is composed of six columns, the date and time, the opening price, the highest price, the lowest price, and the closing price.

Table Data

`ID Field1 Field2 Field3 Field4 Field5 Field6
2 2016.02.17 11:36:00 PM 1.42918 1.42924 1.42904 1.4292
3 2016.02.17 11:37:00 PM 1.42919 1.42922 1.42886 1.42912
4 2016.02.17 11:38:00 PM 1.42911 1.42917 1.42902 1.42917
5 2016.02.17 11:39:00 PM 1.42918 1.42918 1.42887 1.42904`

The second table consists of seven columns, the date and six different times.

Table Time
`ID Field1 Field2 Field3 Field4 Field5 Field6 Field7
2 2016.01.02 5:35:00 AM 6:58:00 AM 12:25:00 PM 3:30:00 PM 5:52:00 PM 7:22:00 PM
3 2016.01.03 5:35:00 AM 6:58:00 AM 12:25:00 PM 3:30:00 PM 5:53:00 PM 7:22:00 PM
4 2016.01.04 5:36:00 AM 6:59:00 AM 12:26:00 PM 3:31:00 PM 5:53:00 PM 7:23:00 PM
5 2016.01.05 5:36:00 AM 6:59:00 AM 12:26:00 PM 3:31:00 PM 5:54:00 PM 7:24:00 PM`

I do the work of four queries to get the stock price in a specified period from the second table showing the date, the opening price, the highest price and its time of achievement, the lowest price and time and its time of achievement, and the closing price.

Quiries 1

`SELECT First(Times.Field1) AS [Date], First(Times.Field2) AS FirstOfField2, First(Times.Field6) AS FirstOfField6, First(Data.Field3) AS [Open], Max(Data.Field4) AS [Max], Min(Data.Field5) AS [Min], Last(Data.Field6) AS [Close]
FROM Data, Times
WHERE (((Data.Field1)=[Times].[Field1]) AND ((Data.Field2) Between [Times].[Field2] And [Times].[Field6]))
GROUP BY Times.ID;`

Result 1
`Date FirstOfField2 FirstOfField6 Open Max Min Close
2016.02.19 5:29:00 AM 6:22:00 PM 1.43217 1.4337 1.42461 1.42653
2016.02.22 5:28:00 AM 6:23:00 PM 1.42744 1.42892 1.40567 1.41282
2016.02.23 5:27:00 AM 6:24:00 PM 1.4133 1.41527 1.40615 1.40813`

Quiries 2

`SELECT First(Result.Date) AS [Date], First(Data.Field2) AS MaxTime, First(Result.Max) AS MaxValue
FROM Data, Result
WHERE (((Data.Field4)=[Result].[Max]) AND ((Data.Field1)=[Result].[Date]) AND ((Data.Field2) Between [Result].[FirstOfField2] And [Result].[FirstOfField6]))
GROUP BY Result.Date;`

Result 2

`Date MaxTime MaxValue
2016.02.18 3:21:00 PM 1.43938
2016.02.19 8:39:00 AM 1.4337
2016.02.22 8:37:00 AM 1.42892`

Quiries 3

`SELECT First(Result.Date) AS [Date], First(Data.Field2) AS MinTime, First(Result.Min) AS MinValue
FROM Data, Result
WHERE (((Data.Field5)=[Result].[Min]) AND ((Data.Field1)=[Result].[Date]) AND ((Data.Field2) Between [Result].[FirstOfField2] And [Result].[FirstOfField6]))
GROUP BY Result.Date;`

Result 3

`Date MinTime MinValue
2016.02.18 10:01:00 AM 1.42558
2016.02.19 4:32:00 PM 1.42461
2016.02.22 3:40:00 PM 1.40567`

Quiries 4 (Final)

`SELECT First(Result.Date) AS [Date], First(Result.FirstOfField2) AS FirstOfFirstOfField2, First(Result.FirstOfField6) AS FirstOfFirstOfField6, First(Result.Open) AS [Open], First(Result.Max) AS MaxValue, First(Max.MaxTime) AS MaxTime, First(Result.Min) AS MinValue, First(Min.MinTime) AS MinTime, First(Result.Close) AS [Close]
FROM Result, [Max], [Min]
WHERE (((Result.Max)=([Max].[MaxValue])) AND ((Result.Min)=([Min].[MinValue])))
GROUP BY Result.Date;`

Result 4 (Final)

`Date FirstOfFirstOfField2 FirstOfFirstOfField6 Open MaxValue MaxTime MinValue MinTime Close
2016.02.18 5:30:00 AM 6:22:00 PM 1.42989 1.43938 3:21:00 PM 1.42558 10:01:00 AM 1.43572
2016.02.19 5:29:00 AM 6:22:00 PM 1.43217 1.4337 8:39:00 AM 1.42461 4:32:00 PM 1.42653
2016.02.22 5:28:00 AM 6:23:00 PM 1.42744 1.42892 8:37:00 AM 1.40567 3:40:00 PM 1.41282
2016.02.23 5:27:00 AM 6:24:00 PM 1.4133 1.41527 11:41:00 AM 1.40615 5:47:00 PM 1.40813`

The problem that I am facing, when I want to identify the data in an interval of time between two days.
Example: when I need to analyze the data in an interval of time between 11:00 pm on 17.02.2016 and 05:00 am on 18.02.2016

Thank you
 
 http://files.engineering.com/getfile.aspx?folder=df0307dd-076c-4aab-9289-82005eabd9c2&file=Database1.zip
First, use TGML tags to show your data:

[pre]
ID Field1 Field2 Field3 Field4 Field5 Field6
2 2016.02.17 11:36:00 PM 1.42918 1.42924 1.42904 1.4292
3 2016.02.17 11:37:00 PM 1.42919 1.42922 1.42886 1.42912
4 2016.02.17 11:38:00 PM 1.42911 1.42917 1.42902 1.42917
5 2016.02.17 11:39:00 PM 1.42918 1.42918 1.42887 1.42904`
[/pre]
Second, do not use reserved words in Access as table and field's names, like Data and Time.

Use meaningful names for your field names and NOT Field1, Field2, Field3 ...

And the last suggestion - I would NOT split Date and Time into separate fields. If you keep it as one DateTime field, you can use BETWEEN in your Select statements.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Quiries 1

`SELECT First(Times.Field1) AS [Date], First(Times.Field2) AS FirstOfField2, First(Times.Field6) AS FirstOfField6, First(Data.Field3) AS [Open], Max(Data.Field4) AS [Max], Min(Data.Field5) AS [Min], Last(Data.Field6) AS [Close]
FROM Data, Times
[highlight #C17D11]WHERE (((Data.Field1)=[Times].[Field1]) AND ((Data.Field2) Between [Times].[Field2] And [Times].[Field6]))
GROUP BY Times.ID;`
[/highlight]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top