I have following data for Example model:
id | object_id | value | updated_at
------------------------------------------------------
1 | 1 | 200 | 2021-04-19 01:00:00
2 | 2 | 1200 | 2021-04-08 01:00:00
3 | 3 | 9000 | 2021-04-07 01:00:00
4 | 1 | 100 | 2021-04-10 01:00:00
5 | 2 | 900 | 2021-04-13 01:00:00
6 | 3 | 8000 | 2021-04-12 01:00:00
I want to get the latest updated rows grouped by object_id, for example:
id | object_id | value | updated_at
------------------------------------------------------
1 | 1 | 200 | 2021-04-19 01:00:00
5 | 2 | 900 | 2021-04-13 01:00:00
6 | 3 | 8000 | 2021-04-12 01:00:00
How can I achieve that result using Eloquent?
do you try?
$users = User::orderBy('updated_at','DESC')
->groupBy('object_id')
->get();
Related
This is my product table.I want to store customer_id from 1000 and save by +1 how much data i stored
id | customer_id | name |
1 | 1000 | ABC |
2 | 1001 | Tripathi |
3 | 1002 | Leaptrig |
4 | 1003 | Falcon |
5 | 1004 | Savillan |
6 | 1005 | Molt |
7 | 1006 | Falt |
My Controller
$lastProduct=Product::pluck('customer_id')->last();
$product=new Product();
$product->name=$request->name;
if($lastProduct){
$product->customer_id=1000+($lastProduct+1);
}
$product->save();
But In this code,Customer id i increment by 1000 2001,3002 like this. so how should i avoid it ?
id | customer_id | name |
1 | 1000 | ABC |
2 | 2001 | Tripathi |
3 | 3002 | Leaptrig |
4 | 4003 | Falcon |
5 | 5004 | Savillan |
6 | 6005 | Molt |
7 | 7006 | Falt |
You can try this :-
$lastProduct=Product::pluck('customer_id')->last();
$product=new Product();
$product->name=$request->name;
if($lastProduct){
$product->customer_id=$lastProduct+1;
}
$product->save();
I want to select data from say 2nd week of a particular month and I have a column containing dates, then, how should I go with it.
I have data as such -
+-----+---------------------+-------------+
| id | date | daily_cases |
+-----+---------------------+-------------+
| 1 | 2020-01-30 00:00:00 | 1 |
| 2 | 2020-01-31 00:00:00 | 0 |
| 3 | 2020-02-01 00:00:00 | 0 |
| 4 | 2020-02-02 00:00:00 | 1 |
| 5 | 2020-02-03 00:00:00 | 1 |
| 6 | 2020-02-04 00:00:00 | 0 |
| 7 | 2020-02-05 00:00:00 | 0 |
| 8 | 2020-02-06 00:00:00 | 0 |
| 9 | 2020-02-07 00:00:00 | 0 |
| 10 | 2020-02-08 00:00:00 | 0 |
I have an Oracle DB View like:
DATE | PRODUCT_NUMBER | PRODUCT_COUNT | PRODUCT_FACTOR
2018-01-01 | 1 | 10 | 3
2018-03-15 | 1 | 8 | 3
2019-02-11 | 1 | 11 | 3
2019-08-01 | 1 | 5 | 3
2019-08-01 | 2 | 20 | 5
2019-08-02 | 2 | 15 | 5
2019-06-01 | 2 | 5 | 5
2020-07-01 | 2 | 30 | 5
2018-07-07 | 3 | 100 | 2
Where,
DATE is the date
NUMBER is a unique Product Number
COUNT is the number of items from the Product Number in the storage facility
FACTOR is the number of products that fit into a storage rack
I now need to know how much it changed since the last update for every Product Number.
Since the first entry has no past date to compare to, change is undefined and something like NULL, NONE, 0 or so. Doesn't matter as long as I can filter those out later.
Some products only have 1 entry, those should be ignored (nothing to calculate difference on).
End result should be:
DATE | PRODUCT_NUMBER | PRODUCT_COUNT | PRODUCT_FACTOR | PRODUCT_CHANGE | CHANGE_FACTOR
2018-01-01 | 1 | 10 | 3 | NULL | NULL
2018-03-15 | 1 | 8 | 3 | 2 # 10-8 | 6 # 2*3
2019-02-11 | 1 | 11 | 3 | -3 # 8-11 | -9 # 3*-3
2019-08-01 | 1 | 5 | 3 | 6 # 11-5 | 18 # 6*3
2019-08-01 | 2 | 20 | 5 | -15 # 5-20 | -75 # -15*5
2019-08-02 | 2 | 15 | 5 | 5 # 20-15 | 25 # 5*5
2019-06-01 | 2 | 5 | 5 | NULL | NULL
2020-07-01 | 2 | 30 | 5 | -15 # 15-30 | -75 # -15*5
How can I achieve this within Oracle SQL?
End result is a bit unclear:
Why for product_number 2 15 and 5 values are compared - 2019-06-01 is less than 2019-08-01 and should be first row
Why change_factor for product 1 on the first row is 3 and for product 2 it's null
Why change_factor for 2019-02-11 is calculated as 11 * 0 instead of 0 * 3
Assumming all of this as typos(changed 2019-06-01 to 2019-09-01) you can use something like below
select dt, product_number, product_count, product_factor, product_change, product_change*product_factor change_factor
from (
select "DATE" dt, product_number, product_count, product_factor,
greatest(lag(product_count) over(partition by product_number order by "DATE") - product_count, 0) product_change
from test_tab t1
where (select count(1) from test_tab t2 where t1.product_number = t2.product_number and rownum < 3) > 1
)
fiddle
See also LAG documentation
I have read about this already in SO and MariaDB knowledgeable about this incompatibility between Mysql and Mariadb. But I am not sure how to resolve this issue in Laravel Eloquent / DB queries.
My Problem: The groupBy orderBy query gives different results in MariaDB and MySql. It works fine in mySql by the results are in different order in MariaDB.
This is my query:
$messages = ChatMessages::select(DB::raw('t.*'))
->from(DB::raw('(SELECT * FROM chat_messages ORDER BY created_at DESC) t'))
->whereIn('message_id', $messageIds)
->groupBy('message_id')
->orderBy('created_at', 'DESC')
->paginate(3);
For example, lets say this is the chat_messages table:
+----+----------+---------------------+-----------+
| id | message_id | created_at | name |
+----+----------+---------------------+-----------+
| 1 | 1000 | 2017-01-01 06:03:40 | Anna |
+----+----------+---------------------+-----------+
| 2 | 1007 | 2017-01-02 07:13:20 | Becky |
+----+----------+---------------------+-----------+
| 3 | 1000 | 2017-01-03 08:20:12 | Christina |
+----+----------+---------------------+-----------+
| 4 | 1004 | 2017-01-03 08:20:15 | Dorothy |
+----+----------+---------------------+-----------+
| 5 | 1004 | 2017-01-04 09:25:45 | Emma |
+----+----------+---------------------+-----------+
| 6 | 1000 | 2017-01-05 10:30:10 | Fiona |
+----+----------+---------------------+-----------+
| 7 | 1007 | 2017-01-05 10:33:23 | Gigi |
+----+----------+---------------------+-----------+
| 8 | 1007 | 2017-01-06 12:46:34 | Heidi |
+----+----------+---------------------+-----------+
| 9 | 1000 | 2017-01-06 12:46:34 | Irene |
+----+----------+---------------------+-----------+
| 10 | 1007 | 2017-01-07 14:58:37 | Jane |
+----+----------+---------------------+-----------+
| 11 | 1007 | 2017-01-07 14:58:37 | Katy |
+----+----------+---------------------+-----------+
The query works fine in MySql database and the results are returned as this:
+----+----------+---------------------+-----------+
| id | message_id | created_at | name |
+----+----------+---------------------+-----------+
| 11 | 1007 | 2017-01-07 14:58:37 | Katy |
+----+----------+---------------------+-----------+
| 9 | 1000 | 2017-01-06 12:46:34 | Irene |
+----+----------+---------------------+-----------+
| 5 | 1004 | 2017-01-04 09:25:45 | Emma |
+----+----------+---------------------+-----------+
However, in MariaDB database, the results are returned incorrectly like this. It seems to group the message_id in ascending order first and then adding the orderBy to that:
+----+----------+---------------------+-----------+
| id | message_id | created_at | name |
+----+----------+---------------------+-----------+
| 4 | 1004 | 2017-01-03 08:20:15 | Dorothy |
+----+----------+---------------------+-----------+
| 2 | 1007 | 2017-01-02 07:13:20 | Becky |
+----+----------+---------------------+-----------+
| 1 | 1000 | 2017-01-01 06:03:40 | Anna |
+----+----------+---------------------+-----------+
I tried changing the query thought of using unique() instead like this:
ChatMessages::whereIn('message_id', $messageIds)
->orderBy('created_at', 'DESC')
->paginate(3)
->unique('message_id');
Although it works in MariaDB and MySql the same way, but the pagination is applied before the unique check and therefore returned lesser results:
+----+----------+---------------------+-----------+
| id | message_id | created_at | name |
+----+----------+---------------------+-----------+
| 11 | 1007 | 2017-01-07 14:58:37 | Katy |
+----+----------+---------------------+-----------+
| 9 | 1000 | 2017-01-06 12:46:34 | Irene |
+----+----------+---------------------+-----------+
How can I resolve this?
You are probabbly trying to do a "groupwise max". This can no longer be done by the trick of having a subquery with an ORDER BY.
A subquery, but definition, has no order. However, in the past, both MariaDB and MySQL would perform the ORDER BY, and that happened to be beneficial to the outer query.
MariaDB was first to ignore the inner ORDER BY; MySQL picked up on it later. Follow the tag [greatest-n-per-group] for various workarounds.
Say i have 3 or 4 table which some table are connected with parent table. I want to show group by last record.
Table: table1
--------------------------------------------------------------------
| table1Id(AI)(PK) | date | tagid | blah3 | blah3 | blah4 |
--------------------------------------------------------------------
1 | 2016-05-01 | 101 |
2 | 2016-05-04 | 102 |
3 | 2016-05-10 | 101 |
4 | 2016-05-15 | 101 |
5 | 2016-05-04 | 103 |
6 | 2016-05-20 | 101 |
But when i do query group by tagid it will retrieve first row
--------------------------------------------------------------------
| table1Id(AI)(PK) | date | tagid | blah3 | blah3 | blah4 |
--------------------------------------------------------------------
1 | 2016-05-04 | 101 |
2 | 2016-05-04 | 102 |
5 | 2016-05-04 | 103 |
What i want to look like this
--------------------------------------------------------------------
| table1Id(AI)(PK) | date | tagid | blah3 | blah3 | blah4 |
--------------------------------------------------------------------
6 | 2016-05-20 | 101 |
2 | 2016-05-04 | 102 |
5 | 2016-05-04 | 103 |
My query like this
$this->db->select('*');
$this->db->from('tablename1');
$this->db->join('tablename2', 'tablename2.tagid= tablename1.tagid', 'left');
$this->db->group_by('tablename1.tagId, tablename2.tagId');
$this->db->order_by('tablename1.tagId','asc');
Use sub query. See below code
$this->db->select('*');
$this->db->from('tablename1');
$this->db->order_by('table1Id','DESC');
$subquery = $this->db->get_compiled_select();
$this->db->select('*');
$this->db->from('('.$subquery.') a');
$this->db->join('tablename2', 'tablename2.tagid= a.tagid', 'left');
$this->db->group_by('a.tagId, tablename2.tagId');
$this->db->order_by('a.tagId','asc');