首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将SQL查询(使用子查询连接)转换为KnexJS

将SQL查询(使用子查询连接)转换为KnexJS
EN

Stack Overflow用户
提问于 2018-12-19 13:14:03
回答 1查看 689关注 0票数 0

我正在尝试将SQL查询转换为KnexJS格式,但当前的KnexJS查询显示以下错误。

  • 在"as“堆栈或附近出现语法错误: error:在"as"

或附近出现语法错误

以下是原始查询,也是我一直在为KnexJS处理的查询。请更正我的KnexJS查询。基本上,我想知道如何构建KnexJS查询-内部连接子查询提前谢谢!

原始SQL查询:

代码语言:javascript
复制
select DATE_RANGE.START_DATE, DATE_RANGE.END_DATE, count (distinct DATE) as DATE_COUNT
from TASK_HISTORY
join
(select 
STORE_ID, 
to_number(to_char(to_date(to_char(DATE,'99999999'),'YYYYMMDD') - 1,'YYYYMMDD'),'99999999') as END_DATE
, count (distinct DATE) as REC_COUNT
, to_number(to_char(to_date(to_char(lag (DATE) over (order by DATE asc),'99999999'),'YYYYMMDD') + 1,'YYYYMMDD'),'99999999') as START_DATE
, count (case when FINISH_TIME is not null then 1 end) as COUNT_FINISHED
, count (case when FINISH_TIME is null then 1 end) as COUNT_UNFINISHED
  from TASK_HISTORY
  where STORE_ID = 43
  group by DATE, STORE_ID
  having count (case when FINISH_TIME is not null then 1 end) = 0
  order by DATE)
  as DATE_RANGE
on TASK_HISTORY.DATE >= DATE_RANGE.START_DATE 
    AND TASK_HISTORY.DATE <= DATE_RANGE.END_DATE
    AND TASK_HISTORY.STORE_ID = 43
group by DATE_RANGE.START_DATE, DATE_RANGE.END_DATE, DATE_RANGE.REC_COUNT
order by DATE_COUNT desc, START_DATE desc

更新:

以下是对我有效的解决方案:

代码语言:javascript
复制
    await db
      .table("task_history")
      .select('date_range.start_date', 'date_range.end_date')
      .select(db.raw(`count(distinct date) as date_count`))
      .join(
        db
        .select('task_history.store_id')
        .table('task_history')
        .select(db.raw(
          `to_number(to_char(to_date(to_char(date,'99999999'),'YYYYMMDD') - 1,'YYYYMMDD'),'99999999') as end_date`
        ))
        .select(db.raw(`count(distinct date) as rec_count`))
        .select(db.raw(
          `to_number(to_char(to_date(to_char(lag (date) over (order by date asc),'99999999'),'YYYYMMDD') + 1,'YYYYMMDD'),'99999999') as start_date`
        ))
        .select(db.raw(`count(case when FINISH_TIME is not null then 1 end) as COUNT_FINISHED`))
        .select(db.raw(`count(case when FINISH_TIME is null then 1 end) as COUNT_UNFINISHED`))
        .where('task_history.store_id', 43)
        .groupBy('task_history.date', 'task_history.store_id')
        .having(db.raw(`count(case when FINISH_TIME is not null then 1 end) = 0 order by date`))
        .as('date_range'),
        function () {
          this.on('task_history.date', '>=', 'date_range.start_date')
            .andOn('task_history.date', '<=', 'date_range.end_date')
            .andOn('task_history.store_id', 43)
        }
      )
      .groupBy('date_range.start_date', 'date_range.end_date', 'date_range.rec_count')
      .orderBy('date_count', 'desc')
      .orderBy('start_date', 'desc')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-19 23:14:38

这可能会对你有帮助-

代码语言:javascript
复制
const sql = db.table("task_history")
   .select('DATE_RANGE.START_DATE', 'DATE_RANGE.END_DATE')
   .select(db.raw(`count(distinct DATE) as DATE_COUNT`))
   .innerJoin(
      db.select('store_id')
         .table('task_history')
         .select(db.raw(
            `to_number(to_char(to_date(to_char(DATE,'99999999'),'YYYYMMDD') - 1,'YYYYMMDD'),'99999999') as END_DATE`
         ))
         .select(db.raw(`count(distinct DATE) as REC_COUNT`))
         .select(db.raw(
            `to_number(to_char(to_date(to_char(lag (DATE) over (order by DATE asc),'99999999'),'YYYYMMDD') + 1,'YYYYMMDD'),'99999999') as START_DATE`
         ))
         .select(db.raw(`count(case when FINISH_TIME is not null then 1 end) as COUNT_FINISHED`))
         .select(db.raw(`count(case when FINISH_TIME is null then 1 end) as COUNT_UNFINISHED`))
         .where('store_id', 43)
         .groupBy('date', 'store_id')
         .having(db.raw(`count(case when FINISH_TIME is not null then 1 end) = 0 order by DATE`))
         .as('DATE_RANGE')
      , function () {
         this.on('DATE_RANGE.START_DATE', '>=', 'TASK_HISTORY.DATE')
            .andOn('TASK_HISTORY.DATE', '<=', 'DATE_RANGE.END_DATE')
            .andOn('TASK_HISTORY.STORE_ID', 43)
      })
   .where('task_history.date', '>=', 'DATE_RANGE.START_DATE')
   .where('task_history.date', '<=', 'DATE_RANGE.END_DATE')
   .groupBy('DATE_RANGE.START_DATE', 'DATE_RANGE.END_DATE', 'DATE_RANGE.REC_COUNT')
   .orderBy('DATE_COUNT', 'desc')
   .orderBy('START_DATE', 'desc')
   .toSQL();
console.log(sql);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53844960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档