I have the following dataset with let's say ID = {1,[...],5} and Col1 = {a,b,c,Null} :
ID | Col1 | Date |
---|---|---|
1 | a | 01/10/2022 |
1 | a | 02/10/2022 |
1 | a | 03/10/2022 |
2 | b | 01/10/2022 |
2 | c | 02/10/2022 |
2 | c | 03/10/2022 |
3 | a | 01/10/2022 |
3 | b | 02/10/2022 |
3 | Null | 03/10/2022 |
4 | c | 01/10/2022 |
5 | b | 01/10/2022 |
5 | Null | 02/10/2022 |
5 | Null | 03/10/2022 |
I would like to group my rows by ID, compute new columns to show the number of occurences and compute a new column that would show a string of characters, depending on the frequency of Col1. With most a = Hi, most b = Hello, most c = Welcome, most Null = Unknown. If multiple modalities except Null have the same frequency, the most recent one based on date wins.
Here is the dataset I need :
ID | nb_a | nb_b | nb_c | nb_Null | greatest |
---|---|---|---|---|---|
1 | 3 | 0 | 0 | 0 | Hi |
2 | 0 | 1 | 2 | 0 | Welcome |
3 | 1 | 1 | 0 | 1 | Hello |
4 | 0 | 0 | 1 | 0 | Welcome |
5 | 0 | 1 | 0 | 2 | Unknown |
I have to do this in a compute recipe in Dataiku. The group by is handled by the group by section of the recipe while the rest of the query needs to be done in the "custom aggregations" section of the recipe. I'm having troubles with the if equality then most recent part of the code.
My SQL code looks like this :
CASE WHEN SUM(CASE WHEN Col1 = a THEN 1 ELSE 0) >
SUM(CASE WHEN Col1 = b THEN 1 ELSE 0)
AND SUM(CASE WHEN Col1 = a THEN 1 ELSE 0) >
SUM(CASE WHEN Col1 = c THEN 1 ELSE 0)
THEN 'Hi'
CASE WHEN SUM(CASE WHEN Col1 = b THEN 1 ELSE 0) >
SUM(CASE WHEN Col1 = a THEN 1 ELSE 0)
AND SUM(CASE WHEN Col1 = b THEN 1 ELSE 0) >
SUM(CASE WHEN Col1 = c THEN 1 ELSE 0)
THEN 'Hello'
CASE WHEN SUM(CASE WHEN Col1 = c THEN 1 ELSE 0) >
SUM(CASE WHEN Col1 = a THEN 1 ELSE 0)
AND SUM(CASE WHEN Col1 = c THEN 1 ELSE 0) >
SUM(CASE WHEN Col1 = b THEN 1 ELSE 0)
THEN 'Welcome'
Etc, etc, repeat for other cases.
But surely there must be a better way to do this right? And I have no idea how to include the most recent one when frequencies are the same.
Thank you for your help and sorry if my message isn't clear.