brianc/node-sql

aggregate sum + value statements, with case statement

Opened this issue · 1 comments

Hi, I'm trying to figure out how to construct this statement -- which is fine in postgres:

select 
  case when (sum(regular_labor) > 100) then sum(regular_labor)
  else NULL
  end as "labor_alert"
from fact_labor
where location_key in (514829237237059584)
and business_date_key = 20141201;

What I've tried:

var labor = schema.fact_labor;
var qb = labor.select(
  labor.regular_labor.case(
    [labor.regular_labor.sum().gt(100)],
    [labor.regular_labor.sum()],
    null
  ).as('labor_alert')
).from(labor).where(
  labor.business_date_key.equals(20141201),
  labor.location_key.in('514829237237059584')
);

I've built other case statement queries and aggregates + value statements with node-sql, but I'm having some trouble composing them, and thought I'd query if it is possible yet? There were no tests/examples that looked like this.

Oh, I see a recent issue seems to address the same problem #149 -- perhaps the answer is that it is not possible yet. If so, feel free to close this and I'll follow #149