Question

How can I convert this SQL query into Perl DBIx::Class code?

SELECT COUNT(*) AS num_grads,
       SUM(CASE WHEN employment_status = 1 THEN 1 ELSE 0 END) AS num_employed
  FROM students
 WHERE status = 6  -- 6 means they graduated
   AND grad_date BETWEEN Convert(datetime, ?) AND Convert(datetime, ?)
Was it helpful?

Solution

Assuming that you mean DBIx::Class, then I'd probably go for something like:

my $rs = My::Schema->resultset('Students') #
    ->search({
        status => 6,
        grad_date => { 'between' => [ $start_dt, $end_dt ] },
    });

my $num_grads    = $rs->count();
my $num_employed = $rs->search({ employment_status => 1 })->count();

Of course the datetime bit will depend on your datetime formats, whether you are automatically inflating/deflating datetimes etc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top