Laravel-总和中违反基数的错误

时间:2019-07-02 09:55:14

标签: laravel eloquent

我正在尝试查询以获取当日,当周,当月,当年和总销售额。结果一并。我使用了如下所示的查询:

    $currentDay = date('d');
    $currentWeek = date('w');
    $currentMonth = date('m');
    $currentYear = date('y');

    $revenues = DB::table("vw_revenue_report")
->select(
    "vw_revenue_report.channel",
    DB::raw("SUM(CASE WHEN ('DAY(created_at) = ?',$currentDay) THEN amount ELSE 0 END) daily_revenue"),    
    DB::raw("SUM(CASE WHEN ('WEEK(created_at) = ?',$currentWeek) THEN amount ELSE 0 END) weekly_revenue"),
    DB::raw("SUM(CASE WHEN ('MONTH(created_at) = ?',$currentMonth) THEN amount ELSE 0 END) monthly_revenue"),
    DB::raw("SUM(CASE WHEN ('YEAR(created_at) = ?',$currentYear) THEN amount ELSE 0 END) yearly_revenue"),
    DB::raw("SUM(vw_revenue_report.amount) total_revenue")
)
->groupBy("vw_revenue_report.channel")
->orderByRaw('vw_revenue_report.channel ASC')
    ->get();  

在尝试运行它时,出现以下错误:

  

“ SQLSTATE [21000]:违反基数:1241操作数应包含1列(SQL:选择vw_revenue_reportchannel,SUM(如果('DAY(created_at)=?) ,02)THEN数量ELSE 0 END)每日收入,SUM(CASE WHEN('WEEK(created_at)=?',2)THEN数量ELSE 0 END)每周收入,SUM(CASE WHEN('MONTH(created_at)=?',07) )然后以vw_revenue_report组为基础,按vw_revenue_report组的{_1}}()()(当(YEAR(created_at)=?',19)THEN量ELSE 0 END)Annual_revenue的情况下,monthly_revenue月收入。 }按照vw_revenue_report.channel ASC,channelvw_revenue_report的升序限制100偏移量0)◀“

我该怎么解决?

1 个答案:

答案 0 :(得分:0)

希望它将为您服务

$currentDay = date('d');
$currentWeek = date('w');
$currentMonth = date('m');
$currentYear = date('y');

$revenues = DB::table("vw_revenue_report")
->select(
    "vw_revenue_report.channel","sum(amount) as Total_in_day",
    DB::raw("(select sum(amount) as tot from vw_revenue_report where WEEK(created_at) = $currentWeek) as Total_in_week"),
    DB::raw("(select sum(amount) as tot from vw_revenue_report where MONTH(created_at) = $currentMonth) as Total_in_month"),
    DB::raw("(select sum(amount) as tot from vw_revenue_report where YEAR(created_at) = $currentYear) as Total_in_year")
)
->where("DAY(created_at)",$currentDay)
->get();