PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] How to get total users average time passed 7 days

 February 22, 2022     laravel, mysql, phpmyadmin     No comments   

Issue

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();

enter image description here


Solution

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();


Answered By - Darshan Mehta
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing