Select from two column by two dates in codeignter models - codeigniter

I want to select from two column with two dates given in codeigniter models.
$total_car = 5;
My customer order table
booked_car start_date end_date
1 2019-06-06 2019-06-07
2 2019-07-15 2019-07-18
1 2019-08-27 2019-09-03
I want to check total car has been booked by selected dated in my models
$this->db->select('SUM(total_car) AS booked_car');
$this->db->from('car_order');
$this->db->where('start_date >=', '2019-08-27');
$this->db->where('end_date <=', '2019-09-03');
Result when search date same with in customer order table:
$booked_car = 1;
$avalaible car = 4 (get from $total_car - $booked_car);
But when I try to change date on start_date and end_date :
$this->db->select('SUM(total_car) AS booked_car');
$this->db->from('car_order');
$this->db->where('start_date >=', '2019-08-29');
$this->db->where('end_date <=', '2019-09-05');
or
$this->db->where('start_date >=', '2019-08-31');
$this->db->where('end_date <=', '2019-09-04');
Result :
$booked_car = 0;
$avalaible car = 5 (get from $total_car - $booked_car);
I think result should be :
$booked_car = 1;
$avalaible car = 4 (get from $total_car - $booked_car);
Because on '2019-08-29' or '2019-08-31' or any date as long as range between '2019-08-27' until '2019-09-03', car still booked base on booked_car table above.
How can I get correct results base on date in order table in my models.
Thank you for your assistance.

maybe you should do this
$this->db->where('DATE(start_date) >=', datefordatabase('2019-08-29'));
$this->db->where('DATE(end_date) <=', datefordatabase('2019-09-05'));

Related

How to get the product name with the highest value by month?

I'm trying to display a data by month and displays the product with the highest value or bought quantity. But I don't know how I should do it.
This is the code I have and tried so far
public function viewSales(){
$data = sales::select(
[sales::raw("SUM(product_fee) as product_fee, MONTHNAME(created_at) as month_name, MAX(bought) as bought"),'product']
)->whereYear('created_at', date("Y"))
->orderBy('created_at', 'asc')
->groupBy('month_name')
->get();
return view('admin.viewSales', compact('data'));
}
This is what's inside my database
id
product
bought
product_fee
created_at
1
Dictionary
1
200
2023-01-14 18:55:34
2
Horror
3
100
2023-01-15 17:55:34
3
How to cook
5
300
2023-01-16 11:55:34
and this is what I'm trying to display
Most bought product
sold
overall earning
How to cook
5
600
i think you can try this :
public function viewSales(){
$data = sales::select(
[sales::raw("(bought*product_fee) as overall_earning, MONTHNAME(created_at) as month_name, MAX(bought) as bought"),'product']
)->whereYear('created_at', date("Y"))
->orderBy('created_at', 'asc')
->groupBy('month_name')
->get();
return view('admin.viewSales', compact('data'));
}
I hope this works for you
Below SQL will give you the desired output for current year and month.
Fetch for current year
SELECT
product as most_bought_product_for_the_year,
bought as sold,
(SELECT SUM(product_fee) from products WHERE YEAR(created_at) = year(curdate())) as overall_earning
FROM `products`
WHERE
bought = (SELECT MAX(bought) from products WHERE YEAR(created_at) = year(curdate()));
Fetch for months of the current year
SELECT
product as most_bought_for_the_month,
bought as sold,
MONTHNAME(created_at) as month_name,
SUM(product_fee) as overall_earning
FROM `products`
WHERE
YEAR(created_at) = YEAR(curdate())
AND
bought IN (SELECT MAX(bought) from products WHERE YEAR(created_at) = YEAR(curdate()) GROUP by MONTH(created_at))
GROUP BY MONTH(created_at)
ORDER BY MONTH(created_at);
SQL Fiddle

Laravel Sum transactions amount grouped by category

