Unable to find sum of total quantity with first shipment date - mysql

I have below table where against the quantity I have Expected date of delivery. I need to find the nearest date and sum of quantity that will be shipped in.
Shipment table:-
Item
EDD
Quantity
A
15-Jul
20
A
15-Jul
25
A
18-Jul
15
B
20-Jul
10
B
20-Jul
10
C
25-Jul
5
C
28-Jul
7
The expected out should be
Item
date
Quantity
A
15-Jul
45
B
20-Jul
20
C
25-Jul
5
I have tried below query
SELECT
t2.item
t2.date,
t1.Quantity
FROM ( SELECT
item,
min(EDD) as date
FROM table
GROUP BY item
) AS t2
LEFT JOIN( SELECT
item,
min(EDD) as date,
Quantity
FROM table
GROUP BY item,
Quantity
) AS t1 ON (t2.item = t1.item AND t2.date = t1.date)
the output I am getting is
A - 15-Jul - 20
B - 20-Jul - 10
C - 25-Jul - 5
If I add sum(Quantity) anywhere in the query the result becomes
A - 15-Jul - 60
B - 20-Jul - 20
C - 25-Jul - 12
Need help to rectify the above issue.
Thanks

Assuming table name is shipment
select item, edd, sum(quantity) from shipment
where (item,edd) in (
select item, min(edd) date from shipment group by item
)
group by item, edd
Second version with join (always worth to check which has better execution plan)
select s.item, s.edd, sum(s.quantity) from shipment s
join (
select item, min(edd) min_date from shipment group by item
) m on m.item = s.item and m.min_date = s.edd
group by s.item, s.edd

Related

Aggregate information from one table to another with a different “layout” (mysql)

this is my starting table which provides sales information by Id.
Id
Store_Name
Market
Sales
Main_Product
1
StoreA
Rome
10
a
2
StoreB
Rome
15
b
3
StoreC
Rome
9
c
4
Mag1
Paris
10
a
5
Mag2
Paris
23
b
6
Mag3
Paris
12
c
7
Shop1
London
11
a
8
Shop2
London
31
b
9
Shop3
London
45
c
10
Shop4
London
63
d
In order to build a report and create some dynamic sentences, I will need the dataset to be "paginated" as per below table:
Id
Dimension
Dimension_Name
Sales
Main_Product
1
ShoppingCentre
StoreA
10
a
1
Market
Rome
34
a
2
ShoppingCentre
StoreB
15
b
2
Maket
Rome
34
b
3
ShoppingCentre
StoreC
9
c
3
Market
Rome
34
c
Do you have any tip about how to build the last table starting from the first one?
To sum-up:
The new table will be always by Id
Aggregation of market sales happens at row level where every single shopping centre is located
This is the query that I have built so far but wondering if there is a better and more efficient way to accomplish the same:
with store_temp_table as (
select
id
,Store_Name
,Market
, Main_Product
, sum(Sales) as Sales
from Production_Table
where 1=1
group by
1,2,3,4
)
, market_temp_table as (
select
market
, sum(Sales) as Sales
from Production_Table
where 1=1
group by
1
)
, store_temp_table_refined as(
Select
a.id
,a.Main_Product
, 'ShoppingCentre' as Dimension_Name
,SUM(a.Sales) as Sales
FROM store_temp_table a INNER JOIN
market_temp_table b on a.market = b.market
group by
1,2,3
)
, market_temp_table_refined as (
Select
a.id
,a.Main_Product
, 'Market' as DimensionName
,SUM(b.Sales) as Sales
FROM store_temp_table a INNER JOIN
market_temp_table b on a.market = b.market
group by
1,2,3
)
select * from store_temp_table_refined
union all
select * from market_temp_table_refined
Thank you
Use a CTE that returns the dimensions that you want and cross join it to a query that returns the columns of the table and an additional column with the total sales of each market:
WITH Dimensions(id, Dimension) AS (VALUES
ROW(1, 'ShoppingCentre'),
ROW(2, 'Market')
)
SELECT p.Id,
d.Dimension,
CASE d.id WHEN 1 THEN p.Store_Name ELSE p.Market END Dimension_Name,
CASE d.id WHEN 1 THEN p.Sales ELSE p.MarketSales END Sales,
p.Main_Product
FROM Dimensions d
CROSS JOIN (SELECT *, SUM(Sales) OVER (PARTITION BY Market) AS MarketSales FROM Production_Table) p
ORDER BY p.id, d.id;
Or, with UNION ALL:
SELECT Id,
'ShoppingCentre' Dimension,
Store_Name Dimension_Name,
Sales,
Main_Product
FROM Production_Table
UNION ALL
SELECT Id,
'Market',
Market,
SUM(Sales) OVER (PARTITION BY Market),
Main_Product
FROM Production_Table
ORDER BY Id,
CASE Dimension WHEN 'ShoppingCentre' THEN 1 WHEN 'Market' THEN 2 END;
See the demo.

