How to sort by month name - sql

I have such query:
SELECT MonthName(Month(Transaction_Date)), SUM(Sales)
FROM Sales
GROUP BY MonthName(Month(Transaction_Date))
ORDER BY MonthName(Month(Transaction_Date))
But results aren't sorted by date. How to sort the by month name: January, February, March etc?

One method is to include both in the GROUP BY:
SELECT MonthName(Month(Transaction_Date)), SUM(Sales)
FROM Sales
GROUP BY MonthName(Month(Transaction_Date)), Month(Transaction_Date)
ORDER BY Month(Transaction_Date);
Or just aggregate by MONTH():
SELECT MonthName(Month(Transaction_Date)), SUM(Sales)
FROM Sales
GROUP BY Month(Transaction_Date)
ORDER BY Month(Transaction_Date);
The MONTHNAME() is then applied after the aggregations.
Alternatively, if the Transaction_Dates are all from the same year, use aggregation functions:
SELECT MonthName(Month(Transaction_Date)), SUM(Sales)
FROM Sales
GROUP BY MonthName(Month(Transaction_Date))
ORDER BY MIN(Transaction_Date);
Note that you may also want to be including the year along with the month -- that is a best practice, because usually you don't want to mix data from different years in the same month.

Order the results by the month number as opposed to the month name, e.g.:
SELECT MonthName(Month(Transaction_Date)), Sum(Sales)
FROM Sales
GROUP BY MonthName(Month(Transaction_Date)), Month(Transaction_Date)
ORDER BY Month(Transaction_Date)

Ordering by the month name would be alphabetical but if you just order by Month(transaction_date) ASC/DESC that should order your results correctly.
SELECT MonthName(Month(Transaction_Date)), SUM(Sales)
FROM Sales
GROUP BY Month(Transaction_Date)
ORDER BY Month(Transaction_Date)

I guess you should order by transaction date.
SELECT
MonthName(Month(Transaction_Date))
As Month_Name , SUM(Sales)
FROM Sales
GROUP BY Month_Name
ORDER BY Transaction_Date

Try using this query:
SELECT MonthName(Month(Transaction_Date)), SUM(Sales)
FROM Sales
GROUP BY MonthName(Month(Transaction_Date))
ORDER BY Transaction_Date ASC
Note that :
ASC - for ascending A to Z order
DESC - for descending Z to A order

Related

RANK() over (PARTITION BY) To show only TOP 3 rows for each month

I have a question about ranking . (My using Pgadmin for my SQL codes)
Mange to get my sum of sales in DESC order and rank 1 to 3 for the month of APR
But how can I achieve my result by showing only rank 1 to 3 for the month of Apr , May and June.
I need to reflect only 9 rows in my table .
SELECT restaurant_id,
EXTRACT(year FROM submitted_on) AS year,
EXTRACT(month FROM submitted_on) AS month,
SUM(total_amount),
RANK() OVER (PARTITION BY(extract(month from submitted_on))
ORDER BY SUM(total_amount) DESC) rank
FROM orders
WHERE submitted_on::date BETWEEN '2021-04-01' AND '2021-06-30'
GROUP BY restaurant_id, year, month
If you just want 3 records you should use row_number instead of rank. for your requirement you can do it in this way:
select t.* from (
SELECT restaurant_id,
EXTRACT(year FROM submitted_on) AS year,
EXTRACT(month FROM submitted_on) AS month,
SUM(total_amount),
RANK() OVER (PARTITION BY(extract(month from submitted_on))
ORDER BY SUM(total_amount) DESC) rank
FROM orders
WHERE submitted_on::date BETWEEN '2021-04-01' AND '2021-06-30'
GROUP BY restaurant_id, year, month
) t
where rank <=3;

How do I write a query to find highest earning day per quarter?

