Group orders by date - sql

I have this table food_order which contains data like:
ID food_name order_id order_month
1 apple 123 2017-02
2 apple 345 2017-11
3 pear 4656 2017-02
4 orange 5778 2017-09
5 apple 454 2017-02
What I need to have is a table that shows the number of orders for a specific food in a specific month.
I.e
ID food name nr_orders month
1 apple 2 2017-02
I need to do this using joins and no other functions over there.
Help would be appreciated

Here you could try SQL Query for Microsoft SQL Server :
SELECT MIN(id) [ID],
food_name [food name],
COUNT(order_id) [nr_orders],
order_month
FROM #TM
GROUP BY food_name,
order_month
ORDER BY ID;
Result :
ID food name nr_orders order_month
1 apple 2 2017-02
2 apple 1 2017-11
3 pear 1 2017-02
4 orange 1 2017-09
Note : order_month datatype is VARCHAR

You can use like this
SELECT food_name, SUM(quantity) as quantity, SUM(amount) as amount, MONTH(date) as Month
FROM food_order
GROUP BY food_name, MONTH(date)

Related

Computing window functions for multiple dates

I have a table sales of consisting of user id's, products those users have purchased, and the date of purchase:
date
user_id
product
2021-01-01
1
apple
2021-01-02
1
orange
2021-01-02
2
apple
2021-01-02
3
apple
2021-01-03
3
orange
2021-01-04
4
apple
If I wanted to see product counts based on every users' most recent purchase, I would do something like this:
WITH latest_sales AS (
SELECT
date
, user_id
, product
, row_number() OVER(PARTITION BY user_id ORDER BY date DESC) AS rn
FROM
sales
)
SELECT
product
, count(1) AS count
FROM
latest_sales
WHERE
rn = 1
GROUP BY
product
Producing:
product
count
apple
2
orange
2
However, this will only produce results for my most recent date. If I looked at this on 2021-01-02. The results would be:
product
count
apple
2
orange
1
How could I code this so I could see counts of the most recent products purchased by user, but for multiple dates?
So the output would be something like this:
date
product
count
2021-01-01
apple
1
2021-01-01
orange
0
2021-01-02
apple
2
2021-01-02
orange
1
2021-01-03
apple
1
2021-01-03
orange
2
2021-01-04
apple
2
2021-01-04
orange
2
Appreciate any help on this.
I'm afraid the window function row_number() with the PARTITION BY user_id clause is not relevant in your case because it only focusses on the user_id of the current row whereas you want a consolidate view with all the users.
I dont have a better idea than doing a self-join on table sales :
WITH list AS (
SELECT DISTINCT ON (s2.date, user_id)
s2.date
, product
FROM sales AS s1
INNER JOIN (SELECT DISTINCT date FROM sales) AS s2
ON s1.date <= s2.date
ORDER BY s2.date, user_id, s1.date DESC
)
SELECT date, product, count(*)
FROM list
GROUP BY date, product
ORDER BY date
see the test result in dbfiddle

SQL select top 2 customers with most purchases for each item and display their most recent purchase date

I would like to identify the top 2 users by purchase volume and when their last order was, for each item, from this dataset
customer_id item created_at revenue
1 apple 3/3/2022 23
2 pear 3/18/2022 21
3 apple 3/18/2022 76
4 banana 3/18/2022 62
1 apple 3/28/2022 33
3 peanut 3/29/2022 62
1 banana 3/7/2022 52
4 peanut 3/13/2022 17
1 peanut 3/30/2022 29
2 banana 3/17/2022 12
3 peanut 3/31/2022 26
2 apple 3/4/2022 12
purchase volume can be inferred from the revenue. Please help!
Quick solution ( not validated for syntax )
select t.customer_id , t.item , max(created_at) as last_order_dt
from
table_name t JOIN
(
select TOP 2 customer_id, sum(revenue)
from table_name
group by customer_id
order by sum(revenue) desc
) a
t.customer_id = a.customer_id

How to find out first product item client purchased whose bought specific products?