MySQL How to get the row with max date when joining multiple tables?

My goal is the get a list of current prices and prices at the time of whatever date is given. The price as of today is always product.price. Each time a new price is set, an entry is added to product_audit and revinfo.
If we are looking for what the prices were on 2020-11-31, it would return:
num CurrentPrice OldPrice
--------------------------------------
1001 100 175
1030 110 100
2010 150 130
EDIT FOR CLARIFICATION: My intention is to get what the price was on a specific day. So OldPrice is actually the newest entry in Product_aud/revinfo that is before or on the set date (in this case, 2020-11-31). Looking specifically at code 1001, the price was changed on 2020-08-02, 2020-09-26, and 2020-01-08. If we are looking at 2020-11-31, that means it should grab 2020-09-26 because it is the soonest date before then. This means the price of 1001 on 2020-11-31 was 175.
There are three tables: Product, product_audit, revinfo
Everytime the price is changed, an entry is added to product_audit with the new price and a reference to a new entry in revinfo that has the date/time. Revinfo contains entries for other audit tables mixed in.
product.id = product_audit.id
product_audit.rev = revinfo.id
product
id num price
------------------------
1 1001 100
2 1030 110
3 2010 150
product_audit
id rev price
------------------------
1 1 200
1 3 175
1 6 100
2 2 100
2 7 110
3 4 130
3 5 120
3 8 150
revinfo
id timestamp
-------------------
1 2020-08-02
2 2020-09-25
3 2020-09-26
4 2020-11-12
5 2020-12-20
6 2021-01-08
7 2021-01-09
8 2021-01-23
Of course this just returns the oldest price from product_audit:
SELECT product.num, product.price AS CurrentPrice, product_audit.price AS OldPrice
FROM product
LEFT JOIN product_audit ON product_audit.id = product.id
LEFT JOIN revinfo ON revinfo.id = product_audit.rev
WHERE rev.timestamp <= "2020-11-31"
GROUP BY product.id
I tried nesting joins like this based on some stuff I was reading, but quickly realized it still wasn't going to get the right price:
SELECT product.id, product.num, product.price AS CurrentPrice, revisions.price AS OldPrice
FROM product
LEFT JOIN (SELECT product_audit.id AS id, product_audit.price AS price, MAX(revinfo.timestamp) AS timestamp
FROM product_audit
LEFT JOIN revinfo ON product_audit.rev = revinfo.id
WHERE revinfo.timestamp <= $DATE{Date}
GROUP BY product_aud.id) AS revisions ON revisions.id = product.id
I can't seem to think of how to get to that last step. Some sort of WHERE timestamp = (SELECT...) maybe? But I haven't been able to figure that out.
Also, just a heads up, I'm limited to statements that start with SELECT because of permissions. I can't add functions or anything like that.
I had to assume how we were getting the "old" price, and my assumption was that you wanted the "earliest" revision record, so I used Row_number and a derived table to get that record and then use it in the join constraint for the revision table... not exactly sure what your logic is, but here is a fiddle with the resultset that matches your "desired results"
SELECT product.num, product.price AS CurrentPrice, product_audit.price AS OldPrice
FROM product
LEFT JOIN (select p.price, p.id, p.rev,
ROW_NUMBER() over (partition by p.id order by p.rev asc) as rn
From product_audit p
) AS product_audit ON product_audit.id = product.id
and product_audit.rn = 1
LEFT JOIN revinfo ON revinfo.id = product_audit.rev
WHERE revinfo.timestamp <= '2020-11-31';
https://www.db-fiddle.com/f/fbvrgo2gRLoBPhgwQnuvY9/3
WITH cte AS ( SELECT product.num,
product.price CurrentPrice,
product_audit.price OldPrice,
ROW_NUMBER() OVER (PARTITION BY product.num
ORDER BY revinfo.`timestamp` DESC) rn
FROM product
JOIN product_audit ON product_audit.id = product.id
JOIN revinfo ON revinfo.id = product_audit.rev
WHERE revinfo.`timestamp` <= #date
)
SELECT num, CurrentPrice, OldPrice
FROM cte
WHERE rn = 1;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a276ec8ad89e3c2f3aaeee411072fa3e

