Conditional aggregation, filter in laravel

眉间皱痕 提交于 2021-02-11 13:23:57

问题


This is my second question based after the solution of Rows to columns based on date/time in Postgresql The table is in the upper link :)

Here is my model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class StambMeasCanal extends Model
{
  protected $connection = 'stamb';
  protected $table = 'meas_kanal';
  protected $fillable = ['fullname','formattedvalue','recordtime','qualitydesc','statedesc','id'];
}

Part of my controller:

function getdata(Request $request)
{
  $start_date = date('d-m-Y 00:00:00');
  $end_date = date('d-m-Y 23:59:59');
  if($request->start_date != '' && $request->end_date != '')
  {
    // if user fill dates
    $dateScope = array($request->start_date ." 00:00:00", $request->end_date ." 23:59:59");
  } else {
    // default load page - today
    $dateScope = array($start_date, $end_date);
  };

  $students = StambMeasCanal::whereBetween('recordtime', $dateScope)
  ->selectRaw('recordtime')
  ->selectRaw('max(formattedvalue) filter (where fullname = "Данни.Кота") as kotaiazovir')
  ->selectRaw('max(formattedvalue) filter (where fullname = "Данни.Напрежение") as naprevenieqzovir')
  ->selectRaw('max(formattedvalue) filter (where fullname = "Данни.Температура") as tempqzovir')
  ->selectRaw('max(formattedvalue) filter (where fullname = "Данни.Температура_табло") as temptabloqzovir')
  ->selectRaw('max(formattedvalue) filter (where fullname = "Данни.Ниво") as nivoqzovir')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_00_600.Данни.Напрежение") as naprevenie600')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_00_600.Данни.Ниво") as nivo600')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_00_600.Данни.НивоУЗ") as nivouz600')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_00_600.Данни.Разход") as razhod600')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_00_600.Данни.Температура") as temperatura600')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_09_200.Данни.Напрежение") as naprevenie200')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_09_200.Данни.Ниво") as nivo200')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_09_200.Данни.НивоУЗ") as nivouz200')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_09_200.Данни.Разход") as razhod200')
  ->selectRaw('max(formattedvalue) filter (where fullname = "ГСК_09_200.Данни.Температура") as temperatura200')
  ->selectRaw('max(qualitydesc) as QualityDesc')
  ->selectRaw('max(qualitydesc) as StateDesc')
  ->groupBy('recordtime')
  ->orderBy('recordtime', 'ASC')
  ->get();

  return Datatables::of($meascanal)
  ->make(true);
}

After the request i have an error:

message: "SQLSTATE[42703]: Undefined column: 7 ERROR:  column "Данни.Кота" does not exist↵LINE 1: ...ime, max(formattedvalue)..

Is it beacouse bad integration that I made in the laravel query?

EDIT1: The problem was the quotes. I needed to escape them:

where fullname = \'Данни.Кота\'

EDIT2:

The last two columns(QualityDesc and StateDesc) are not bound to any FormattedValue columns and are displayed in wrong order... Any idea how to fix it?

来源:https://stackoverflow.com/questions/60207632/conditional-aggregation-filter-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!