【问题标题】:Using coalesce with sequelize on node.js在 node.js 上使用合并和续集
【发布时间】:2020-07-19 03:05:01
【问题描述】:

我有以下疑问:

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 编写此内容的正确方法;你有什么建议吗?

【问题讨论】:

    标签: mysql sql node.js sequelize.js coalesce


    【解决方案1】:

    我并没有真正遵循您查询的逻辑,但是 Sequelize 中的规则是您可以使用 sequelize.fn() 调用任何函数,因此在 Sequelize 中使用 COALESCE 从具有别名的 2 列中获取值的简单示例在你的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']]
    

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2014-05-28
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多