SQL : Show date when product was present in 6 months of data

I am pretty new to sql but have a problem I can't seem to find an answer to yet.
I need to find the month where the number of months a product in a certain segment showed up in a report is equal to 6. However, a product may not show up in sequential months, shows up multiple times a month, and some products have not beensold in 6 months yet.
I have a database with the following attributes:
Entry_Id Product_Name Sold_Date Payment_Amount Segment
======================================================================
112341 ABC Product 2017/12/20 10.50 Segment 1
112342 123 Product 2016/08/21 11.20 Segment 1
112343 ABC Product 2017/12/20 11.50 Segment 1
112344 123 Product 2017/08/21 11.20 Segment 1
112345 123 Product 2017/06/12 11.20 Segment 1
112346 123 Product 2016/06/21 11.20 Segment 1
112347 123 Product 2016/05/02 11.20 Segment 1
112348 123 Product 2015/04/01 11.20 Segment 1
112348 123 Product 2018/01/05 11.20 Segment 1
I would like to get something to the following effect
Product_Name Date where N = 6 segment
=================================================
ABC Product N/A Segment 1
123 Product 2018/01/05 Segment 1
The Day of month does not matter, just the month where the the number of months it has shown up in is equal to 6.
This is my first question and I will be as active as possible, please ask any clarifying questions.
Thank you!
Use GROUP BY and COUNT() to count the number of months that each product was sold in, and HAVING to filter the results.
SELECT t1.product_name, max_date, segment
FROM yourTable AS t1
JOIN (
SELECT product_name, MAX(sold_date) AS max_date
FROM yourTable
GROUP BY product_name
HAVING COUNT(DISTINCT YEAR(sold_date), MONTH(sold_date)) >= 6
) AS t2 ON t1.product_name = t2.product_name AND t1.sold_date = t2.max_date
The t2 subquery finds all the products that were sold in at least 6 different months. Joining that with the table finds the row with the last date, so its segment column can be selected.
If you want to include the products that weren't sold in at least 6 months, with N/A in that column, you can move the test out of the subquery.
SELECT product_name, IF(month_count >= 6, max_date, "N/A") AS last_date, segment
FROM yourTable AS t1
JOIN (
SELECT product_name, MAX(sold_date) AS max_date, COUNT(DISTINCT YEAR(sold_date), MONTH(sold_date)) as month_count
FROM yourTable
GROUP BY product_name
) AS t2 ON t1.product_name = t2.product_name AND t1.sold_date = t2.max_date
You can do this with a correlated subquery:
select t.*
from t
where 6 = (select count(distinct year(t2.sold_date), month(t2.sold_date))
from t t2
where t2.segment = t.segment and and t2.product = t.product and
t2.sold_date <= t.sold_date
);
Because of the count(distinct), this will show all records from the 6th month. Also, this is not particularly efficient, but it does use just standard SQL.
You can summarize this to one row per segment/product:
select t.segment, t.product, min(t.sold_date)
from t
where 6 = (select count(distinct year(t2.sold_date), month(t2.sold_date))
from t t2
where t2.segment = t.segment and and t2.product = t.product and
t2.sold_date <= t.sold_date
)
group by t.segment, t.product;

