我正在尝试查询以获取当日,当周,当月,当年和总销售额。结果一并。我使用了如下所示的查询:
$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_report
。channel
,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,channel
。vw_revenue_report
的升序限制100偏移量0)◀“
我该怎么解决?
答案 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();