I want to some selected column. I have tried to sum for fee. But it throws an error:
Integrity constraint violation: 1052 Column 'service_fee' in field
list is ambiguous (SQL: select count(*) as aggregate from..........
I have tried below way. Can anybody help with this? Thanks in advance.
$events = Event::select($columns)->EventComplete()
->where('events.event_end_date', '<', $currentDate)
->where('events.have_balance', '>', 0)
->join('role_users_details as rud', 'events.promoter_id', '=', 'rud.id')
->join('transaction as tra', 'events.id', '=', 'tra.event_id', 'left outer')
->join(\DB::raw("(select tran.id as ttid, sum(tran.ticket_price) as total_amt, sum(tran.ticket_service_fee) as service_fee, tran.order_service_fee as order_service_fee,tran.order_service_fee_per as order_service_fee_per , count('ticket_order.*') as total_tickets
from `transaction` as `tran`
right join `ticket_order` on `tran`.`id` = `ticket_order`.`transaction_id`
where `ticket_order`.`status` = 1 group by `ttid`) as tt"), 'tt.ttid', '=', 'tra.id', 'left outer ')
->selectRaw('CONCAT_WS(" ", rud.first_name, rud.middle_name, rud.last_name) as promoter')
->selectRaw('sum(tt.total_amt) as tickets')
->selectRaw('sum(tt.service_fee) as service_fee')
->selectRaw('sum(tt.order_service_fee) as order_fee')
->selectRaw('sum(tt.order_service_fee_per) as order_fee_per')
->selectRaw('sum(tt.total_tickets) as total_tickets')
->selectRaw('(service_fee + order_fee + order_fee_per) as fee')
->selectRaw('FROM_UNIXTIME(events.event_end_date, "%Y/%m/%d %h:%i:%s") as ending_date')
->groupBy('events.id');
Could you just add all the sums together?
->selectRaw('sum(tt.service_fee) + sum(tt.order_service_fee) + sum(tt.order_service_fee_per) as fee')
Related
Im going to count my pallet_condition 0,1,2,3 and group it by month
but i always got an error like this please help whats wrong with my code.
something wrong with my date_format?
My Error
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in group statement is ambiguous (SQL: select count(*) as aggregate from `liip_psrm` inner join `liip_psrm_items` on `liip_psrm`.`id` = `liip_psrm_items`.`psrm_items_id` where date(`liip_psrm_items`.`created_at`) >= 2018-10-09 and date(`liip_psrm_items`.`created_at`) <= 2019-01-31 and `liip_psrm`.`status` = 0 and `liip_psrm`.`customer_id` = 14 and `pallet_condition` = 0 group by WEEK(created_at) order by WEEK(created_at) asc)
My Callback function
$problem_condition_computations = function($condition) use ($psrm_maintenance, $start, $end, $customers){
return DB::table('liip_psrm')
->join('liip_psrm_items', 'liip_psrm.id', '=', 'liip_psrm_items.psrm_items_id')
->whereDate('liip_psrm_items.created_at', '>=', $start)
->whereDate('liip_psrm_items.created_at', '<=', $end)
->select(
DB::raw("DATE_FORMAT(liip_psrm_items.created_at, '%Y-%m-%d') AS dates
"))
->where('liip_psrm.status','=', 0)
->where('liip_psrm.customer_id','=',$customers)
->where('pallet_condition', $condition)
->groupBy('dates')
->count();
};
"dates" field you are using in groupBy is alias name, So it do not identifying it and not working as you are expecting.
Same as you used in Select
DB::raw("DATE_FORMAT(liip_psrm_items.created_at, '%Y-%m-%d') AS dates")
Put it in GroupBy
& It will work.
Hope, It will help, Let me know if you still find difficulties.
I am working on some maintaince project where all are sql queries but i want to convert it in to laravel. Here is the below query :-
$members = Member::select('members.id','members.first_name',
'members.surname','members.username','members.password',
'members.email','user_access.active',
DB::raw('SUM( IF (user_access.active = "y", 1, 0) ) as acount'))
->join('user_access','user_access.member_id','=','members.id')
->where('special_access','=','n')
->groupby('user_access.member_id')
->having('acount','>','0')
->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderby('user_access.note','DESC')
->paginate(30);
Its giving me error when i execute the query. below the error in having clause
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'acount' in 'having clause' (SQL: select count(*) as aggregate from `members` inner join `user_access` on `user_access`.`member_id` = `members`.`id` where `special_access` = n group by `user_access`.`member_id` having `acount` > 0)
I guess something like this will help you.
$userAccess = DB::table('user_access')
->where('special_access','=','n')
->where('active','y')
->groupby('member_id');
Member::select([
'members.id',
'members.first_name',
'members.surname',
'members.username',
'members.password',
'members.email',
'user_access.active'
])->joinSub($userAccess, 'user_access', function($join){
$join->on('user_access.member_id', '=', 'members.id');
})->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderBy('user_access.note','asc')
->paginate(30);
OR
$members = Member::select([
'members.id',
'members.first_name',
'members.surname',
'members.username',
'members.password',
'members.email',
'user_access.active'
])->join('user_access', function($join){
$join->on('user_access.member_id', '=', 'members.id')->on('user_access.active', '=', DB::raw('"y"'));
})
->where('special_access','=','n')
->groupby('user_access.member_id')
->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderby('user_access.note','DESC')
->paginate(30);
As apokryfos said in the comments:
The paginator will attempt to get the count of the query and will remove all selects from it when doing so leading to this error.
If you just need records with user_access.active = "y" then you do not need to select them in the first place and then try to filter them out by HAVING
I'm trying to join a table on itself but keep getting errors. Here is my current code. I've also tried Raw statement. Not sure which direction to go in at the moment.
My code:
$Calle = db('VoipBill')
->table('billing')
->join('billing', 'billing.srcnum', '=', 'billing.dstnum')
->where('acct_name', '100080_company')
->where('srcnum', $call->srcnum)
->whereBetween('calldate', [$set_time_lower, $set_time_upper])
->get();
This is the error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique
table/alias: 'billing' (SQL: select * from billing inner join
billing on billing.srcnum = billing.dstnum where acct_name
= 100080_company and srcnum = +27******** and calldate between 2016-05-02 09:19:27 and 2016-05-02 09:19:37)
You have to alias the tables. Try it it this way:
$Calle = db('VoipBill')
->table('billing as bsrc')
->join('billing as bdst', 'bsrc.srcnum', '=', 'bdst.dstnum')
->where('bsrc.acct_name', '100080_company')
->where('bsrc.srcnum', $call->srcnum)
->whereBetween('bsrc.calldate', [$set_time_lower, $set_time_upper])
->get();
Hi I want to get data from database, I am using join query but I have got this error:
QueryException in Connection.php line 673:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'surat_masuk' (SQL: select jenis_surat.jenis_surat, surat_masuk.nomor_surat, surat_masuk.perihal, surat_masuk.tanggal_publish from surat_masuk inner join jenis_surat on id_jenis_surat = jenis_surat.id_jenis_surat inner join surat_masuk on id_jenis_surat = surat_masuk.id_jenis_surat)
I have 2 tables, surat_masuk and jenis_surat, I want to get jenis_surat field from jenis_surat table and nomor_surat, perihal, tanggal_publish from surat_masuk table. and there is column id_jenis_surat in both table.
This is my query:
$surat = new Surat();
$surats = $surat->join('jenis_surat', 'id_jenis_surat', '=', 'jenis_surat.id_jenis_surat')
->join('surat_masuk', 'id_jenis_surat', '=', 'surat_masuk.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')
->get();
Do you know how to fix it?
$surats = $surat->join('jenis_surat', 'surat_masuk.id_jenis_surat', '=', 'jenis_surat.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')->get();
$surat = new Surat();
$surats = $surat->join('jenis_surat', 'surat_masuk.id_jenis_surat', '=', 'jenis_surat.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')
->get();
OR
$surats = \DB::table('surat_masuk')
->join('jenis_surat', 'surat_masuk.id_jenis_surat', '=','jenis_surat.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')
->get();
I basically want to do:
select * from request where id = 1 and created_at like (today's date);
but using eloquent.
I tried:
$query = m_requests::where(['id' => Auth::User()->id, "created_at", "like", "%".date("Y-m-d")."%"]);
but that returns the error:
SQLSTATE[42S22]: Column not found: 1054 Unknown
column '0' in 'where clause' (SQL: select * from
from `request` where (`id` = 1 and `0` = created_at
and `1` = like and `2` = %2016-04-22%))
Does anyone know how I could achieve what I am trying to do?
In summary:
I want the id to equal the id requested, but I want the date to be "LIKE" today's date (This is because I'm only interested in the date it was created and not the time it was created).
Cheers
Try this, it should work:
m_requests::where('id', '=', Auth::user()->id)
->where('created_at', 'LIKE', '%'.date("Y-m-d").'%');