I am trying to get a the max value per user within a time frame. The date range is determined by the first occurrence by each user within a separate time range and the 2 weeks prior, ie the first time a user shows up in October is 2018-10-01, therefore the range is from 2018-09-18 to 2018-10-01. Within that range, I am looking for the maximum Value of a column. All of this data comes from the same table.
Example:
+------------+------------+---------+
| Profile_ID | Date | Value |
+------------+------------+---------+
| 1 | 2018-10-05 | 100 |
| 2 | 2018-10-02 | 50 |
| 2 | 2018-10-04 | 78 |
| 2 | 2018-10-05 | 56 |
| 1 | 2018-10-08 | 110 |
| 1 | 2018-10-01 | 99 |
| 2 | 2018-09-30 | 88 |
| 1 | 2018-09-27 | 106 |
+------------+------------+---------+
I am looking for the peak VALUE from the two weeks prior to and including the first occurrence after OCT 1. That would be 2018-10-01 for USER 1 and 2018-10-02 for user 2. The peak value would then be 106 and 88, respectively.
I tried to the code:
SELECT max(Value)
FROM table
WHERE Date BETWEEN (
SELECT (min(Date) - INTERVAL 2 week)
FROM table
WHERE stamp between '2018-10-01' AND '2018-10-25'
GROUP BY profile_id
)
AND
(
SELECT min(Date)
FROM table
WHERE stamp between '2018-10-01' AND '2018-10-25'
GROUP BY profile_id
)
I am getting an error:
Syntax Error: at or near "2"
but I think that is just the start of my problems. Does anyone know the best way to find the max value in that time range for each profile_id?
If I understand correctly, you can use row_number()
and some date filtering:
select t.*
from (select t.*,
row_number() over (partition by profile_id order by date desc) as seqnum
from t
where date <= '2018-10-01'
) t
where seqnum = 1;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.