I followed this post Order by using month name in PostgreSQL but not success!
I have a query (its working) and I just need to order the results by mont name. This is thr query I am using:
select to_char(purchase_date, 'Month') as mes_2021,
sum(gmv::float4) as soma_gmv
from tablename
where purchase_date > '2021-01-01'
GROUP BY mes_2021
I am trying:
order by to_date(purchase_date, 'Month') - No success
order by date_part(purchase_date::date, 'Month') - No success
If i use order by mes_2021
One trick is to use a window function on the date:
select to_char(purchase_date, 'Month') as mes_2021,
sum(gmv::float4) as soma_gmv
from tablename
where purchase_date > '2021-01-01'
group by mes_2021
order by min(purchase_date);
This, of course, assumes that the dates are all in the same year. But your where clause is taking care of that.
Related
I have a table like this, I hope to count the number of ids by month. I used the following code but it does not work.
id date_time
1390880502018723840,2021-05-08
1390881127930372100,2021-05-08
1390881498270736386,2021-05-08
SELECT twitter.tweets.id
WHERE Month(twitter.tweets.date_time)=01 AND Year(twitter.tweets.date_time)=2021 ;
you have to use count() function and to_char to get year month part of date in one column:
SELECT count(witter.tweets.id)
WHERE to_char(twitter.tweets.date_time,'YYYY-MM')= '2021-01';
you can generalize it for all the month/year by using group by :
SELECT to_char(twitter.tweets.date_time,'YYYY-MM') , count(witter.tweets.id)
group by to_char(twitter.tweets.date_time,'YYYY-MM');
To get counts for all months since Jan 2021:
SELECT date_trunc('month', date_time), count(*)
FROM twitter.tweets
WHERE date_time >= '2021-01-01'
GROUP BY 1;
If id can be NULL (which should be disallowed for an id column), use the slightly more expensive count(id) instead.
Count of distinct IDs:
SELECT date_trunc('month', date_time), count(DISTINCT id)
FROM twitter.tweets
WHERE date_time >= '2021-01-01'
GROUP BY 1;
For only Jan 2021:
SELECT count(DISTINCT id)
FROM twitter.tweets
WHERE date_time >= '2021-01-01'
WHERE date_time < '2021-02-01';
I am using the following to transfrom the date variable to YYYY and Mon
select
to_char(date, 'Mon') as month,
extract(year from date) as year,
v1,
count(v2) as count
from data
group by 1,2,3
order by 2,1;
However, when I do an order by, it does not order my data properly. So I think I have to convert Mon either to a number or back to a date to be able to sort it properly. Thoughts?
Of course not. It is ordering by the month name.
Instead, just order by the date:
order by date
If this were an aggregation query, then you would use an aggregation function:
order by min(date)
I would use a derived table where the grouping is done on a proper date value and convert it only for display purposes:
select to_char(month_date, 'Mon' as month,
extract(year from month_date) as year,
v1,
count
from (
select date_trunc('month', "date") as month_date
v1,
count(v2) as count
from data
group by 1,2
)
order by month_date
I'm learning Postgresql. I have this table with dates(DateTime type) and orders. My goal is to display the amount of orders done in a month, but I also need to show the names of the months in order.
I'm able to do the first part but I'm having trouble figuring out how to order the months, since I've already converted them from dates to strings. So this is what I've been doing:
SELECT TO_CHAR(created_at, 'Month') as month_name, COUNT(id) as orders
FROM orders
WHERE aasm_state != 'cart'
GROUP BY month_name
ORDER BY to_date(month_name, 'Month')
Then, I get "ERROR: column "month_name" does not exist".
I know the problem is in the last line because if I take it out I get no error.
Thanks in advance.
You could change to this. As for postgreSQL, you could use column alias in GROUP BY clause to "save" your typing, although for general it should not be supported.
SELECT TO_CHAR(created_at, 'Month') as month_name, COUNT(id) as orders
FROM orders
WHERE aasm_state != 'cart'
GROUP BY date_part('month', created_at), TO_CHAR(created_at, 'Month') --, month_name
ORDER BY date_part('month', created_at);
Or try this
SELECT TO_CHAR(created_at, 'Month') as month_name, COUNT(id) as orders
FROM orders
WHERE aasm_state != 'cart'
GROUP BY TO_CHAR(created_at, 'Month')
ORDER BY EXTRACT(MONTH FROM to_date(TO_CHAR(created_at, 'Month') || ' 2019', 'Month YYYY'));
Your order by can just use the column alias:
SELECT TO_CHAR(created_at, 'Month') as month_name, COUNT(id) as orders
FROM orders
WHERE aasm_state <> 'cart'
GROUP BY month_name
ORDER BY month_name;
The problem in your query is the expression on month_name. You would have still gotten a type-conversion error (I think) if the code ran.
So, month_name can be used in ORDER BY. However, it cannot be used in an expression.
The simplest way to order chronologically is:
order by min(created_at)
I have db with names etc with date of birth. How can I get count of columns for all 12 months of the dates?
Exact code depends on the database you use; you should, somehow, "extract" month from date of birth in order to GROUP BY it.
In Oracle, you might have done it as
select to_char(date_of_birth), 'mon') dob_month,
count(*)
from your_table
group by to_char(date_of_birth, 'mon');
or
select extract(month from date_of_birth) dob_month,
count(*)
from your_table
group by extract(month from date_of_birth);
I'm trying to make a simple query to find the average car sales per month from a table called "salestransaction".
My code:
select to_char(st.dateofsale, 'MON') as mnth, count(*) / 2 as Average_Car_Sales
from salestransaction st
group by to_char(st.dateofsale, 'MON')
order by to_char(st.dateofsale, 'MON');
My order by month is not outputting the correct order.
What can I do to make it output by month? starting from JAN - DEC?
Thank you.
An easy approach is to add month number to group by, then order by this field:
select to_char(st.dateofsale, 'MON') as mnth,
count(*) / 2 as Average_Car_Sales
from salestransaction st
group by EXTRACT(month FROM st.dateofsale),
to_char(st.dateofsale, 'MON')
order by EXTRACT(month FROM st.dateofsale);
If you try to order without aggregate function or without adding month number in group by expression then you will get ORA-00979: not a GROUP BY expression error
To avoid Extract you can use to_char with MM pattern:
select to_char(st.dateofsale, 'MON') as mnth,
count(*) / 2 as Average_Car_Sales
from salestransaction st
group by to_char(st.dateofsale, 'MM'),
to_char(st.dateofsale, 'MON')
order by to_char(st.dateofsale, 'MM');
My recommendation:
order by min(st.dateofsale);
That is, just pull a value out for each group and use that for the ordering.
If you have data from multiple years, the above might not work. Instead:
order by min(extract(month from st.dateofsale))
It is because you order them according to the month name in this case, december comes before january. You need to get their numeric values.
Here is an example from oracle documantation:
SELECT EXTRACT(month FROM order_date) "Month",
COUNT(order_date) "No. of Orders"
FROM orders
GROUP BY EXTRACT(month FROM order_date)
ORDER BY EXTRACT(month FROM order_date)
You can check it from this link for more detailed information:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm