mysql - 在 node.js 上使用合并和续集
问题描述
我有以下查询:
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 编写它;你有什么建议吗?
解决方案
我并没有真正遵循您的查询逻辑,但是 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']]
推荐阅读
- r - 在两个数据框之间的列中添加和减去值
- scrapy - 你可以在 Scrapy 的 POST 请求中使用布尔值吗?
- r - 在 geom_histogram 上绘制 geom_point
- python - Python如何以整数形式获取当前时间
- angular - 如何使用 Angular 制作可点击的通知 Firebase
- excel - Excel VBA中仅特定单元格范围的下拉列表选择?
- java - OOP Java如何增加生命值和减少mp?
- java - 无论 Serializable 接口实现如何,我都会收到 java.io.NotSerializableException
- spring-cloud-dataflow - 如何在 Spring Cloud Data Flow UI 上显示日志
- android - 在 Kotlin 中使用 Handler 类会使我的 android 应用程序崩溃