I need a sum of unit for that devices 1,2 on the base of date but i'm trying with my query but result is not right.And i don't when having function is used.so please help me.My query,schema sample and expected result is given below
Database data
|--------------------------------------
|device_id | unit | date
|--------------------------------------
| 1 | 2 | 2016-07-01
---------------------------------------
| 1 | 3 | 2016-07-02
--------------------------------------
| 2 | 1 | 2016-07-02
--------------------------------------
| 2 | 4 | 2016-07-10
--------------------------------------
| 1 | 3 | 2016-07-10
-------------------------------------
| 2 | 1 | 2016-07-21
--------------------------------------
| 2 | 2 | 2016-07-23
--------------------------------------
| 1 | 3 | 2016-07-25
--------------------------------------
| 2 | 2 | 2016-07-27
--------------------------------------
| 1 | 3 | 2016-07-27
My query
$startMonth=Carbon::today()->startOfMonth()->toDateString();
$now=Carbon::today()->toDateString();
$consumption= PowerConsumption::groupBY(['date'])
->selectRaw('sum(unit) as yAxis,date')
->whereIn('device_id',[1,2])
->whereBetween('date',[$startMonth,$now])->get();
And Expected Result
[
{
yAxis:2,
date:2016-07-01
},
{
yAxis:4, //3+1
date:2016-07-02
},
{
yAxis:7, //4+3
date:2016-07-10
},
{
yAxis:1,
date:2016-07-21
},
{
yAxis:2,
date:2016-07-23
},
{
yAxis:3,
date:2016-07-25
},
{
yAxis:5, //2+3
date:2016-07-27
}
]
Please help me by modify my query and query return above expected result
Related
I have two tables as follows that joined by budget_id.
finance_budget table
+-----------+--------+------------+-----------------+---------------+
| budget_id | amount | date | transfer_status | budget_status |
+-----------+--------+------------+-----------------+---------------+
| 1 | 135000 | 2019-10-01 | Pending | issue |
| 2 | 25000 | 2019-10-02 | Pending | issue |
| 3 | 234000 | 2019-10-03 | Pending | issue |
| 4 | 175000 | 2019-10-03 | Pending | issue |
+-----------+--------+------------+-----------------+---------------+
finance_budget_issue table
+----+-----------+-----------+--------+-----------------+---------------+
| id | budget_id | office_id | amount | transfer_status | budget_status |
+----+-----------+-----------+--------+-----------------+---------------+
| 1 | 1 | 100 | 135000 | Pending | issue |
| 2 | 2 | 101 | 12500 | Pending | issue |
| 3 | 2 | 102 | 12500 | Pending | issue |
| 4 | 3 | 100 | 100000 | Pending | issue |
| 5 | 3 | 105 | 75000 | Pending | issue |
| 6 | 3 | 104 | 59000 | Pending | issue |
| 7 | 4 | 102 | 125000 | Pending | issue |
| 8 | 4 | 110 | 50000 | Pending | issue |
+----+-----------+-----------+--------+-----------------+---------------+
I tried to change the "transfer_status" of above two tables from "Pending" to "Approved" using my model
Controller
public function approveIssues($id){
$this->checkPermissions('index', 'pendingIssues');
if(empty($id)){
redirect('budget/pendingIssue');
}
if($this->Budget_model->approveIssues($id)){
$this->session->set_flashdata('message', 'Allocation Approved successfully ..!!');
redirect('budget/pendingIssue');
}else{
$this->session->set_flashdata('error', 'Error ...!!!');
redirect('budget/pendingIssue');
}
}
Model
function approveIssues($id)
{
$this->db->update('finance_budget_issue', array('transfer_status'=>'Approved'), array('id' => $id));
$this->db->update('finance_budget', array('transfer_status'=>'Approved'), array('budget_id' => $id));
if ($this->db->affected_rows()) {
$activity=FZ_Controller::activity('approve','finance_budget_issues',$id,NULL);
$this->db->insert('finance_user_activity',$activity);
return true;
}
return false;
}
When I press the Approved button in my view the system shows "Error". If I remove the line, $this->db->update('finance_budget', array('transfer_status'=>'Approved'), array('budget_id' => $id));
in my model the operation is executing successfully and shows the "Allocation Approved successfully ..!!" Message.
Desired Output
I also need the transfer_status of finance_budget table should be into "Approved".
You can try this Model Code
function approveIssues($id) {
$this->db->set('finance_budget.transfer_status','Approved');
$this->db->set('finance_budget_issue.transfer_status','Approved');
$this->db->where('finance_budget.budget_id', $id);
$this->db->where('finance_budget_issue.id', $id);
$this->db->update('finance_budget JOIN finance_budget_issue ON finance_budget.budget_id = finance_budget_issue.id');
if ($this->db->affected_rows()) {
$activity=FZ_Controller::activity('approve','finance_budget_issues',$id,NULL);
$this->db->insert('finance_user_activity',$activity);
return true;
}
return false;
}
i think it's very helpful for you.
I'm trying to retrieve data from many to many relation grouped in one object for the duplicated date.
I have menu table and daily_mealz table and pivot table(menu_daily_mealz)
the problem is the daily_mealz contain duplicated date value in its date column but every raw contain different meal_id.
So, I need to retrieve one raw but contain all mealsIDs related to this date
I only retrieve with belongsTo relation and them for loop over the data to get the object I need.
Relations
public function dailyMeals(){
return $this->belongsToMany(DailyMeals::class, 'menu_daily_meals', 'menu_id', 'daily_meal_id');
}
public function menus(){
return $this->belongsToMany(Menu::class, 'menu_daily_meals', 'daily_meal_id', 'menu_id');
}
DataBase Structure
Menu table
+-----+----------------+
| id | name |
+-----+----------------+
| 1 | first menu |
| 2 | second menu |
+-----+----------------+
daily mealz table
+----+-------------+---------+-------+
| id | date | meal_id | stock |
+----+-------------+---------+-------+
| 1 | 2019-03-01 | 1 | 250 |
| | | | |
| 2 | 2019-03-01 | 2 | 100 |
| | | | |
| 3 | 2019-03-02 | 3 | 150 |
| | | | |
| 4 | 2019-03-02 | 4 | 70 |
| | | | |
| 5 | 2019-03-03 | 5 | 350 |
| | | | |
| 6 | 2019-03-03 | 6 | 180 |
+----+-------------+---------+-------+
Menu_daily_meals table
+----+---------+---------------+
| id | menu_id | daily_meal_id |
+----+---------+---------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 1 | 5 |
| 6 | 1 | 6 |
| 7 | 2 | 3 |
| 8 | 2 | 5 |
| 9 | 2 | 6 |
+----+---------+---------------+
I need to retrieve object like that
{
"id": 1,
"name": "first menu",
"daily_meals": [
{
"id": 1,
"daily_date": "2019-03-01",
"meals" : [
{
"meal_id" : 1,
"stock" : 250
},
{
"meal_id" : 2,
"stock" : 100
},
]
},
{
"id": 2,
"daily_date": "2019-03-02",
"meals" : [
{
"meal_id" : 3,
"stock" : 150
},
{
"meal_id" : 4,
"stock" : 70
},
]
},
{
"id": 3,
"daily_date": "2019-03-03",
"meals" : [
{
"meal_id" : 5,
"stock" : 350
},
{
"meal_id" : 6,
"stock" : 180
},
]
}
]
}
Any Help, Please?
You should use load function to load external data to your model(use eager loading):
$menu=Menu::find(1);
$menu->load('dailyMeals','dailyMeals.meals');
Note : For hide pivot object in response add this code to your menu model:
protected $hidden = ['pivot'];
Note : You should have dailyMeals in your menu model and meals relation in your dailyMeals model
For more information You can Visit Laravel Document.
What I want to do is, the distinct id based on the max value of the associated column and then update a column accordingly.
For example
id | name | total | description | updateThis
1 | john | 100 | example | 0
2 | dave | 300 | example | 0
2 | johno | 500 | example | 0
4 | derik | 900 | example | 0
5 | Sam | 1000 | example | 0
4 | bool | 12200 | example | 0
1 | john | 1200 | example | 0
5 | john | 300 | example | 0
I want it to look like this:
id | name | total | description | updateThis
1 | john | 1200 | example | 1
2 | johno | 500 | example | 1
4 | bool | 12200 | example | 1
5 | Sam | 1000 | example | 1
I am using Query Builder not the eloquent way of doing it, either would be a useful answer and if someone can give both that would be awesome too, I just need help thanks.
You can use this:
$rows = DB::table('table')
->select('id', DB::raw('max(total) as total'))
->groupBy('id')
->get();
foreach($rows as $row) {
DB::table('table')
->where('id', $row->id)
->where('total', $row->total)
->update(['updateThis' => 1]);
}
I have table with data like this
code | month | sales | value
1 | 1 | 1 | 4
1 | 1 | 2 | 2
1 | 2 | 1 | 2
2 | 1 | 1 | 4
and I want to group the data like this
code | month | sales | value
1 | 1 | 1 | 6
1 | 1 | 2 | 2
2 | 1 | 1 | 4
so far I have this mysql query
SELECT code,month,sales,sum(value) as value FROM data GROUP BY code
but it only gives this result
code | month | sales | value
1 | 1 | 1 | 8
2 | 1 | 1 | 4
Any suggestion please?
These will give u expected result:
SELECT code,month,sales,sum(value) as value FROM data GROUP BY code, sales
I have a Facility Booking System and I would like to create a CSV format report.
1. I want to count total booking records
2. I want to count how many user booked our facilities, based on that timeslot
3. I want to combine two queries into one as I need to put the result into the CSV
And here is the table structure:
fbid | memberid | fbdate | fbtimeslot | fname | fposition | fattendance
------------------------------------------------------------------------------
1 | C00001 | 2014-12-12 | 1 | computer | 1 | 1
2 | C00002 | 2014-12-12 | 1 | computer | 4 | 3
3 | C00003 | 2014-12-12 | 1 | computer | 6 | 1
4 | C00002 | 2014-12-12 | 1 | computer | 8 | 3
5 | C00004 | 2014-12-12 | 1 | computer | 4 | 0
6 | C00002 | 2014-12-12 | 1 | computer | 24 | 1
7 | C00001 | 2014-12-12 | 2 | computer | 1 | 0
8 | C00002 | 2014-12-12 | 3 | computer | 1 | 0
For task 1, I have found the solution:
(fattendance = 3 means the position has changed and I have to keep record but the CSV report doesn't this)
SELECT fbtimeslot, COUNT(*) FROM facilitybooking WHERE fname='computer' AND fattendance<>3 GROUP BY fbtimeslot
But for 2 and 3, I tried more than 20 different statements but I still couldn't get the result.
Well, hard tasks though.
I will be very appreciated if you can help me to solve this.
Extras: SQLFiddle Link: http://sqlfiddle.com/#!2/4800b8/3