I have a SQL query that I need to write in ecto. something like this
SELECT salesperson,
Avg(CASE WHEN DATEPART(weekday, [the_date]) NOT IN (1, 7) THEN [pencil_sales] END)) as pencil_salesAV_weekday,
Avg(CASE WHEN DATEPART(weekday, [the_date]) NOT IN (1, 7) THEN [pen_sales] END) as pen_salesAV_weekday,
Avg(CASE WHEN DATEPART(weekday, [the_date]) IN (1, 7) THEN [pencil_sales] END)) as pencil_salesAV_weekend,
Avg(CASE WHEN DATEPART(weekday, [the_date]) IN (1, 7) THEN [pen_sales] END) as pen_salesAV_weekend,
Month(the_date) as Month,
Year(the_date) as Year
FROM regionalsales
GROUP BY GROUPING SETS((Month(the_date), Year(the_date), salesperson), (salesperson));
I’m having a hard time converting this in ecto. I need to use case statement in the fragment but I don’t know the exact syntax and I’m missing something in that case. How can I write this?
The group by part would probably be something like group_by: fragment("GROUPING SETS((Month(?), Year(?), ?), (?))", the_date, the_date, salesperson, salesperson)