分组不在Laravel 5.2工作

时间:2017-05-30 14:03:30

标签: mysql laravel-5

分组依据不起作用,可能是因为我在选择选项中使用数学表达式。

select FLOOR(SUM(DATEDIFF(rd.checkin_date,rd.created_at))/COUNT(DATEDIFF(rd.checkin_date,rd.created_at))) AS names, 
COUNT(rd.id) as totbooking, 
SUM(DATEDIFF(rd.checkout_date,rd.checkin_date)) AS nights, 
round(SUM(CASE WHEN h.currency_id=151 THEN rd.total_cents WHEN h.currency_id!=151 THEN rd.total_cents/cur.exchange_rate ELSE 0 END)/100) AS revenue, 
round((round(SUM(CASE WHEN h.currency_id=151 THEN rd.total_cents WHEN h.currency_id!=151 THEN rd.total_cents/cur.exchange_rate ELSE 0 END)/100)) / (SUM(DATEDIFF(rd.checkout_date,rd.checkin_date)))) as adr, FLOOR((FLOOR(SUM(CASE WHEN h.currency_id=151 THEN rd.total_cents WHEN h.currency_id!=151 THEN rd.total_cents/cur.exchange_rate ELSE 0 END)/100)) / COUNT(rd.id)) as atp, FLOOR(SUM(DATEDIFF(rd.checkin_date,rd.created_at))/COUNT(DATEDIFF(rd.checkin_date,rd.created_at))) as leadtime, 
SUM(DATEDIFF(rd.checkout_date,rd.checkin_date)) / (COUNT(rd.id)) as alos, (SUM(DATEDIFF(rd.checkout_date,rd.checkin_date)) / (h.total_rooms * 31))*100 as share from `reservation_details` as `rd` 
left join `hotels` as `h` on `rd`.`hotel_id` = `h`.`id` 
left join `currencies` as `cur` on `cur`.`id` = `h`.`currency_id` 
left join `chains` as `c` on `c`.`id` = `h`.`chain_id`
where `rd`.`status` >= 50 and date(rd.created_at) >= "2017-04-30" and date(rd.created_at) <= "2017-05-30" GROUP BY names

2 个答案:

答案 0 :(得分:0)

您正在使用别名,因此在ion.sound子句中您也应该使用别名 我认为GROUP BY属于names所以你需要这样做

hotels

答案 1 :(得分:0)

为什么不尝试这种方式?如果你让我们看一些数据会有所帮助

select names,sum(totbooking),sum(nights),sum(revenue),sum(adr),leadtime,sum(alos),sum(share)
from (
    select FLOOR(SUM(DATEDIFF(rd.checkin_date,rd.created_at))/COUNT(DATEDIFF(rd.checkin_date,rd.created_at))) AS names, COUNT(rd.id) as totbooking, 
    SUM(DATEDIFF(rd.checkout_date,rd.checkin_date)) AS nights,round(SUM(CASE WHEN h.currency_id=151 THEN rd.total_cents WHEN h.currency_id!=151 THEN rd.total_cents/cur.exchange_rate ELSE 0 END)/100) AS revenue, 
    round((round(SUM(CASE WHEN h.currency_id=151 THEN rd.total_cents WHEN h.currency_id!=151 THEN rd.total_cents/cur.exchange_rate ELSE 0 END)/100)) / (SUM(DATEDIFF(rd.checkout_date,rd.checkin_date)))) as adr, FLOOR((FLOOR(SUM(CASE WHEN h.currency_id=151 THEN rd.total_cents WHEN h.currency_id!=151 THEN rd.total_cents/cur.exchange_rate ELSE 0 END)/100)) / COUNT(rd.id)) as atp, FLOOR(SUM(DATEDIFF(rd.checkin_date,rd.created_at))/COUNT(DATEDIFF(rd.checkin_date,rd.created_at))) as leadtime, 
    SUM(DATEDIFF(rd.checkout_date,rd.checkin_date)) / (COUNT(rd.id)) as alos, (SUM(DATEDIFF(rd.checkout_date,rd.checkin_date)) / (h.total_rooms * 31))*100 as share 
    from `reservation_details` as `rd` 
    left join `hotels` as `h` on `rd`.`hotel_id` = `h`.`id` 
    left join `currencies` as `cur` on `cur`.`id` = `h`.`currency_id` 
    left join `chains` as `c` on `c`.`id` = `h`.`chain_id`
    where `rd`.`status` >= 50 and date(rd.created_at) >= "2017-04-30" and date(rd.created_at) <= "2017-05-30" )
group by names