2

I'm looking to display how many of the jobs in the job table that are being grouped by customer_id are approved. Approval is determined if approval_date is not null. So if there are 12 total jobs, I want to show that 7 are approved, for example. If the approval date is null, the job is not approved.

select   
  c.customer_name
  ,count(*) as counts
  , -- i want to add a column here that tells me which jobs have (j.approval_date is not null)
from job j
join customer c on j.customer_id = c.customer_id
group by c.customer_name

The code below gives syntax error:

,count(approval_date is not null) as is_approved
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
Adam Levitt
  • 10,316
  • 26
  • 84
  • 145

1 Answers1

2

You could use conditional sum:

select   
  c.customer_name
  ,count(*) as counts
  ,SUM(CASE WHEN j.approval_date is not null THEN 1 ELSE 0 END)
from job j
join customer c on j.customer_id = c.customer_id
group by c.customer_name;
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275