How to get last item field from aggregate in mysql

I build query to get stock of each item from transaction data like this:
id id_item item_name qty multipler no_seal no_pallet Activity
---------------------------------------------------------------------------
1 23 Apple 20 1 SE002 PL123 IN
2 23 Apple 5 1 SE002 PL123 IN
3 23 Apple 10 -1 SE002 PL123 OUT
4 23 Apple 15 0 SE001 PL123 CHANGE_SEAL
5 23 Apple 15 0 SE001 PL055 CHANGE_PALLET
I try to get current stock of the item by query like this
SELECT id_item, item_name, SUM(qty * multipler) AS stock_qty, MAX(no_seal), MAX(no_pallet)
FROM item_transaction
GROUP BY id_item, item_name
id_item item_name qty no_seal no_pallet
-----------------------------------------------
23 Apple 15 SE002 PL123
but I have problem get last state of no_seal and no_pallet, aggregate function MAX() it give maximum value rather than last records,
I could use sub query to fetch max id of each item, but is it overkill to sum millions of data?
I want result stock of item id 23 (Apple) like this for no_seal and no_pallet
id_item item_name qty no_seal no_pallet
-----------------------------------------------
23 Apple 15 SE001 PL055
I'm sorry if my question is unclear, qty 15 come from
20 (in) + 5 (in) - 10 (out) + 0 (15 item change seal) + 0 (15 item change pallet) = 15
You could use an inner join of max id related to the item
SELECT
, a.item_name
, t.stock_qty
, a.no_seal
, a.no_pallet
from item_transaction a
inner join (
select max(id) max_id, SUM(a.qty * a.multipler) stock_qty, item_name
from item_transaction
group by item_name ) t on t.max_id = a.item_id and t.item_name = a.item_name
group by a.item_name
SELECT
*
FROM
(
SELECT id_item, item_name, SUM(qty * multipler) AS stock_qty, MAX(id) AS last_id
FROM item_transaction
GROUP BY id_item, item_name
)
summary
INNER JOIN
item_transaction
ON item_transaction.id = summary.last_id
If you want the last state for one stock item, then you can use
select id_item,
item_name,
no_seal,
no_pallet,
(select sum(qty * multipler) from item_transaction t2 where t2.id_item = t1.id_item) qty
from item_transaction t1
where id = (
select max(id)
from item_transaction t3
where id_item = 23
)
This solution assumes that id is unique and you want item_transaction with highest id.

Group BY product using MAX(price) or MAX(date) according to time interval

