简体   繁体   中英

Using coalesce with sequelize on node.js

I have the following query:

select coalesce(round(sum(vdamesatual) / 
       (select count(*)
        from feriados
        where month(data)=month(current_date) and 
        day(data)<day(current_date) and
        diadasemana in(2,3,4,5,6) and
        feriadonacional=0 and 
        uf <> (select distinct uforigem from baseprodutos where Empresa = 'test'))
       * 
       (select count(*)
        from feriados
        where month(data)=month(current_date) and 
        day(data)>=day(current_date) and
        diadasemana in(2,3,4,5,6) and
        feriadonacional=0 and 
        uf <> (select distinct uforigem from baseprodutos where Empresa = 'test'))),0) as projecao
from baseprodutos
where Empresa = 'test' and
UFDest = 'uf' and
Fabricante = 'sample';

As you can see I have a coalesce right after the first select, and I am having trouble finding the right way to write this with sequelize; Would you have some suggestion?

I don't really follow the logic of your query, but the rule in Sequelize is that you can call any function with sequelize.fn() so a simple example of using COALESCE in Sequelize to get a value from 2 columns with an alias would be like this in your attributes :

[[sequelize.fn('COALESCE', mysql.col('col_name'), mysql.col('col_2_name')), 'alias'], 'another_col']

You can include another function if needed, like SUM by doing like this:

 [[sequelize.fn('COALESCE', sequelize.fn('SUM', (mysql.col('col_name'), mysql.col('col_2_name'))), (some other code here ...)),'alias']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM