
Originally Posted by
madpiet
Starting from qryIntraDayDateHighLowTimes_v2? or where?
I think you have to join on the value and the date back to the original table (or other query) to do that. Or did you want something like
(Date, HighLow, Time, Value)?
For completeness, the input table snippet is this:
ID |
BarDate |
open |
high |
low |
close |
1 |
11/1/2023 2:45:00 AM |
$329.34 |
$329.34 |
$329.34 |
$329.34 |
2 |
11/1/2023 2:54:00 AM |
$329.16 |
$329.16 |
$329.16 |
$329.16 |
3 |
11/1/2023 4:03:00 AM |
$329.24 |
$329.26 |
$329.24 |
$329.26 |
4 |
11/1/2023 4:39:00 AM |
$329.50 |
$329.50 |
$329.50 |
$329.50 |
5 |
11/1/2023 5:00:00 AM |
$329.51 |
$329.56 |
$329.51 |
$329.56 |
6 |
11/1/2023 5:21:00 AM |
$329.36 |
$329.36 |
$329.36 |
$329.36 |
7 |
11/1/2023 5:30:00 AM |
$329.36 |
$329.36 |
$329.35 |
$329.35 |
8 |
11/1/2023 6:00:00 AM |
$329.24 |
$329.24 |
$329.24 |
$329.24 |
The query to extract daily dates, highs and lows, and the times of the highs and lows, qryIntraDayDateHighLowTimes_v2 is:
Code:
SELECT A.IntraDayDate, TIMEVALUE(B.BarDate) AS HighTime, A.IntraDayHigh, TIMEVALUE(C.BarDate) AS LowTime, A.IntraDayLow
FROM ((SELECT DATEVALUE(BarDate) AS IntraDayDate, MAX(high) AS IntraDayHigh, MIN(low) AS IntraDayLow FROM DIA_3min WHERE TIMEVALUE(BarDate) BETWEEN #7:30:00 AM# AND #1:57:00 PM# GROUP BY DATEVALUE(BarDate)) AS A INNER JOIN DIA_3min AS B ON (A.IntraDayHigh = B.high) AND (DATEVALUE(B.BarDate) = A.IntraDayDate)) INNER JOIN DIA_3min AS C ON (A.IntraDayLow = C.low) AND (DATEVALUE(C.BarDate) = A.IntraDayDate)
WHERE TIMEVALUE(B.BarDate) BETWEEN #7:30:00 AM# AND #1:57:00 PM# AND TIMEVALUE(C.BarDate) BETWEEN #7:30:00 AM# AND #1:57:00 PM#
ORDER BY A.IntraDayDate;
The snippet of that query's output is:
IntraDayDate |
HighTime |
IntraDayHigh |
LowTime |
IntraDayLow |
11/1/2023 |
1:33:00 PM |
$333.36 |
7:42:00 AM |
$330.10 |
11/2/2023 |
1:54:00 PM |
$338.52 |
7:42:00 AM |
$334.41 |
11/3/2023 |
11:03:00 AM |
$341.62 |
8:54:00 AM |
$339.39 |
11/6/2023 |
7:30:00 AM |
$341.64 |
12:15:00 PM |
$339.89 |
11/7/2023 |
9:39:00 AM |
$342.07 |
7:39:00 AM |
$340.30 |
11/8/2023 |
7:48:00 AM |
$342.59 |
10:06:00 AM |
$339.94 |
11/9/2023 |
7:30:00 AM |
$342.05 |
1:39:00 PM |
$338.77 |
The input table with half-hour intervals that allows us to calculate bucket totals and percentages for those half-hour intervals is:
ID |
StartTime |
EndTime |
2 |
7:30 |
8:00 |
3 |
8:00 |
8:30 |
4 |
8:30 |
9:00 |
5 |
9:00 |
9:30 |
6 |
9:30 |
10:00 |
7 |
10:00 |
10:30 |
8 |
10:30 |
11:00 |
9 |
11:00 |
11:30 |
10 |
11:30 |
12:00 |
11 |
12:00 |
12:30 |
12 |
12:30 |
13:00 |
13 |
13:00 |
13:30 |
14 |
13:30 |
14:00 |
The query to extract the count of the highs and the percentage of those highs in half-hourly time timeslots, is:
Code:
SELECT B.StartTime, B.EndTime, COUNT(A.HighTime) AS NumHighs, ROUND(100 * COUNT(A.HighTime)/(SELECT TOP 1 COUNT(DATEVALUE(A.IntraDayDate)) FROM qryIntraDayDateHighLowTimes_v2), 2) AS PctHighs
FROM qryIntraDayDateHighLowTimes_v2 AS A INNER JOIN HalfHourIntervalsT AS B ON (A.HighTime >= B.StartTime) AND (A.HighTime < B.EndTime)
GROUP BY StartTime, EndTime;
The final output of that query is:
StartTime |
EndTime |
NumHighs |
PctHighs |
7:30 |
8:00 |
110 |
28.5 |
8:00 |
8:30 |
24 |
6.22 |
8:30 |
9:00 |
23 |
5.96 |
9:00 |
9:30 |
19 |
4.92 |
9:30 |
10:00 |
18 |
4.66 |
10:00 |
10:30 |
12 |
3.11 |
10:30 |
11:00 |
16 |
4.15 |
11:00 |
11:30 |
16 |
4.15 |
11:30 |
12:00 |
17 |
4.4 |
12:00 |
12:30 |
17 |
4.4 |
12:30 |
13:00 |
17 |
4.4 |
13:00 |
13:30 |
27 |
6.99 |
13:30 |
14:00 |
70 |
18.13 |
Replace all of the Highs in the above query with Lows and you get the Daily Lows and Percentages:
StartTime |
EndTime |
NumLows |
PctLows |
7:30 |
8:00 |
126 |
32.64 |
8:00 |
8:30 |
42 |
10.88 |
8:30 |
9:00 |
23 |
5.96 |
9:00 |
9:30 |
12 |
3.11 |
9:30 |
10:00 |
14 |
3.63 |
10:00 |
10:30 |
17 |
4.4 |
10:30 |
11:00 |
16 |
4.15 |
11:00 |
11:30 |
19 |
4.92 |
11:30 |
12:00 |
17 |
4.4 |
12:00 |
12:30 |
17 |
4.4 |
12:30 |
13:00 |
18 |
4.66 |
13:00 |
13:30 |
19 |
4.92 |
13:30 |
14:00 |
46 |
11.92 |
The only remaining problem is that when I both highs and lows into the same query, both the highs and lows have the same numbers which is obviously incorrect. I think it's because the criteria are different for the highs and lows, but I don't know how to resolve those differences in a single query.
This is the combined query which produces the duplicate results:
Code:
SELECT B.StartTime, B.EndTime, COUNT(A.HighTime) AS NumHighs, ROUND(100 * COUNT(A.HighTime)/(SELECT TOP 1 COUNT(DATEVALUE(A.IntraDayDate)) FROM qryIntraDayDateHighLowTimes_v2), 2) AS PctHighs, COUNT(A.LowTime) AS NumLows, ROUND(100 * COUNT(A.LowTime)/(SELECT TOP 1 COUNT(DATEVALUE(A.IntraDayDate)) FROM qryIntraDayDateHighLowTimes_v2), 2) AS PctLows
FROM qryIntraDayDateHighLowTimes_v2 AS A INNER JOIN HalfHourIntervalsT AS B ON (A.HighTime<B.EndTime) AND (A.HighTime>=B.StartTime) OR (A.LowTime<B.EndTime) AND (A.LowTime>=B.StartTime)
GROUP BY StartTime, EndTime;
The bogus output: StartTime |
EndTime |
NumHighs |
PctHighs |
NumLows |
PctLows |
7:30 |
8:00 |
229 |
59.33 |
229 |
59.33 |
8:00 |
8:30 |
66 |
17.1 |
66 |
17.1 |
8:30 |
9:00 |
45 |
11.66 |
45 |
11.66 |
9:00 |
9:30 |
31 |
8.03 |
31 |
8.03 |
9:30 |
10:00 |
32 |
8.29 |
32 |
8.29 |
10:00 |
10:30 |
29 |
7.51 |
29 |
7.51 |
10:30 |
11:00 |
32 |
8.29 |
32 |
8.29 |
11:00 |
11:30 |
35 |
9.07 |
35 |
9.07 |
11:30 |
12:00 |
34 |
8.81 |
34 |
8.81 |
12:00 |
12:30 |
34 |
8.81 |
34 |
8.81 |
12:30 |
13:00 |
35 |
9.07 |
35 |
9.07 |
13:00 |
13:30 |
46 |
11.92 |
46 |
11.92 |
13:30 |
14:00 |
116 |
30.05 |
116 |
30.05 |
If you have any ideas about combining the highs and lows into a single query, I'm all ears. A Union query won't work because the column headers would be misleading.
Cheers,
Eric