I've been searching for answers for 2 day and still nothing. Please, help me.
I have a database with products, product's prices and the date when this prices were registered:
product_id | price | date
-------------------------
1 | 8.95 | 2012-12-01
2 | 3.40 | 2012-12-01
1 | 9.05 | 2012-12-19
3 | 2.34 | 2012-12-24
3 | 2.15 | 2012-12-01
1 | 8.80 | 2012-12-19
1 | 8.99 | 2012-12-02
2 | 3.45 | 2012-12-02
Observe that is possible to have different price values for a product on the same day (rows 3 and 6). This is because there are many suppliers for a single product. There is a supplier column on database too, but I found it irrelevant for the solution. You can add it to the solution if I'm wrong.
Basically what I want is to write a query that returns two combined sets of data, as follow:
First set is made by minimum price of products inserted in the last month. As today is jan, 15, query should read rows 3, 4 and 6, apply the minimum price, and return only rows 4 and 6, both with minimum price for that product on the last month.
Second set is made by last products inserted, with no price registered on last month. i.e, for products not shown in the first set, query should search for the last inserted ones.
I hope that is clear. Ask me more if it isn't.
The query result for this database should be:
product_id | price | date
-------------------------
1 | 8.80 | 2012-12-19 <-Min price for product 1 on last month
3 | 2.34 | 2012-12-24 <-Min price for product 3 on last month
2 | 3.45 | 2012-12-02 <-No reg for product 2 on last month, show last reg.
I've tried everything: UNION, (DATE_SUB(CURDATE(), INTERVAL 1 MONTH), MIN(price), MAX(date) etc, etc. Nothing works. I don't know where to search now, please help me.
(SELECT product_id, MIN(price), date
FROM products
WHERE date + INTERVAL 1 MONTH > NOW()
GROUP BY product_id)
UNION
(SELECT product_id, price, MAX(date)
FROM products
WHERE product_id NOT IN (SELECT product_id
FROM products
WHERE date + INTERVAL 1 MONTH > NOW()
GROUP BY product_id)
GROUP BY product_id)
This should work but I'm not sure it's the most optimized way to do it.
something like this will do the trick:
SELECT * FROM (
SELECT DISTINCT b.product_id, IF (c.min IS NULL,(SELECT ROUND(e.price,2) FROM products AS e WHERE e.product_id = b.product_id ORDER BY e.date DESC LIMIT 1 ),c.min) AS min, IF (c.date IS NULL,(SELECT f.date FROM products AS f WHERE f.product_id = b.product_id ORDER BY f.date DESC LIMIT 1 ),c.date) AS date, IF(c.min IS NULL,'<-No reg for product 2 on last month, show last reg.','<-Min price for product 1 on last month') as text FROM products AS b
LEFT JOIN
(SELECT a.product_id, round(MIN(a.price),2) AS min, a.date FROM products AS a WHERE a.date BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND CURDATE() GROUP BY a.product_id) AS c
ON (b.product_id = c.product_id)
) AS d
ORDER BY d.text, d.product_id
Gives output:
product_id|min|date|text
1|8.80|2012-12-19|<-Min price for product 1 on last month
3|2.34|2012-12-24|<-Min price for product 1 on last month
2|3.45|2012-12-02|<-No reg for product 2 on last month, show last reg.
Break it down into several sub-queries:
Products with prices in the last month, min price
join in date for that price
UNION
Products with no-prices in the last month, max date
join in price on that date
SQL Fiddle
Here
Query
SELECT MINPRICE.product_id, P.date, MINPRICE.price
FROM
(
-- Min price in last 31 days
SELECT product_id, MIN(price) AS price
FROM Prices
WHERE DATEDIFF(CURDATE(), date) < 31
GROUP BY product_id
) MINPRICE
-- Join in to get the date that the price occured on
INNER JOIN Prices P ON
P.product_id = MINPRICE.product_id
AND
P.price = MINPRICE.price
UNION
SELECT MAXDATE.product_id, MAXDATE.date, P.price
FROM
(
-- Product with no price in last 31 days - get most recent date
SELECT product_id, MAX(date) AS date
FROM Prices
WHERE product_id NOT IN
(
SELECT product_id
FROM Prices
WHERE DATEDIFF(CURDATE(), date) < 31
)
) MAXDATE
-- join in price on that date
INNER JOIN Prices P ON
P.product_id = MAXDATE.product_id
AND
P.date = MAXDATE.date
Not that I tested but you can try...
SELECT * FROM
(SELECT * FROM (
SELECT * FROM table ORDER BY date DESC)
as tmp GROUP BY product_id) t1
LEFT JOIN
(SELECT * FROM (
SELECT * FROM table WHERE date => CURDATE() ORDER BY price)
as tmp2 GROUP BY product_id) t2
ON t1.product_id = t2.product_id