I need help with query or way to do with single query in yii2.
Application is in yii2 , working on admin dashboard. I need to show the Avg days of all products in stock from today date that has been in system which are enabled (in short average age of inventory that is in stock).
NOTE : Just a single avg total required for column to show count on dashboard as a result.
I have a table with column stock_date
, that stores in DateTime format Y-m-d H:i:s
.
Formula
- ( STOCK_DATE - TODAY DATE ) will give you age of single product in system.
- Total SUM of column `stock_date' / Total number of products that are enabled will give you average days.
Table columns:
- inventory_id (primary key)
- status (1 for enabled)
- stock_date (datetime)
$AvgStatsCount = Inventory::find()
->select('sum(stock_date ) as stockdate')
....
->all();
Anyone know how to do that in right way ?
Posting Answer : working code for me :
$query = new Query;
$query->from('mytable')->where(['status' => 1]);
$AvgStatsCount = $query->average('DATEDIFF(stock_date, CURRENT_DATE())');