在 sql 中具有“类型”列的每个日期的金额总和

Posted

技术标签:

【中文标题】在 sql 中具有“类型”列的每个日期的金额总和【英文标题】:Sum of amount for each date having 'type' column in sql 【发布时间】:2021-04-06 13:14:21 【问题描述】:

我有表expense_incomes,表的结构是:

| id | type    |  amount  |    date      |
|----------------------------------------|
|  1 | Expense |  100     |  2021-04-02  |
|  2 | Expense |  150     |  2021-04-02  |
|  3 | Expense |  150     |  2021-04-03  |
|  4 | Income  |  500     |  2021-04-04  |
|  5 | Expense |  250     |  2021-04-04  |
|----------------------------------------|

我想要两个不同数组 groupBy 日期中的费用和收入的总和。 我试过这段代码。 (在上面的示例中)我想将 2021-04-02 的费用推入费用数组,收入推入收入数组。

$record_by_date = DB::select("SELECT SUM(amount) as 'amount', cast(date as date) dateAdded, type as 'type' FROM expense_incomes GROUP BY cast(date as date), type");
$expenses_array = array();
$incomes_array = array();
$date_array = array();
foreach($record_by_date as $record)

  if($record->type == 'Expense') 
      array_push($expenses_array, $record->amount);
      array_push($incomes_array, 0);
   elseif($record->type == 'Income') 
      array_push($incomes_array, $record->amount);
      array_push($expenses_array, 0);
  
  array_push($date_array, $record->dateAdded);

我现在得到了什么:

$expenses_array = [250, 150, 0, 250];
$incomes_array = [0, 0, 500, 0];
$date_array = [2021-04-02, 2021-04-03, 2021-04-04, 2021-04-04];

预期的数组结构:

$expenses_array = [250, 150, 250];
$incomes_array = [0, 0, 500];
$date_array = [2021-04-02, 2021-04-03, 2021-04-04];

【问题讨论】:

你预期的最终数组结构是什么? @MKhalidJunaid 预期结果现已添加到原始问题中 【参考方案1】:

我相信您想要一个聚合查询,其中每个日期的金额有条件总和。

在纯 SQL 中,它可以使用 CASE 语句来完成

select  date as dateAdded,
        sum(case when type='Income' then amount else 0 end) as income,
        sum(case when type='Expense' then amount else 0 end) as expense
from expense_incomes 
group by date 
order by date 

DEMO

在查询生成器中可以转换为

$data= DB::table("expense_incomes")
            ->select([ "date as dateAdded",
                        DB::raw("sum(case when type='Income' then amount else 0 end) as income"),
                        DB::raw("sum(case when type='Expense' then amount else 0 end) as expense")
            ])
            ->groupBy("date")
            ->orderBy("date")
            ->get();

现在您可以使用集合助手pluck 为您的列提取数据

$expenses_array = $data->pluck('expense');
$incomes_array = $data->pluck('income');
$date_array = $data->pluck('dateAdded');

【讨论】:

【参考方案2】:
$expensesArray =  DB::select("SELECT SUM(amount) as 'amount', cast(date as date) dateAdded, type as 'type' FROM expense_incomes GROUP BY cast(date as date), type where type = 'Expense'")->toArray();

$incomeArray =  DB::select("SELECT SUM(amount) as 'amount', cast(date as date) dateAdded, type as 'type' FROM expense_incomes GROUP BY cast(date as date), type where type = 'Income'")->toArray();

【讨论】:

请查看编辑,我已添加预期结果 请分享您的阵列响应您现在得到的结果。 我想从单个查询中获取结果,因为我有时希望在同一日期出现支出和收入 首先使用dd($record_by_date)打印查询结果 我已将当前结果添加到问题中

以上是关于在 sql 中具有“类型”列的每个日期的金额总和的主要内容,如果未能解决你的问题,请参考以下文章

Pyspark - 获取具有条件的列的累积总和

SQL:获取行列值的总和

MDX 查询以对具有特定日期范围的日期维度进行分组

具有 1 个不同的列和来自另一列的总和值 [关闭]

SQL Server:对列的每个组值求和(或差),直到在另一列上满足条件

将查询分组为 3 列,并在 Apex Oracle 中显示另一列的总和