How can I convert this SQL query using COUNT, SUM, CASE, and BETWEEN into Perl DBIx::Class?
-
12-10-2019 - |
Pergunta
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, ?)
Solução
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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow