Laravel 查询生成器在按 ID 分组的查询中按子查询减去 COUNT
Laravel Query Builder Subtract COUNT by Subquery in query grouped by ID
我陷入了我在 Laravel 查询中遇到的这个问题。
首先,我使用这个查询来获取交易
$transactions = Transaction::select(
'customers.member_id as member_id',
DB::raw('
CASE
WHEN transactions.customer_id IS NOT NULL
THEN
customers.name
ELSE
"Tanpa pelanggan"
END AS customer_name
'),
'customers.phone as customer_phone',
DB::raw('COUNT(transactions.id) as sum_qty'),
DB::raw('SUM(final_amount) as sum_amount')
)
->join('outlets', 'outlets.id', '=', 'transactions.outlet_id')
->leftJoin('customers', 'customers.id', '=', 'transactions.customer_id')
->where('outlets.company_id', '=', $companyID)
->where(function ($q) use ($permittedOutlets) {
if (!empty($permittedOutlets)) {
$q->whereIn('transactions.outlet_id', $permittedOutlets);
}
})
->where(function ($query) use ($companyID, $outletID, $startDate, $endDate, $filterData) {
if ($outletID != 0) {
$query->where('transactions.outlet_id', '=', $outletID);
}
if (!empty($startDate) && !empty($endDate)) {
if ($startDate == $endDate) {
$query->whereDate(deviceTimestamp(), '=', $startDate);
} else {
$query->whereDate(deviceTimestamp(), '>=', $startDate)
->whereDate(deviceTimestamp(), '<=', $endDate);
}
}
if ($filterData != "") {
$query->where(function ($q) use ($filterData) {
$q->where('customers.name', 'like', '%' . $filterData . '%')
->orWhere('customers.phone', 'like', '%' . $filterData . '%');
});
}
})
->isSuccessAndVoidedInDifferentDate()
->orderBy($sortField, $sort)
->groupBy('transactions.customer_id');
以及 isSuccessAndVoidedInDifferentDate 交易模型范围
$query->whereRaw('
CASE
WHEN transactions.void_id IS NOT NULL
THEN
EXISTS (
SELECT *
FROM transactions as transactions2
WHERE transactions.void_id = transactions2.id
AND transactions.created_at <> transactions2.created_at
LIMIT 1
)
WHEN transactions.status = \''.self::STATUS_VOID.'\'
THEN
EXISTS (
SELECT *
FROM transactions as transactions2
WHERE transactions.id = transactions2.void_id
AND transactions.created_at <> transactions2.created_at
LIMIT 1
)
ELSE
TRUE
END
');
对于原始 MySql 查询
SELECT
`customers`.`member_id` AS `member_id`,
CASE
WHEN transactions.customer_id IS NOT NULL THEN customers.name
ELSE 'Tanpa pelanggan'
END AS customer_name,
`customers`.`phone` AS `customer_phone`,
COUNT(transactions.id) AS sum_qty,
SUM(final_amount) AS sum_amount
FROM
`transactions`
INNER JOIN
`outlets` ON `outlets`.`id` = `transactions`.`outlet_id`
LEFT JOIN
`customers` ON `customers`.`id` = `transactions`.`customer_id`
WHERE
`outlets`.`company_id` = 153113
AND (`transactions`.`outlet_id` IN (9164))
AND (DATE(ADDDATE(transactions.device_timestamp,
INTERVAL transactions.timezone HOUR)) >= '2020-02-01'
AND DATE(ADDDATE(transactions.device_timestamp,
INTERVAL transactions.timezone HOUR)) <= '2020-02-29')
AND CASE
WHEN
transactions.void_id IS NOT NULL
THEN
EXISTS( SELECT
*
FROM
transactions AS transactions2
WHERE
transactions.void_id = transactions2.id
AND transactions.created_at <> transactions2.created_at
LIMIT 1)
WHEN
transactions.status = 'void'
THEN
EXISTS( SELECT
*
FROM
transactions AS transactions2
WHERE
transactions.id = transactions2.void_id
AND transactions.created_at <> transactions2.created_at
LIMIT 1)
ELSE TRUE
END
AND `transactions`.`deleted_at` IS NULL
GROUP BY `transactions.customer_id`
ORDER BY `transactions.customer_id` ASC
所有交易都有两种不同的状态。状态为成功或作废。交易类型分为两类。第一类是有客户的交易,第二类是没有客户的交易。在这种情况下,没有客户的交易将被 select 编辑为 "Tanpa pelanggan"。这里的所有交易都按 transactions.customer_id.
分组
本题关注
DB::raw('COUNT(transactions.id) as sum_qty'),
在 select 部分。为了更简单的理解这里的company, outlets 和 date 的where子句可以忽略。我正在尝试创建一个条件,其中查询中的计数将 return 成功交易的计数减去按 transactions.customer_id 分组的无效交易的计数。我有点困惑是否应该使用与主查询相同的所有条件进行子查询,这有点重复,或者我还没有找到更好的解决方案。
示例案例:
数据包括:
1 笔无效交易
其余交易成功
预期结果:
member_id | customer_name | customer_phone| sum_qty | sum_ammount
NULL | Sam | 123xxxx | 1 | 20720
NULL | Tanpa Pelanggan| - | 2 | 28490
实际结果:
member_id | customer_name | customer_phone| sum_qty | sum_ammount
NULL | Sam | 123xxxx | 1 | 20720
NULL | Tanpa Pelanggan| - | 3 | 51800
我的问题是解决这个问题的最佳方法是什么?
提前致谢!
找到解决方案!
DB::raw('SUM(CASE WHEN transactions.status != \'void\' THEN 1 ELSE 0 END) - SUM(CASE WHEN transactions.status = \'void\' THEN 1 ELSE 0 END) as sum_qty')
其实就是用SUM,在SUM函数里面加上CASE条件。
我陷入了我在 Laravel 查询中遇到的这个问题。
首先,我使用这个查询来获取交易
$transactions = Transaction::select(
'customers.member_id as member_id',
DB::raw('
CASE
WHEN transactions.customer_id IS NOT NULL
THEN
customers.name
ELSE
"Tanpa pelanggan"
END AS customer_name
'),
'customers.phone as customer_phone',
DB::raw('COUNT(transactions.id) as sum_qty'),
DB::raw('SUM(final_amount) as sum_amount')
)
->join('outlets', 'outlets.id', '=', 'transactions.outlet_id')
->leftJoin('customers', 'customers.id', '=', 'transactions.customer_id')
->where('outlets.company_id', '=', $companyID)
->where(function ($q) use ($permittedOutlets) {
if (!empty($permittedOutlets)) {
$q->whereIn('transactions.outlet_id', $permittedOutlets);
}
})
->where(function ($query) use ($companyID, $outletID, $startDate, $endDate, $filterData) {
if ($outletID != 0) {
$query->where('transactions.outlet_id', '=', $outletID);
}
if (!empty($startDate) && !empty($endDate)) {
if ($startDate == $endDate) {
$query->whereDate(deviceTimestamp(), '=', $startDate);
} else {
$query->whereDate(deviceTimestamp(), '>=', $startDate)
->whereDate(deviceTimestamp(), '<=', $endDate);
}
}
if ($filterData != "") {
$query->where(function ($q) use ($filterData) {
$q->where('customers.name', 'like', '%' . $filterData . '%')
->orWhere('customers.phone', 'like', '%' . $filterData . '%');
});
}
})
->isSuccessAndVoidedInDifferentDate()
->orderBy($sortField, $sort)
->groupBy('transactions.customer_id');
以及 isSuccessAndVoidedInDifferentDate 交易模型范围
$query->whereRaw('
CASE
WHEN transactions.void_id IS NOT NULL
THEN
EXISTS (
SELECT *
FROM transactions as transactions2
WHERE transactions.void_id = transactions2.id
AND transactions.created_at <> transactions2.created_at
LIMIT 1
)
WHEN transactions.status = \''.self::STATUS_VOID.'\'
THEN
EXISTS (
SELECT *
FROM transactions as transactions2
WHERE transactions.id = transactions2.void_id
AND transactions.created_at <> transactions2.created_at
LIMIT 1
)
ELSE
TRUE
END
');
对于原始 MySql 查询
SELECT
`customers`.`member_id` AS `member_id`,
CASE
WHEN transactions.customer_id IS NOT NULL THEN customers.name
ELSE 'Tanpa pelanggan'
END AS customer_name,
`customers`.`phone` AS `customer_phone`,
COUNT(transactions.id) AS sum_qty,
SUM(final_amount) AS sum_amount
FROM
`transactions`
INNER JOIN
`outlets` ON `outlets`.`id` = `transactions`.`outlet_id`
LEFT JOIN
`customers` ON `customers`.`id` = `transactions`.`customer_id`
WHERE
`outlets`.`company_id` = 153113
AND (`transactions`.`outlet_id` IN (9164))
AND (DATE(ADDDATE(transactions.device_timestamp,
INTERVAL transactions.timezone HOUR)) >= '2020-02-01'
AND DATE(ADDDATE(transactions.device_timestamp,
INTERVAL transactions.timezone HOUR)) <= '2020-02-29')
AND CASE
WHEN
transactions.void_id IS NOT NULL
THEN
EXISTS( SELECT
*
FROM
transactions AS transactions2
WHERE
transactions.void_id = transactions2.id
AND transactions.created_at <> transactions2.created_at
LIMIT 1)
WHEN
transactions.status = 'void'
THEN
EXISTS( SELECT
*
FROM
transactions AS transactions2
WHERE
transactions.id = transactions2.void_id
AND transactions.created_at <> transactions2.created_at
LIMIT 1)
ELSE TRUE
END
AND `transactions`.`deleted_at` IS NULL
GROUP BY `transactions.customer_id`
ORDER BY `transactions.customer_id` ASC
所有交易都有两种不同的状态。状态为成功或作废。交易类型分为两类。第一类是有客户的交易,第二类是没有客户的交易。在这种情况下,没有客户的交易将被 select 编辑为 "Tanpa pelanggan"。这里的所有交易都按 transactions.customer_id.
分组本题关注
DB::raw('COUNT(transactions.id) as sum_qty'),
在 select 部分。为了更简单的理解这里的company, outlets 和 date 的where子句可以忽略。我正在尝试创建一个条件,其中查询中的计数将 return 成功交易的计数减去按 transactions.customer_id 分组的无效交易的计数。我有点困惑是否应该使用与主查询相同的所有条件进行子查询,这有点重复,或者我还没有找到更好的解决方案。
示例案例: 数据包括:
1 笔无效交易 其余交易成功
预期结果:
member_id | customer_name | customer_phone| sum_qty | sum_ammount
NULL | Sam | 123xxxx | 1 | 20720
NULL | Tanpa Pelanggan| - | 2 | 28490
实际结果:
member_id | customer_name | customer_phone| sum_qty | sum_ammount
NULL | Sam | 123xxxx | 1 | 20720
NULL | Tanpa Pelanggan| - | 3 | 51800
我的问题是解决这个问题的最佳方法是什么?
提前致谢!
找到解决方案!
DB::raw('SUM(CASE WHEN transactions.status != \'void\' THEN 1 ELSE 0 END) - SUM(CASE WHEN transactions.status = \'void\' THEN 1 ELSE 0 END) as sum_qty')
其实就是用SUM,在SUM函数里面加上CASE条件。