I need to write SQL query to pull the single, highest-earning day for a certain brand of each quarter of 2018. I have the following but it does not pull a singular day - it pulls the highest earnings for each day.
select distinct quarter, order_event_date, max(gc) as highest_day_gc
from (
select sum(commission) as cm, order_date,
extract(quarter from order__date) as quarter
from order_table
where advertiser_id ='123'
and event_year='2018'
group by 3,2
)
group by 1,2
order by 2 DESC
You can use window functions to find the highest earning day per quarter by using rank().
select rank() over (partition by quarter order by gc desc) as rank, quarter, order_event_date, gc
from (select sum(gross_commission) gc,
order_event_date,
extract(quarter from order_event_date) quarter
from order_aggregation
where advertiser_id = '123'
and event_year = '2018'
group by order_event_date, quarter) a
You could create the query above as view and filter it by using where rank = 1.
You could add the LIMIT clause at the end of the sentence. Also, change the las ORDER BY clause to ORDER BY highest_day_gc. Something like:
SELECT DISTINCT quarter
,order_event_date
,max(gc) as highest_day_gc
FROM (SELECT sum(gross_commission) as gc
,order_event_date
,extract(quarter from order_event_date) as quarter
FROM order_aggregation
WHERE advertiser_id ='123'
AND event_year='2018'
GROUP BY 3,2) as subquery
GROUP BY 1,2
ORDER BY 3 DESC
LIMIT 1

SQL order with equal group size

I have a table with columns month, name and transaction_id. I would like to count the number of transactions per month and name. However, for each month I want to have the top N names with the highest transaction counts.
The following query groups by month and name. However the LIMIT is applied to the complete result and not per month:
SELECT
month,
name,
COUNT(*) AS transaction_count
FROM my_table
GROUP BY month, name
ORDER BY month, transaction_count DESC
LIMIT N
Does anyone have an idea how I can get the top N results per month?
Use row_number():
SELECT month, name, transaction_count
FROM (SELECT month, name, COUNT(*) AS transaction_count,
ROW_NUMBER() OVER (PARTITION BY month ORDER BY COUNT(*) DESC) as seqnum
FROM my_table
GROUP BY month, name
) mn
WHERE seqnum <= N
ORDER BY month, transaction_count DESC

Find max value for each year

I have a question that is asking:
-List the max sales for each year?
I think I have the starter query but I can't figure out how to get all the years in my answer:
SELECT TO_CHAR(stockdate,'YYYY') AS year, sales
FROM sample_newbooks
WHERE sales = (SELECT MAX(sales) FROM sample_newbooks);
This query gives me the year with the max sales. I need max sales for EACH year. Thanks for your help!
Use group by and max if all you need is year and max sales of the year.
select
to_char(stockdate, 'yyyy') year,
max(sales) sales
from sample_newbooks
group by to_char(stockdate, 'yyyy')
If you need rows with all the columns with max sales for the year, you can use window function row_number:
select
*
from (
select
t.*,
row_number() over (partition by to_char(stockdate, 'yyyy') order by sales desc) rn
from sample_newbooks t
) t where rn = 1;
If you want to get the rows with ties on sales, use rank:
select
*
from (
select
t.*,
rank() over (partition by to_char(stockdate, 'yyyy') order by sales desc) rn
from sample_newbooks t
) t where rn = 1;

Group by month and add year and employee

I have a simple table with every sale made in few past years. I would like to find out maximum sale per month and who made it and in which year and month.
Table has following columns:
Id, date, amount, employeeId
I group data by year(date), month(date), employeeId and use sum(amount) to find sale of each employee in each month. Then I group further by month(date) and use max on sum(amount) column to find maximum sale per month. This is easy.
After that I would like to find out when exactly (date) and who exactly (employeeId) made that particular sale.
Group data by year(date), month(date), employeeId and use sum(amount) to find sale of each employee in each month then order by sum(amount) desc. Your highest sellers will be at the top of the results.
Most databases support the ANSI standard rank() function. This may do what you want:
select s.*
from (select year(date) as yyyy, month(date) as mm, employeeid, sum(amount) as amount,
rank() over (partition by year(date), month(date)
order by sum(amount) desc
) as seqnum
from simpletable
) s
where seqnum = 1;
I think you can do this like, if I understand correctly:
select top 1 with ties year(date), month(date), employeeId
from TableName
group by year(date), month(date), employeeId
order by sum(amount) desc