I'm trying to sum all transactions and group these records by category with the summed value of transactions for a particular month.
Example (Current month):
Groceries => 500
Subscriptions => 100
Coffee => 0
But the current code I have only groups categories when a transaction has occurred for that month and will return the following:
Groceries => 500
Subscriptions => 100
Here are my the following database tables:
Transactions table:
id
date
category_id
amount
1
2021-01-01
1
200
2
2021-01-01
1
300
3
2021-01-01
2
100
3
2020-06-06
3
100
Categories table:
id
display_name
description
1
Groceries
Food
2
Subscriptions
Subscriptions
3
Coffee
Coffee
And my code that selects transactions for the current month but doesn't return a full list of categories even if there are no transactions.
$currentMonth = date('m');
$grouped = DB::table('categories')
->join('transactions', function ($join) use ($currentMonth) {
$join->on('transactions.category_id', '=', 'categories.id')
->whereRaw('MONTH(date) = ?',[$currentMonth]);
})
->groupBy('categories.display_name')
->selectRaw('sum(transactions.amount) as sum, display_name as name')
->pluck('sum', 'name');
Move the criteria in the WHERE clause to the ON clause of the join, and use a left join:
$grouped = DB::table('categories c')
->leftJoin('transactions t', function ($join) {
$join->on('t.category_id', '=', 'c.id')
$join->on(DB::raw('MONTH(t.date) = ?'))
})
->groupBy('c.display_name')
->selectRaw('SUM(t.amount) AS sum, c.display_name AS name')
->setBindings([$currentMonth])
->pluck('sum', 'name');
The raw MySQL query I am suggesting here is:
SELECT
c.display_name,
SUM(t.amount) AS sum
FROM categories c
LEFT JOIN transactions t
ON c.id = t.category_id AND
MONTH(t.date) = ?
GROUP BY
c.display_name;
The structure used above in the left join is critical here, because it ensures that every category on the left side of the join would appear in the result set, even if it have no matching transactions for a given month.

Laravel Relationships setup for the following table

I am trying to set up a exam timetable module,
This is the table structure:-
id
title
start_date
end_date
class_id
subject_id
date(for each subject_id)
Now the thing is that class_id exists in classroom table as "id" and I need to get it's name from it which is in the table as "title". Similarly for the subject_id where it exists in subject table as "id" and it's name which is in the table as "title". Once I get these the database table would look something like this:-
id title start_date end_date class_id subject_id date
1 test some_date some_date 1 1 date
1 test some_date some_date 1 2 date
1 test some_date some_date 1 3 date
1 test some_date some_date 1 4 date
how do I set up my controller with the relationships, please help!
Try this.
In exam model.
public function class()
{
return $this->hasMany('App\Class');
}
public function subject()
{
return $this->hasMany('App\Subject');
}
In class model.
public function exam()
{
return $this->belongsTo('App\Subject');
}
In subject model
public function exam()
{
return $this->belongsTo('App\Subject');
}
This way, you can perform
return $exam->subject->title
or
return $exam->class->title
but if you have relationship between class and subject already, you can achieve this in another way using belongs to through which you can find here. https://laravel.com/docs/5.6/eloquent-relationships#has-many-through

Movie data set analysis using PIG

I have the following data set for a movie database:
Ratings: UserID, MovieID, Rating :: Movies: MovieID, Title :: Users: UserID, Gender, Age
Now I have to JOIN the above 3 datasets and determine which movie has the highest rating among females and lowest rating among males, and vice versa.
I have done the JOIN:
myusers = LOAD '/user/cloudera/movies/input/users.dat'
USING PigStorage(':')
AS (user:int, n1, gender:chararray, n2, age:int);
ratings = LOAD '/user/cloudera/movies/input/ratings.dat'
USING PigStorage(':')
AS (user:int, n1, movie:int, n2, rating:int);
movies = LOAD '/user/cloudera/movies/input/movies.dat'
USING PigStorage(':')
AS (movie:int,n1,title:chararray);
data = JOIN ratings BY user, myusers BY user;
data2= JOIN data BY ratings::movie, movies BY movie;
But after this I am running into many issues such as "ERROR 0: Scalar has more than one row in the output" when I try to print columns from data2. Any ideas to help me accomplish this task?
After the following step
data = JOIN ratings BY user, myusers BY user;
Create two datasets one for male and another for female by using gender as the filter.Order the dataset and get the max and min for both the datasets.
male = FILTER data by gender == 'M'; -- Use the gender value for male
female = FILTER data by gender == 'F';
m_max = LIMIT (ORDER male by rating DESC) 1;
f_max = LIMIT (ORDER female by rating DESC) 1;
m_min = LIMIT (ORDER male by rating ASC) 1;
f_min = LIMIT (ORDER female by rating ASC) 1;

how would you query this in linq?

lets say i have 2 tables: products (just product ID and name) and sales (sale ID, product ID, amount, date)
now, given a start date and end date, i want to sum for every product its total sales amount in the given time frame
notice that naturally some products will just have zero sales
how should i write this query?
var products =
from p in mycontext.Products
select new
{
Product = p,
Sales = p.Sales
.Where(s=>s.StartDate > startDate && s.EndDate < endDate)
.Sum(s=>s.amount)
}

Resources