sql - Is it possible to use 'case' with and in 'count'? -
is possible use case
, in count
select branches.name agence, count( case loanstatus when '1' , datepart(month,loanaccount.issuedate)= 2 1 else null end )as nombre_de_credits_demande , count( case loanstatus when '2' datepart(month,loanaccount.chargeoffdate)= 2 1 else null end )as nombre_de_credits_approuve
please me
you can use count()
. prefer sum()
:
select branches.name agence, sum(case when loanstatus = '1' , datepart(month, loanaccount.issuedate) = 2 1 else 0 end ) nombre_de_crédits_demandé , sum(case when loanstatus = '2' , datepart(month, loanaccount.issuedate) = 2 1 else 0 end ) nombre_de_crédits_approuvé
the issue code not count()
versus sum()
mixing of 2 different case
syntaxes. when use case <var> when <val>
, cannot include other conditions. use when
full conditions want.
and, if like, can use count()
instead of sum()
.
and, conciseness, prefer month()
function:
select branches.name agence, sum(case when loanstatus = '1' , month(loanaccount.issuedate) = 2 1 else 0 end ) nombre_de_crédits_demandé , sum(case when loanstatus = '2' , month(loanaccount.issuedate) = 2 1 else 0 end ) nombre_de_crédits_approuvé
Comments
Post a Comment