我有一个数据库表gl_trans
这是我的预期输出。
到目前为止,这是我的源代码。
$start_date = (!empty($_POST["start_date"])) ? ($_POST["start_date"]) : ('');
$data = $usersQuery
->select(
'gl_trans.tran_date as date2',
'gl_trans.account as account2', //GET ACCCOUNT COLUMN
DB::raw('(CASE WHEN gl_trans.amount >= 0 THEN gl_trans.amount ELSE 0 END) AS debit'), //GET DEBIT COLUMN
DB::raw('(CASE WHEN gl_trans.amount < 0 THEN -gl_trans.amount ELSE 0 END) AS credit'), // GET CREDIT COLUMN
DB::raw('(debit - credit) AS current_balance'),
DB::raw('(SELECT sum(amount) FROM gl_trans WHERE tran_date < "'.$start_date.'" AND account = account2 and amount >= 0 ) AS debit_open_balance'), //SUM OF PREVIOUS DATE OF DEBIT VALUES (POSITIVE AMOUNT)
DB::raw('(SELECT sum(amount) FROM gl_trans WHERE tran_date < "'.$start_date.'" AND account = account2 and amount < 0 ) AS credit_open_balance'), //SUM OF PREVIOUS DATE OF CREDIT VALUES (NEGATIVE AMOUNT)
DB::raw('( SELECT (CASE
WHEN current_balance >= 0
THEN (COALESCE(debit_open_balance, 0) - COALESCE(-credit_open_balance, 0) + current_balance)
ELSE (COALESCE(debit_open_balance, 0) - COALESCE(-credit_open_balance, 0) - current_balance) end)) as balance') // GET BALANCE COLUMN USING THE CURRENCT BALANCE
)
->orderBy('date2', 'ASC')
->get();
我的问题是我无法使用$ star_date来获取debit_open_balance和credit_open_balance的确切值,因为它应该获取行的tran_date,但它不是唯一的。
我尝试使用id 注意:期初余额是上一个日期(<$ start_date)的(总正金额-负总金额) 请帮助谢谢。