Group users by month for last year in Lucid - group-by

I am working on a project on AdonisJs and Lucid ORM where i need to show a chart with user registration count grouped by every month (created_at -> month name) for the last year.
I did this query in Laravel eloquent but didn't find a solution for how to do this query in Lucid.
Please note: I'm using PostgreSQL and CockroachDB
User table columns
id | username | email | created_at | updated_at
PostgreSQL query
SELECT
to_char(date_trunc('month', created_at), 'YYYY, Month') AS created_month,
COUNT(id) AS total_registrations,
FROM users
GROUP BY date_trunc('month', created_at)
ORDER BY date_trunc('month', created_at)
I really need to know how I can do the same using Lucid ORM.
I'll appreciate any help from you. Best regards!

Related

How to export multiple queries from DataGrip

I have a file that contains multiple select queries.
I run these on a weekly basis to generate internal company reports
For example (these are not the actual queries):
-- Select number of users
SELECT count(id) from users;
-- Select number of active users
SELECT count(id) from users where active = true;
-- Select number of logins this week
SELECT count(id) from users where last_login > current_date - interval '1 week';
--- etc...
Is there a way, in DataGrip, to run all of these queries and export them to csv files?
I am able to run each query and then export the dataset, however, is a time consuming process doing this one by one.
I believe there isn't a convenient way.
As a workaround, will combining all queries into one solve your problem?
It's not the most elegant way, but you'll get all the data displaying in a single result:
SELECT
(SELECT COUNT(id) FROM users) AS "Number of users",
(SELECT COUNT(id) FROM users WHERE active = TRUE) as "Number of active users",
(SELECT COUNT(id) FROM users WHERE last_login > CURRENT_DATE - INTERVAL '1 week') as "Number of logins this week"
It's possible. Select all queries and then use Execute To Files:

Reorganize the data by months in grafana using psql

im trying to do a "AVG" by months but grafana wont let me do it because im using two Selects and the first one is using the other select and i can`t specifie my time column.
I want to get a "time series"(type of grafana dashboard) where it show´s me the average by month in grafana but i dont know how could i do it with psql and the code i have.
This is the code im using:
SELECT AVG(lol), NOW() as time FROM
(SELECT COUNT(DISTINCT(ticket_id)), SUM(time_spent_minutes) AS "lol"
FROM ticket_messages
WHERE admin_id IN ('20439', '20457', '20291', '20371', '20357', '20235','20449','20355','20488')
GROUP BY ticket_id) as media
Where is the temporal column coming from? As of now your query is not doing anything specifically.
I would think that probably you have a ticket_date column available somewhere, the below query could become
SELECT
EXTRACT(MONTH FROM ticket_date) ticket_month,
SUM(time_spent_minutes)/COUNT(DISTINCT ticket_id) avg_time
FROM ticket_messages
WHERE
admin_id IN ('20439', '20457', '20291', '20371', '20357', '20235','20449','20355','20488')
GROUP BY EXTRACT(MONTH FROM ticket_date)

How to group by month off a timestamp field from Redshift in Superset

I am trying to show some trend over month in Superset from a table which has a timestamp field called created_at but have no idea how to get it right.
The SQL query generated from this is the followings:
SELECT
DATE_TRUNC('month', created_at) AT TIME ZONE 'UTC' AS __timestamp,
SUM(cost) AS "SUM(cost)"
FROM xxxx_from_redshift
WHERE created_at >= '2017-01-01 00:00:00'
AND created_at <= '2018-07-25 20:42:13'
GROUP BY DATE_TRUNC('month', created_at) AT TIME ZONE 'UTC'
ORDER BY "SUM(cost)" DESC
LIMIT 50000;
Like I mentioned above, I don't know how to make this work and 2nd question is why ORDER BY is using SUM(cost)? If this is a time-series, shouldn't it use ORDER BY 1 instead? I tried to change Sort By but to no avail.
It is quite silly but I found that SUM(cost) doesn't work while sum(cost) works. It is a bug in Superset and will be addressed in https://github.com/apache/incubator-superset/pull/5487

Calculate previous order date and status in Postgres

I have a simple table of orders, and I need to calculate some stats for each order. Essentially I have a Postgres db with fields:
Order_ID (unique), User_ID, Created_at (date), City, Total
I want to write a query that will generate, for each Order_ID:
1) the Created_at date of the user's most recent order prior to the current Order_ID (so if a customer placed order with Order_ID=200005b on 9/20/14, what is the date of that user's most recent previous order?)
2) another field showing a user's "Status" based on this date, given the following cases:
-- if this is user's first order, Status="new";
-- if most recent previous order date <= 60 days before the given/current order, Status="active";
-- if most recent previous order date > 60 days before the given/current order, Status="reactivated"
I think there's a way to write this query using some nested SELECTS, and maybe a self-join, but I don't know PostgreSQL well enough to understand the ordering of queries. I have been able to generate an "Order_N" field using the following query that I could use to lookup (Order_N)-1 to find the date, but I get stuck once trying to use that in nesting.
SELECT
user_id,
order_id,
created_at,
row_number() over (partition by user_id order by created_at ) as order_n
order by user_id, created_at;
Does anyone have any ideas?

Converting a MySQL GROUP BY to Postgres

I'm working out a query that I've ran successfully in MySQL for a while, but in Postgres it's not working with the ole -
ERROR: column "orders.created_at" must appear in the GROUP BY clause
or be used in an aggregate function
Here's the query:
SELECT SUM(total) AS total, to_char(created_at, 'YYYY/MM/DD') AS order_date
FROM orders
WHERE created_at >= (NOW() - INTERVAL '2 DAYS')
GROUP BY to_char(created_at, 'DD')
ORDER BY created_at ASC;
It's just supposed to return something like this:
total | order_date
---------+------------
1099.90 | 2013/01/15
650.00 | 2013/01/16
4399.00 | 2013/01/17
The main thing is I want the sum grouped by each individual day of the month.
Anyone have ideas?
UPDATE:
The reason I'm grouping by day is because the graph will be labeled with each day of the month, and the total sales for each.
1st - $3400.00
2nd - $2237.00
3rd - $1489.00
etc.
I'm not sure why you're doing a conversion there. I think the better thing to do would be this:
SELECT
SUM(total) AS total,
created_at::date AS order_date
FROM
orders
WHERE
created_at >= (NOW() - INTERVAL '2 DAYS')
GROUP BY
created_at::date
ORDER BY
created_at::date ASC;
I would recommend this query and then format the daily labels in your graph through the graph settings to ensure you do not have any weird issues of the same day in different months getting grouped. However, to get what you display in your edit you can do this:
SELECT
SUM(total) AS total,
to_char(created_at, 'DDth') AS order_date
FROM
orders
WHERE
created_at >= (NOW() - INTERVAL '2 DAYS')
GROUP BY
to_char(created_at, 'DDth')
ORDER BY
to_char(created_at, 'DDth') ASC;
Here is the sql you need in order to run this. The group by and order by need to contain the same expression.
SELECT SUM(total) AS total,
to_char(created_at, 'YYYY/MM/DD') AS order_date
FROM orders
WHERE created_at >= (NOW() - INTERVAL '2 DAYS')
GROUP BY to_char(created_at, 'YYYY/MM/DD')
order by to_char(created_at, 'YYYY/MM/DD')
http://sqlfiddle.com/#!12/52d99/2
Hope this helps,
Matt