Languages

node-postgres - difference in the date notation

0 points
Asked by:
Tehya-Blanchard
354

I wanted to group the rows in my database by day, but when I query heidiSQL and then node.js, I get a difference in the results - the days are different in both cases. How to fix it?

Results in HeidiSQL:

Β 

Results in Node.js:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚          month           β”‚ count β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚    0    β”‚ 2021-02-28T23:00:00.000Z β”‚  '5'  β”‚
β”‚    1    β”‚ 2021-03-31T22:00:00.000Z β”‚  '7'  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

My code:Β 

const { Client } = require('pg');

const client = new Client({
    host: '127.0.0.1',
    user: 'my_username',
    database: 'my_database',
    password: 'my_password',
    port: 5432,
});

const fetchEventsGroupedByMonths = async () => {
	const query = `
            SELECT
                DATE_TRUNC('month', "event_timestamp") AS "month",
                COUNT(*)
            FROM "events"
            GROUP BY DATE_TRUNC('month', "event_timestamp")
    `;
    await client.connect();
    try {
        const { rows } = await client.query(query);
        return rows;
    } finally {
        await client.end();
    }
};

fetchEventsGroupedByMonths()
    .then(result => console.table(result))
    .catch(error => console.error(error.stack));
0 answers
Native Advertising
πŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

β€οΈπŸ’» πŸ™‚

Join