'Return values for latest three month
I am new to sql and I have a question for SQL using Data GRIP.
I have a table with list of client name as my rows and for each client (row) I have multiple dates.

I need to change the structure of the table and instead of having my dates as rows, I want to list them as column but only for the latest three month AND if I do not have any value then return ' no activity'
I used the below sql query but the result is incorrect because I still cannot get distinct list of my rows
SELECT DISTINCT(client),
CASE WHEN month = dateadd(month, -3, date_trunc('MONTH',getdate())::DATE)
THEN cast(categories AS varchar(12))
ELSE 'no activity' END AS latest_three_month,
CASE WHEN month = dateadd(month, -2, date_trunc('MONTH',getdate())::DATE)
THEN cast(categories AS varchar(12))
ELSE 'no activity' END AS latest_second_month,
CASE WHEN month = dateadd(month, -1, date_trunc('MONTH',getdate())::DATE)
THEN cast(categories AS varchar(12))
ELSE 'no activity' END AS latest_month
FROM MYTABLE;
Even though, I specified "SELECT DISTINCT(client)", but the result is looks like the below image and it is incorrect:
Appreciate your help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


