i want to check if the time entered is existed already in the db or not? here in $dbsum and $dbesum i am doing the sum like if i enter starttime 11:30 then it will give 690 in $dbsum and if i enter endtime 12.30 then it will give 750 in $dbesum. 'c_start' and 'c_end' is the sum that already existed in the database.Now i want to check if $dbsum and $dbesum existed between 'c_start' and 'c_end' or not. my query is like below in controller. but it's not working.
$dbstime = explode(':',$request->input('starttime'));
$dbetime = explode(':',$request->input('endtime'));
$dbstime[0] = $dbstime[0]*60;
$dbetime[0] = $dbetime[0]*60;
$dbsum = array_sum($dbstime);
$dbesum = array_sum($dbetime);
$users=DB::table("bookings")
->select("bookdate")
->where([
['bookdate', '=', $request->input('bookdate')],
['roomname', '=', $request->input('roomname')],
['starttime', '=', $request->input('starttime')],
['endtime', '=', $request->input('endtime')],
->whereBetween('c_start', [$dbsum, $dbesum])
->whereBetween('c_end', [$dbsum, $dbesum])
->get();
$users=DB::table("bookings")
->select("id")
->where('bookdate', '=', $request->input('bookdate'))
->where('roomname', '=', $request->input('roomname'))
->where(function ($query){
$query->where($dbsum, '>', 'c_start');
$query->orWhere($dbsum, '<', 'c_end');
})
->orWhere(function($query){
$query->where($dbesum, '>', 'c_start');
$query->orWhere($dbesum, '<', 'c_end');
})
->get();
Related
I have 2 tables, groups and questions. I need the result in such a way that it contains group name and corresponding non-deleted question count.
The structure and data for the two tables are as afollows.
My expected Result is as follows
I tried the following code, but it gives the entire row including the deleted questions.
$groupname = DB::table('groups as d')
->select([
'd.id','d.group_name',DB::raw("count(dtls.survey_group_id) as count")
])
->leftJoin('questions as dtls','d.id', '=', 'dtls.survey_group_id')
->whereNotExists( function ($query) {
$query->select(DB::raw(1))
->from('questions')
->where('id', 'd.id')
->where('dtls.is_deleted', 1);
})
->groupBy('d.id','d.group_name')
->get()
->toArray();
A problem here is that ->where('id', 'd.id') mathches id with the literal string d.id which obviously is not what you want. In addition, inner query tables should be aliased to prevent ambiguity. You can try changing it to:
$groupname = DB::table('groups as d')
->select([
'd.id','d.group_name',DB::raw("count(dtls.survey_group_id) as count")
])
->leftJoin('questions as dtls','d.id', '=', 'dtls.survey_group_id')
->whereNotExists( function ($query) {
$query->select(DB::raw(1))
->from('questions as innerDtls')
->whereRaw('innerDtls.id = d.id')
->where('dtls.is_deleted', 1);
})
->groupBy('d.id','d.group_name')
->get()
->toArray();
Alternatively your current query can be simplified to:
$groupname = DB::table('groups as d')
->select([
'd.id','d.group_name',DB::raw("count(dtls.survey_group_id) as count")
])
->leftJoin('questions as dtls','d.id', '=', 'dtls.survey_group_id')
->where('dtls.is_deleted', 0)
->groupBy('d.id','d.group_name')
->get()
->toArray();
I'm trying to count how many tickets our operators have closed in a week(and per day) I have these 2 queries
$closedWeek = Ticket::join('activity_log', 'activity_log.rel_id', '=', 'ticket.id')
->where([
['activity_log.created_at', '>', $fri],
['activity_log.user_id', '=', $id],
['activity_log.event_name', '=', 'ticket_closed'],
['number', 'like', 'SD%']
])->count();
$closedWeek2 = Ticket::join('activity_log', 'activity_log.rel_id', '=', 'ticket.id')
->where([
['activity_log.created_at', '>', $fri],
['activity_log.user_id', '=', $id],
['number', 'like', 'SD%'],
['activity_log.event_name', '=', 'ticket_department_updated']
])
->where(function($query) {
$query->where('activity_log.new_value', '=', 'closed Uninvoiced')
->orWhere('activity_log.new_value', '=', 'To Invoice')
->orWhere('activity_log.new_value', '=', 'closed Other')
->orWhere('activity_log.new_value', '=', 'closed Invoiced');
})->count();
$closedWeek = $closedWeek + $closedWeek2;
They are bringing back slightly bigger numbers than expected and I think some of them are duplicated. How could I put these into one query using where(function($query)) or do I have to use DB::raw instead? I can then count unique ids
You can use GroupBy(); with id,
Example:
$closedWeek = Ticket::join('activity_log', 'activity_log.rel_id', '=', 'ticket.id')
->where([
['activity_log.created_at', '>', $fri],
['activity_log.user_id', '=', $id],
['activity_log.event_name', '=', 'ticket_closed'],
['number', 'like', 'SD%']
])
->groupBy('ticket.id')
->count();
$closedWeek2 = Ticket::join('activity_log', 'activity_log.rel_id', '=', 'ticket.id')
->where([
['activity_log.created_at', '>', $fri],
['activity_log.user_id', '=', $id],
['number', 'like', 'SD%'],
['activity_log.event_name', '=', 'ticket_department_updated']
])
->whereIn('activity_log.new_value',
[ 'closed Uninvoiced',
'To Invoice',
'closed Other',
'closed Invoiced',
])
->groupBy('ticket.id')
->count();
$closedWeek = $closedWeek + $closedWeek2;
OBS:
You dont need '=', on equal wheres, example, where('colun', '=', 'abc'); you can change to where('colun', 'abc');
Please test and see if you have the expected result.
References:
How to get distinct values for non-key column fields in Laravel?
I want to get the per day total users average usage time for passed 7 days i written the the SQL for each users average time it's coming perfectly but i have an u\issues in LARAVEL SQL function please help fix this SQL.
$currentTime = Carbon::today();
$userUsage = DB::table('active_user')
->select(DB::raw('acu_name as name'),
DB::raw('u_fname as fname'),
DB::raw('AVG(TIMESTAMPDIFF(MINUTE,acu_at,acu_et)) as averageTime'),
DB::raw('count(*) as number'))
->join('u_info_one', 'active_user.acu_name', '=', 'u_info_one.u_email')
->whereDate('acu_at', '<=', $currentTime)
->groupBy('acu_name')
->get();
You need to use GROUP BY for this, e.g.:
$userUsage = DB::table('active_user')
->select(DB::raw('acu_name as name'),
DB::raw('u_fname as fname'),
DB::raw('AVG(TIMESTAMPDIFF(MINUTE,acu_at,acu_et)) as averageTime'),
DB::raw('count(*) as number'))
->join('u_info_one', 'active_user.acu_name', '=', 'u_info_one.u_email')
->whereDate('acu_at', '<=', $currentTime)
->groupBy('acu_name', DB::raw('DATE(acu_at)'))
->get();
update
If you just need daily average for all the users, you can remove acu_name from group by, e.g.:
$userUsage = DB::table('active_user')
->select(DB::raw('acu_name as name'),
DB::raw('u_fname as fname'),
DB::raw('AVG(TIMESTAMPDIFF(MINUTE,acu_at,acu_et)) as averageTime'),
DB::raw('count(*) as number'))
->join('u_info_one', 'active_user.acu_name', '=', 'u_info_one.u_email')
->whereDate('acu_at', '<=', $currentTime)
->groupBy(DB::raw('DATE(acu_at)'))
->get();
Correct Answer is
$userUsage = DB::table('active_user')
->select(DB::raw('DATE(acu_at) as date'),
DB::raw('AVG(TIMESTAMPDIFF(MINUTE,acu_at,acu_et)) as averageTime'),
DB::raw('COUNT(DISTINCT `acu_name`) as users'))
->join('u_info_one', 'active_user.acu_name', '=', 'u_info_one.u_email')
->whereDate('acu_at', '<=', $currentTime)
->groupBy(DB::raw('DATE(acu_at)'))
->orderBy(DB::raw('DATE(acu_at)'))
->get();
foreach($userUsage as $usUa)
{
$avegTime = ($usUa->averageTime/$usUa->users);
echo "['".$usUa->date."',".$avegTime."],";
}
Good day all, I am trying to count all records in a table but only if the table does not contain data in a specific column (deleted_at). It is a join table the table names are companies and employees. I am currently counting the records with a DB::raw but it should only count it if the deleted_at column is null. Please understand that i am a beginner.
public function index()
{
$user = Auth::user()->id;
$companies = DB::table('companies AS c')
->select([
'c.id',
'c.logo',
'c.company_name',
'c.created_at',
'c.sector',
'c.deleted_at',
DB::raw('COUNT(e.id) AS employee_count')
])
->leftJoin('employees AS e', 'e.company_id', '=', 'c.id' )
->leftJoin('company_user AS cu', 'cu.company_id', '=', 'c.id')
->where('cu.user_id', '=', $user)
->where('c.deleted_at', '=', null)
->groupBy('c.id')
->get();
return view('account.companies.index')
->with('companies', $companies);
}
If you are using Mysql then you could use conditional aggregation
$companies = DB::table('companies AS c')
->select([
'c.id',
'c.logo',
'c.company_name',
'c.created_at',
'c.sector',
'c.deleted_at',
DB::raw('SUM(c.deleted_at IS NULL) AS employee_count')
])
->leftJoin('employees AS e', 'e.company_id', '=', 'c.id' )
->leftJoin('company_user AS cu', 'cu.company_id', '=', 'c.id')
->where('cu.user_id', '=', $user)
->groupBy('c.id')
->get();
In mysql when an expression is used inside sum(a= b) it will result as a boolean 0/1 so you can get your conditional count using above
Or you could use whereNull() method in your query
->whereNull('c.deleted_at')
Use this code:
$employeeCount = DB::table('employees')
->select('companies.name', DB::raw('count(employees.id) as employee_count'))
->join('companies', 'employees.company','=','companies.id')
->groupBy('companies.id')
->get();
What i'm trying to achieve is the following:
I want to check if there is a record with the same client_code but with a lower/different campaign id. I'm using a sub-query for now and i tried to do it with a join as well but I couldn't get the logic working
This is what i got now:
$oDB = DB::table('campaigns AS c')
->select(
'c.id AS campaign_id',
'cc.id AS campaign_customer_id'
)
->join('campaign_customers AS cc', 'cc.campaign_id', '=', 'c.id')
->where('c.status', '=', ModelCampaign::STATUS_PLANNED)
->where('c.scheduled', '=', 1)
->whereRaw('c.scheduled_at <= NOW()')
->where('cc.status', '=', ModelCampaignCustomer::STATUS_INVITE_EMAIL_SCHEDULED)
->whereNotIn('cc.client_code', '=', function ($query){
$query ->select(DB::raw(1))
->from('campaign_customers')
->whereRaw('campaign_id', '!=', 'c.id');
})
->where('cc.active', '=', 1)
;
any tips on how to work the logic would be great
You can use the ->toSql(); method to see the SQL so you can refactorize your query.
The whereNotIn probably shouldn't have an = in it
->whereNotIn('cc.client_code', function ($query){
....
edit
Try:
->whereNotIn('cc.client_code', function ($query){
$query->select(DB::raw('client_code'))
->from('campaign_customers')
->whereRaw('campaign_id != c.id');
})
I think the whereRaw should be a single text string or maybe you can use where here (looking at this for reference https://laravel.com/docs/5.2/queries#advanced-where-clauses). Also DB::raw(1) is going to return a 1 for each subquery result but you want an id for the whereNotIn.