can't use 'paginate' when together using group by on knex
depran7 opened this issue · comments
issued an error
select count(distinct
undefined
) astotal
fromtransaction
ast
limit 1
the version that I am using
"knex": "^0.20.4",
"knex-paginate": "^1.1.0",
this is my query
await knex.select('id').from('transaction as t').groupBy(knex.raw('date(t.created_at)')).orderBy('id','desc').paginate({perPage: 10, currentPage: 1})
is this a bug?
When group is applied, there is a need to count the grouped items, this is what it trying to do here:
https://github.com/felixmosh/knex-paginate/blob/master/lib/index.js#L53
Probably there is an issue with the usage of Knex.raw
, I will check it.
For the mean time, can you try this:
await knex
.select('id')
.from('transaction as t')
.groupBy('date(t.created_at)')
.orderBy('id', 'desc')
.paginate({ perPage: 10, currentPage: 1 });
When group is applied, there is a need to count the grouped items, this is what it trying to do here:
https://github.com/felixmosh/knex-paginate/blob/master/lib/index.js#L53Probably there is an issue with the usage of
Knex.raw
, I will check it.
For the mean time, can you try this:await knex .select('id') .from('transaction as t') .groupBy('date(t.created_at)') .orderBy('id', 'desc') .paginate({ perPage: 10, currentPage: 1 });
I tried it but if I don't use knex.raw it displays - SQLITE_ERROR: no such column: date (t.created_at)
I use knex.raw because it uses the date function from sqllite
Opps, my bad, I wrote it not properly.
I'm using Mysql 8, which has ONLY_FULL_GROUP_BY
on by default, which enforces me to specify all grouped columns.
Last try,
await knex
.select('id', knex.raw('date(t.created_at) as created_date'))
.from('transaction as t')
.groupBy('created_date')
.orderBy('id', 'desc')
.paginate({ perPage: 10, currentPage: 1 });
Anyway, I will check for the reason for the error.
@depran7, can you add please table schema + some data, so I'll able to reproduce your case?
wow it works, thanks for responding, I hope 'knex-paginate' will continue to be better
Can u provide some structure & data and what was your expected return data...
@depran7, can you add please table schema + some data, so I'll able to reproduce your case?
Can u provide some structure & data and what was your expected return data...
Yes I can, My expectation is to get total transaction data per day (daily transaction)
Examples:
2 feb 2020 is 20 transactions
3 feb 2020 is 30 transactions
etc...
this is the table
-- auto-generated definition
create table "transaction"
(
id integer not null
primary key autoincrement,
no_trs varchar(255),
subtotal float,
disc float,
grand_total float,
total_payment float,
change float,
created_at datetime,
updated_at datetime
);
this is some data
INSERT INTO "transaction" (no_trs,subtotal,disc,grand_total,total_payment,"change",created_at,updated_at) VALUES (
'ABC/001/110320/0001',468000,9900,458100,458100,0,'2020-03-11 17:14:09',NULL);
INSERT INTO "transaction" (no_trs,subtotal,disc,grand_total,total_payment,"change",created_at,updated_at) VALUES (
'ABC/001/120320/0001',99000,0,99000,100000,1000,'2020-03-12 18:05:25',NULL);
INSERT INTO "transaction" (no_trs,subtotal,disc,grand_total,total_payment,"change",created_at,updated_at) VALUES (
'ABC/001/120320/0002',669000,43500,625500,625500,0,'2020-03-12 18:20:40',NULL);
INSERT INTO "transaction" (no_trs,subtotal,disc,grand_total,total_payment,"change",created_at,updated_at) VALUES (
'ABC/001/120320/0003',471000,43500,427500,427500,0,'2020-03-12 18:26:12',NULL);
INSERT INTO "transaction" (no_trs,subtotal,disc,grand_total,total_payment,"change",created_at,updated_at) VALUES (
'ABC/001/120320/0004',768000,43500,724500,724500,0,'2020-03-12 18:31:49',NULL);
INSERT INTO "transaction" (no_trs,subtotal,disc,grand_total,total_payment,"change",created_at,updated_at) VALUES (
'ABC/001/130320/0001',443000,0,443000,443000,0,'2020-03-13 17:21:04',NULL);
Thanx to your issue, I've improved the lib 🙏🏼
Now it supports all kind of queries without any issue.