Answer the question
In order to leave comments, you need to log in
How to get the first non-NULL value in a group for a given field?
Let me explain.
For example, we have some SELECT ... GROUP BY.
It is necessary for each group to get the first non-zero value for the given field (not included in the grouping fields). Obviously, some ORDER BY is assumed within the groups.
Let there be a table like this:
client_id massage dttm
1 NULL 2022-01-01 05:12:12
1 hi 2022-01-01 05:12:15
1 I want to fuck 2022-01-01 05:12:18
3 NULL 2022 -01-02 05:12:12
3 NULL 2022-01-02 05:12:15
3 ohhh 2022-01-02 05:12:18
4 nice dick pic! 2022-01-03 05:12:18
Request should return:
client_id massage dttm
1 I want to fuck 2022-01-01 05:12:18
3 ohhh 2022-01-02 05:12:18
4 nice dick pic! 2022-01-03 05:12:18
Answer the question
In order to leave comments, you need to log in
SELECT DISTINCT
client_id,
FIRST_VALUE(massage)
OVER (PARTITION BY client_id
ORDER BY massage IS NULL, dttm) massage
FROM test
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question