Extracting entries from a specific month in SQL - sql

I'm trying to calculate total revenues from each customer purchase in March from a table called orders, where order_date is in format of datetime (e.g. 2019-03-04). However, I'm unsuccessful to obtain the results with either of the following options:
WITH cte_orders AS
(
SELECT
cust_id,
SUM(order_quantity * order_cost) AS revenue
FROM
orders
WHERE
DATEPART('month', order_date) = 3
GROUP BY
cust_id
)
SELECT cust_id, revenue
FROM cte_orders;
also
WHERE EXTRACT(month from order_date) = '03'
or
WHERE MONTH(order_date) = 03
doesn't work either. What's wrong with my syntax here?

Thanks everyone for the input, finally figured out the right way to do this:
WITH cte_orders AS
(
SELECT
cust_id,
SUM(order_quantity * order_cost) AS revenue,
FROM
orders
WHERE
EXTRACT('MONTH' FROM order_date :: TIMESTAMP) = 3
GROUP BY
cust_id
)
SELECT cust_id, revenue
FROM cte_orders;
with this it converted the date to timestamp and extracted March as required.

What about date_part?
WITH cte_orders AS
(select cust_id
, SUM(order_quantity * order_cost) as revenue
from orders
WHERE date_part('month',TIMESTAMP order_date) = 3
group by cust_id)
select cust_id, revenue from cte_orders;

Did you consider putting the WHERE clause outside of the CTE?
WITH cte_orders AS
(
SELECT
cust_id,
SUM(order_quantity * order_cost) AS revenue
FROM
orders
WHERE
DATEPART('month', order_date) = 3
GROUP BY
cust_id
)
SELECT cust_id, revenue
FROM cte_orders
WHERE MONTH(order_date) = 03;
Also, it would be helpful if you let us know what (if any) results you are getting. Are you getting an error message or is it just not returning the expected values/row count?

Related

Calculating Year over Year Growth using PostgreSQL

I'm using a SQL dataset called Superstore and I want to figure out how to calculate year over year sales growth as a percentage. Here's the code I already have:
SELECT
EXTRACT(year FROM order_date) AS order_year,
SUM(sales) AS total_sales,
FROM orders
WHERE order_date BETWEEN date '2016-01-01' and date '2019-12-01'
GROUP BY 1
ORDER BY 1
Any help would be welcomed. Thank you.
I would aggregate in a subquery, then use a window function:
SELECT *
FROM (SELECT total_sales / lag(total_sales) OVER (ORDER BY order_year) * 100.0,
year
FROM (SELECT CAST (EXTRACT(year FROM order_date) AS integer) AS order_year,
SUM(sales) AS total_sales
FROM orders
GROUP BY 1) AS subq
) AS subq2
WHERE year = 2021;

Why two of my different sql queries that must perform the same result act different?

I need to get the number of new buyers who came in 1990 year.
The first query says it's 17, but the second says it's 29? So which one is wrong and why?
SELECT DISTINCT COUNT(customer_id) FROM SALES_ORDER WHERE
EXTRACT(YEAR FROM order_date) = 1990
AND
customer_id NOT IN (SELECT customer_id FROM SALES_ORDER WHERE EXTRACT(YEAR FROM order_date) < 1990);
SELECT DISTINCT COUNT(customer_id) FROM SALES_ORDER WHERE
customer_id IN (SELECT customer_id FROM SALES_ORDER WHERE EXTRACT(YEAR FROM order_date) = 1990)
AND
customer_id NOT IN (SELECT customer_id FROM SALES_ORDER WHERE EXTRACT(YEAR FROM order_date) < 1990);
Here is my data schema:
Both queries do not do what you want. You are using SELECT DISTINCT COUNT(customer_id), while you probably want SELECT COUNT(DISTINCT customer_id).
I find that the logic would be simpler expressed with two levels of aggregation:
select count(*)
from (
select customer_id
from sales_order
group by customer_id
having extract(year from min(order_date)) = 1990
) t

Return the number of new users by month

Table ORDERS contains the following columns
order_id, order_date, ship_date, ship_mode, customer_id, country, quantity
I just need the code. Here is my work.
SELECT order_date, COUNT (Customer_Id) As new_usercount
FROM (
SELECT Customer_Id, DATE(MIN(Order_Date)) AS order_date
FROM Orders
GROUP BY Customer_Id
) AS ch
GROUP BY order_date
Thanks
Your query is almost correct.
After you aggregate to get each customer's min order date you must group by the year and month of the dates and not the dates:
SELECT YEAR(order_date) year,
MONTH(order_date) month,
COUNT(*) new_usercount
FROM (
SELECT Customer_Id, MIN(Order_Date) order_date
FROM Orders
GROUP BY Customer_Id
) t
GROUP BY YEAR(order_date), MONTH(order_date)

How to write a SQL query to find out customers who have purchased at least two times every month of the year

I have a table with columns order_id, customer_id, order_date and Amount.
How to find the customers who have ordered at least 2 times each month of the year in SQL (from Jan to Dec)?
I think you are looking for something like this
select
order_date_year,
customer_id
from
(
select
year(order_date) as order_date_year,
month(order_date) as order_date_month,
customer_id,
count(*) as number_of_orders
from
tableName
group by
year(order_date),
month(order_date),
customer_id
having
count(*) >= 2
) as t
group by
order_date_year,
customer_id
having
count(*) = 12

SQL: aggregation (group by like) in a column

I have a select that group by customers spending of the past two months by customer id and date. What I need to do is to associate for each row the total amount spent by that customer in the whole first week of the two month time period (of course it would be a repetition for each row of one customer, but for some reason that's ok ). do you know how to do that without using a sub query as a column?
I was thinking using some combination of OVER PARTITION, but could not figure out how...
Thanks a lot in advance.
Raffaele
Query:
select customer_id, date, sum(sales)
from transaction_table
group by customer_id, date
If it's a specific first week (e.g. you always want the first week of the year, and your data set normally includes January and February spending), you could use sum(case...):
select distinct customer_id, date, sum(sales) over (partition by customer_ID, date)
, sum(case when date between '1/1/15' and '1/7/15' then Sales end)
over (partition by customer_id) as FirstWeekSales
from transaction_table
In response to the comments below; I'm not sure if this is what you're looking for, since it involves a subquery, but here's my best shot:
select distinct a.customer_id, date
, sum(sales) over (partition by a.customer_ID, date)
, sum(case when date between mindate and dateadd(DD, 7, mindate)
then Sales end)
over (partition by a.customer_id) as FirstWeekSales
from transaction_table a
left join
(select customer_ID, min(date) as mindate
from transaction_table group by customer_ID) b
on a.customer_ID = b.customer_ID