I want to write a query to locate a group of clients whose purchased specific 2 product categories, at the same time, getting the information of first transaction date and first item they purchased. Since I used group by function, I could only get customer id but not first item purchase due to the nature of group by. Any thoughts to solve this problem?
What I have are transaction tables(t), customer_id tables(c) and product tables(p). Mine is SQL server 2008.
Update
SELECT t.customer_id
,t.product_category
,MIN(t.transaction_date) AS FIRST_TRANSACTION_DATE
,SUM(t.quantity) AS TOTAL_QTY
,SUM(t.sales) AS TOTAL_SALES
FROM transaction t
WHERE t.product_category IN ('VEGETABLES', 'FRUITS')
AND t.transaction_date BETWEEN '2020/01/01' AND '2022/09/30'
GROUP BY t.customer_id
HAVING COUNT(DISTINCT t.product_category) = 2
**Customer_id** **transaction_date** **product_category** **quantity** **sales**
1 2022-05-30 VEGETABLES 1 100
1 2022-08-30 VEGETABLES 1 100
2 2022-07-30 VEGETABLES 1 100
2 2022-07-30 FRUITS 1 50
2 2022-07-30 VEGETABLES 2 200
3 2022-07-30 VEGETABLES 3 300
3 2022-08-01 FRUITS 1 50
3 2022-08-05 FRUITS 1 50
4 2022-08-07 FRUITS 1 50
4 2022-09-05 FRUITS 2 100
In the above, what I want to show after executing the SQL query is
**Customer_id** **FIRST_TRANSACTION_DATE** **first_product_category** **TOTAL_QUANTITY** **TOTAL_SALES**
2 2022-07-30 VEGETABLES, FRUITS 4 350
3 2022-07-30 VEGETABLES 5 400
Customer_id 1 and 4 will not be shown as they only purchased either vegetables or fruits but not both
Check now, BTW need find logic with product_category
select CustomerId, transaction_date, product_category, quantity, sales
from(
select CustomerId, transaction_date, product_category , sum(quantity) over(partition by CustomerId ) as quantity , sum(sales) over(partition by CustomerId ) as sales, row_number() over(partition by CustomerId order by transaction_date ASC) rn
from(
select CustomerId, transaction_date, product_category, quantity, sales
from tablee t
where (product_category = 'FRUITS' and
EXISTS (select CustomerId
from tablee tt
where product_category = 'VEGETABLES'
and t.CustomerId = tt.CustomerId)) OR
(product_category = 'VEGETABLES' and
EXISTS (select CustomerId
from tablee tt
where product_category = 'FRUITS'
and t.CustomerId = tt.CustomerId)))x)over_all
where rn = 1;
HERE is FIDDLE

How to SUM column if same value

I have query to SUM and COUNT my table, but i have trouble, i want to SUM column name after COUNT it.
This is my table...
id no_reg name date qty
1 REG01 T-212-BS 2019-05-03 1
2 REG01 T-212-BS 2019-05-03 1
3 REG01 T-212-BS 2019-05-03 1
4 REG01 T-212-BA 2019-05-03 1
5 REG02 T-111-AA 2019-05-04 1
6 REG03 T-111-AB 2019-05-04 1
I create query....
SELECT no_reg, COUNT(DISTINCT name) AS Name_qty, date, SUM(qty) AS qty
FROM part
GROUP BY no_reg, name, date, qty
and result of query after execution...
no_reg Name_qty date qty
REG01 1 2019-05-03 1
REG01 1 2019-05-03 3
REG02 1 2019-05-04 1
REG03 1 2019-05-04 1
But, I want results like this...
no_reg Name_qty date qty
REG01 2 2019-05-03 4
REG02 1 2019-05-04 1
REG03 1 2019-05-04 1
No need to group by name, even if you're using it in your distinct statement.
SELECT no_reg, COUNT(DISTINCT name) AS Name_qty, date, SUM(qty) AS qty
FROM part
GROUP BY no_reg, date
You're grouping by qty, so any rows that do not have the same qty will be aggregated separately. Since qty is used in an aggregate function, you can remove it from the group by and it should give you the expected results
SELECT no_reg, COUNT(DISTINCT name) AS Name_qty, date, SUM(qty) AS qty
FROM part
GROUP BY no_reg, date
EDIT:
I also noticed that name was included in the group by. You can remove it too since it is used in the count aggregate

How to sum and subtract values using update with group by Sql query?

MyTable:
item_name Qty item_area
Belts 2 India
Shoes 20 US
T-Shirt 10 India
T-Shirt 12 US
T-Shirt 25 US
I get this by select group by query
SELECT item_name, Sum(item_qty) as Qty, item_area
FROM dbo.item_stocks
group by item_area, item_name
and my output is below:
item_name Qty item_area
Belts 2 India
Shoes 20 US
T-Shirt 10 India
T-Shirt 37 US
Now i need to subtract and update .How can i do this ?
For eg. i want to subtract 5 T-Shirt ,it will update in MyTable?
T-shirt=37-5=32
how can i update in MyTable?
You can try to use ROW_NUMBER window function to choose any one row to subtract.
;WITH CTE AS (
SELECT *,ROW_NUMBER() over(PARTITION BY item_name,item_area order by Qty desc)rn
FROM item_stocks
)
update CTE
set Qty = Qty - 5
where rn = 1 and item_name = 'T-Shirt' and item_area = 'US'
SELECT item_name, Sum(Qty) as Qty, item_area
FROM item_stocks
group by item_area, item_name
just add a column which denotes in/Out, say INOUT
don't update row, just add new row for the stock sale.
Here,
INOUT = 1 denotes IN and 2 denotes OUT
table structure like
item_name Qty item_area INOUT
Belts 2 India 1
Shoes 20 US 1
T-Shirt 10 India 1
T-Shirt 12 US 1
T-Shirt 25 US 1
T-Shirt 5 US 2
now your query looks like that
SELECT item_name,
Sum(case when INOUT = 1 then item_qty else (0-item_qty) end) as Qty,
item_area
FROM dbo.item_stocks
group by item_area, item_name