Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

giupes's avatar

Problem with partial match on query builder

Hi guys, i'm new to laravel. I use this query on my controller and work, this one show data of daily distribution for values that was filtered with a form, and this is modulable because when some parameters of filter are not set i don't use in my query.

$query = Measure::query() ->select(DB::raw('DATEPART(hour, DATETIME_D) AS HOUR_N')) ->selectRaw('SUM(CASE WHEN STATUS_N > 0 THEN 1 ELSE 0 END) AS OK_N') ->selectRaw('SUM(CASE WHEN STATUS_N <= 0 THEN 1 ELSE 0 END) AS NOK_N') ->where('MEASUREID_N', '>', 0) ->where('DATETIME_D', '>=', $dfrom) ->where('DATETIME_D', '<=', $dto);

    $query->when($serial_number,function(Builder $q, string $serial_number){
        $q->where('PRODSN_S','=',$serial_number);
    });

    $query->when($user_id,function(Builder $q, string $user_id){
        $q->where('USERID_S','=',$user_id);
    });

    $query->when($product_id,function(Builder $q, string $product_id){
        $q->where('PRODID_S','LIKE','%'.$product_id.'%');
    });

    $query->when($area,function(Builder $q, string $area){
        $q->join('AREAS_DEF','MEASURES.AREAID_N','=','AREAS_DEF.AREAID_N')
        ->where('AREAS_DEF.NAME_S','=',$area);
    });

    $query->when($phase,function(Builder $q, string $phase){
        $q->join('PHASES_DEF','MEASURES.PHASEID_N','=','PHASES_DEF.PHASEID_N')
        ->where('PHASES_DEF.NAME_S','LIKE','%'.$phase.'%');
    });

    $query->groupBy(DB::raw('DATEPART(hour, DATETIME_D)'))
    ->orderBy(DB::raw('DATEPART(hour, DATETIME_D)'));

This is work and this is so fast also

But when i use the same query only with some different parameters that return weekly distribution the query is not resolved and seems that is stacked and freezing. This is the query:

$query = Measure::query() ->select(DB::raw('DATEPART(dw, DATETIME_D) AS DAY_N')) ->selectRaw('SUM(CASE WHEN STATUS_N > 0 THEN 1 ELSE 0 END) AS OK_N') ->selectRaw('SUM(CASE WHEN STATUS_N <= 0 THEN 1 ELSE 0 END) AS NOK_N') ->where('MEASUREID_N', '>', 0) ->where('DATETIME_D', '>=', $dfrom) ->where('DATETIME_D', '<=', $dto);

    $query->when($serial_number,function(Builder $q, string $serial_number){
        $q->where('PRODSN_S','=',$serial_number);
    });

    $query->when($user_id,function(Builder $q, string $user_id){
        $q->where('USERID_S','=',$user_id);
    });

    $query->when($product_id,function(Builder $q, string $product_id){
        $q->where('PRODID_S','LIKE','%'.$product_id.'%');
    });

    $query->when($area,function(Builder $q, string $area){
        $q->join('AREAS_DEF','MEASURES.AREAID_N','=','AREAS_DEF.AREAID_N')
        ->where('AREAS_DEF.NAME_S','=',$area);
    });

    $query->when($phase,function(Builder $q, string $phase){
        $q->join('PHASES_DEF','MEASURES.PHASEID_N','=','PHASES_DEF.PHASEID_N')
        ->where('PHASES_DEF.NAME_S','LIKE', '%'.$phase.'%');
    });

    $query->groupBy(DB::raw('DATEPART(dw, DATETIME_D)'))
    ->orderBy(DB::raw('DATEPART(dw, DATETIME_D)'));

I try to remove 'LIKE' parameters and set '=' instead and this go very fast. This is so stange because when i try the query in SQL Manager this go very fast and without error. I have already encountered this problem with other queries where partial matching was present. Have you ever had problems like this?

0 likes
1 reply
Thunderson's avatar

why do not use count intead of SUM(CASE WHEN STATUS_N > 0 THEN 1 ELSE 0 END) AS OK_N') like this

->selectRaw("COUNT('STATUS_N') as OK_N") ....

Please or to participate in this conversation.