Aggregate SQL in mariadb 10.4

Asked 1 years ago, Updated 1 years ago, 78 views

I would like to do the following in mariadb 10.4.7.
There is an inventory control table that registers data according to the entry and exit of the item.

The inventory table looks like the following.

 | date | varchar | varchara | float |
Date of warehousing | Warehouse | Goods | Inventory |
20190101 | 001 | A01 | 1000 |
20190101 | 001 | A01 | -300 |
.
.
.

The above data contains a few minutes of movement.

This time, I would like to implement the following view.

 Warehouse or goods | January inventory | February inventory | March inventory | ... December inventory |
Key included | Inventory as of January | Inventory as of February | Inventory as of March | Inventory as of December |
.
.
.

As mentioned above, we would like to list the monthly inventory trends by product and warehouse as shown in the cross-summary table.
I've tried many things, but the result doesn't come out as I expected.
If possible, it would be better to create a view in SQL instead of cross-counting on the application side.
I would appreciate it if you could help me.

If I play the SQL below, I can do the aggregation monthly, but I don't know what SQL I'm going to do because I want to show the monthly changes.

SELECT a. Warehouse Code AS 'Total Key',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201901', a. Number of stock, 0))AS 'January',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201902', a. Number of stock, 0))AS 'February',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201903', a. Number of stock, 0))AS 'March',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201904', a. Number of stock, 0))AS 'April',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201905', a. Number of stock, 0))AS 'May',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201906', a. Number of stock, 0))AS 'June',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201907', a. Number of stock, 0))AS 'July',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201908', a. Number of stock, 0))AS 'August',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201909', a. Number of stock, 0))AS 'September',
SUM(if(date_format(a. Date of warehousing, '%Y%m') = '201910', a. Number of stock, 0))AS 'October',
SUM(if(date_format(a. date of entry and exit, '%Y%m')='201911', a. Number of stock, 0))AS 'November',
SUM(if(date_format(a. Date of warehousing, '%Y%m') = '201912', a. Number of stock, 0))AS 'December'
FROM Inventory Table ASa
GROUP BY a. WAREHOUSE CODE

The above is narrowed down in the warehouse, but I would like to do the same for the product.
Warehouses and products are varchara type and the date of arrival and departure is the date type.
Thank you for your cooperation.

php mysql sql database mariadb

2022-09-30 21:42

1 Answers

Logically, at the level of .

"Cumulative total?" means SUM for January, SUM for February, and SUM for January to February, right?
If this is OK,

"The conditions for the warehousing date are ""January 1st, 12:00 p.m. <= January 1st, 12:00 p.m. <Next month, ""January 1st, 12:00 p.m."""

What about asI don't think you need to use date_format.

There is a lack of information, so speed and so on are ignored.


2022-09-30 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.