So for a table like this i want to have another column which has the reqd_col value if cond_value is > 100 and to take the previous value in other cases.
User | cond_Value | Reqd_Col |
---|---|---|
123456 | 159 | 12:30 |
123456 | 34 | 12:32 |
123456 | 46 | 12:35 |
123456 | 98 | 12:37 |
123456 | 123 | 12:56 |
123456 | 12 | 13:00 |
789000 | 100 | 16:00 |
789000 | 54 | 16:10 |
789000 | 23 | 16:14 |
789000 | 122 | 17:05 |
789000 | 98 | 17:08 |
789000 | 133 | 17:23 |
So the output should look like
User | cond_Value | Reqd_Col | Output |
---|---|---|---|
123456 | 159 | 12:30 | 12:30 |
123456 | 34 | 12:32 | 12:30 |
123456 | 46 | 12:35 | 12:30 |
123456 | 98 | 12:37 | 12:30 |
123456 | 123 | 12:56 | 12:56 |
123456 | 12 | 13:00 | 12:56 |
789000 | 100 | 16:00 | 16:00 |
789000 | 54 | 16:10 | 16:00 |
789000 | 23 | 16:14 | 16:00 |
789000 | 122 | 17:05 | 17:05 |
789000 | 98 | 17:08 | 17:05 |
789000 | 133 | 17:23 | 17:23 |
Ideally i would like to have this as a view in BigQuery.
Try below query.
SELECT *, LAST_VALUE(IF(cond_Value >= 100, Reqd_Col, NULL) IGNORE NULLS) OVER w AS Output
FROM sample_table
WINDOW w AS (PARTITION BY User ORDER BY PARSE_TIME('%R', Reqd_Col))
ORDER BY 1, 3;
+--------+------------+----------+--------+
| User | cond_Value | Reqd_Col | Output |
+--------+------------+----------+--------+
| 123456 | 159 | 12:30 | 12:30 |
| 123456 | 34 | 12:32 | 12:30 |
| 123456 | 46 | 12:35 | 12:30 |
| 123456 | 98 | 12:37 | 12:30 |
| 123456 | 123 | 12:56 | 12:56 |
| 123456 | 12 | 13:00 | 12:56 |
| 789000 | 100 | 16:00 | 16:00 |
| 789000 | 54 | 16:10 | 16:00 |
| 789000 | 23 | 16:14 | 16:00 |
| 789000 | 122 | 17:05 | 17:05 |
| 789000 | 98 | 17:08 | 17:05 |
| 789000 | 133 | 17:23 | 17:23 |
+--------+------------+----------+--------+
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.