Hussein Farhat
Hussein Farhat

Reputation: 9

Having a Problem with Converting an SQL query to Knexjs

I am trying to convert a SQL

query to a knex js valid syntax, if anyone can help :

the SQL query is :

select * from vw_invoices_payments 
where concat(updated_at, id) in (
    select concat(max(updated_at), vw.id) 
    from vw_invoices_payments as vw group by id)

Upvotes: 0

Views: 132

Answers (1)

felixmosh
felixmosh

Reputation: 35493

In order to generate the inner query you can do this:

knex
  .from('vw_invoices_payments')
  .columns('*')
  .whereIn(
    knex.raw('concat(updated_at, id)'),
    knex
      .from('vw_invoices_payments as vw')
      .columns(knex.raw('concat(max(updated_at), vw.id)'))
      .groupBy('id')
  );

Upvotes: 2

Related Questions