在node.js上使用coalesce和sequelize

zkure5ic  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(436)

我有以下疑问:

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';

正如你所看到的,我在第一次选择之后有一个合并,我很难找到正确的方法来用sequelize写这个;你有什么建议吗?

t5fffqht

t5fffqht1#

我并不真正遵循您的查询逻辑,但是sequelize中的规则是您可以使用 sequelize.fn() 因此,在sequelize中使用coalesce从带有别名的两列中获取值的简单示例如下所示 attributes :

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

如果需要,可以包含另一个函数,例如 SUM 这样做:

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

相关问题