一个具有多个条件的SQL查询

时间:2019-02-14 14:35:43

标签: sql oracle

我正在运行一个Oracle数据库,下面有两个表。

#account
+----------------------------------+
| acc_id | date       | acc_type   |
+--------+------------+------------+
| 1      | 11-07-2018 | customer   |
| 2      | 01-11-2018 | customer   |
| 3      | 02-09-2018 | employee   |
| 4      | 01-09-2018 | customer   |
+--------+------------+------------+

#credit_request
+-----------------------------------------------------------------+
| credit_id  |   date      | credit_type | acc_id | credit_amount |
+------------+-------------+----------   +--------+
| 1112       | 01-08-2018  | failed      |  1     |    2200       |
| 1214       | 02-12-2018  | success     |  2     |    1500       |
| 1312       | 03-11-2018  | success     |  4     |    8750       | 
| 1468       | 01-12-2018  | failed      |  2     |    3500       |
+------------+-------------+-------------+--------+---------------+

想要为每个客户提供以下服务:

  • 最近一次成功的credit_request
  • 所有失败的credit_requests的credit_amount金额

3 个答案:

答案 0 :(得分:2)

这是一种方法:

select a.acct_id, acr.num_fails,
       acr.num_successes / nullif(acr.num_fails) as ratio,  -- seems weird.  Why not just the failure rate?
       last_cr.credit_id, last_cr.date, last_cr.credit_amount
from account a left join
     (select acc_id, 
             sum(case when credit_type = 'failed' then 1 else 0 end) as num_fails,
             sum(case when credit_type = 'failed' then credit_amount else 0 end) as num_fails,
             sum(case when credit_type = 'success' then 1 else 0 end) as num_successes
             max(case when credit_type = 'success' then date else 0 end) as max_success_date
     from credit_request
     group by acct_id
    ) acr left join
    credit_request last_cr
    on last_cr.acct_id = acr.acct_id and last_cr.date = acr.date;

答案 1 :(得分:1)

以下查询应该可以解决问题。

use Google\Cloud\Vision\V1\Feature\Type;
$res = $v->annotateImage(file_get_contents($fileName), [
    Type::LABEL_DETECTION,
    Type::SAFE_SEARCH_DETECTION
]);

$labels = $res->getLabelAnnotations();
$safeSearch = $res->getSafeSearchAnnotation();

子查询使用SELECT acc_id, MAX(CASE WHEN credit_type = 'success' AND rn = 1 THEN credit_id END) as last_successfull_credit_id, MAX(CASE WHEN credit_type = 'success' AND rn = 1 THEN cdate END) as last_successfull_credit_date, MAX(CASE WHEN credit_type = 'success' AND rn = 1 THEN credit_amount END) as last_successfull_credit_amount, SUM(CASE WHEN credit_type = 'failed' THEN credit_amount ELSE 0 END) total_amount_of_failed_credit, SUM(CASE WHEN credit_type = 'failed' THEN 1 ELSE 0 END) / COUNT(*) ratio_success_request FROM ( SELECT a.acc_id, a.cdate adate, a.acc_type, c.credit_id, c.cdate, c.credit_type, c.credit_amount, ROW_NUMBER() OVER(PARTITION BY c.acc_id, c.credit_type ORDER BY c.cdate DESC) rn FROM account a LEFT JOIN credit_request c ON c.acc_id = a.acc_id ) x GROUP BY acc_id ORDER BY acc_id 为帐户和贷方类型组中的每个记录分配一个序列。外部查询会进行条件聚合以计算您要求的其他计算。

Db Fiddle demo 与您的测试数据一起返回:

ACC_ID | LAST_SUCCESSFULL_CREDIT_ID | LAST_SUCCESSFULL_CREDIT_DATE | LAST_SUCCESSFULL_CREDIT_AMOUNT | TOTAL_AMOUNT_OF_FAILED_CREDIT | RATIO_SUCCESS_REQUEST
-----: | -------------------------: | :--------------------------- | -----------------------------: | ----------------------------: | --------------------:
     1 |                       null | null                         |                           null |                          2200 |                     1
     2 |                       1214 | 02-DEC-18                    |                           1500 |                          3500 |                    .5
     3 |                       null | null                         |                           null |                             0 |                     0
     4 |                       1312 | 03-NOV-18                    |                           8750 |                             0 |                     0

这可能是您要寻找的...由于您未显示预期结果,因此可能不是100%准确,请随时进行调整。

答案 2 :(得分:0)

我猜下面的查询很容易理解和实现。另外,为避免在$hash.getEnumerator() | sort-object -Property value -Desc | foreach { Write-Host ("Key = " + $_.key + " and Value = " + $_.value); } 语句中使用越来越多的术语,您可以仅使用Key = a and Value = 3 Key = b and Value = 2 Key = c and Value = 1 子句,并在CASE语句中使用它来减小查询大小。

WITH
相关问题