2

我正在尝试对自定义报告运行查询,该报告应该根据指定日期范围内的贷款还款类型为符合五个条件的客户返回数据。标准是Salary DeductionBank Standing OrderSelf-PayPost Dated ChequesBank Debit结果应返回来自指定的每个条件的客户端数量的计数。但是,这不是我目前得到的结果;这些查询正在针对这个软件Mambu进行测试。结果取决于一个customfieldcustomfieldvalue它指定要使用的还款类型)列。这就是我期望的结果应该是这样的:

++++++++++++++++++++++++++++++++++++++++++++++++++++++
| LoanProduct           | RepaymentType    | Clients |
++++++++++++++++++++++++++++++++++++++++++++++++++++++
| JUMPSTART LOAN WEEKLY | Self-Pay         | 35      |
------------------------------------------------------
| PAYDAY LOAN MONTHLY   | Salary Deduction | 5       |
------------------------------------------------------
| MICRO-BIZ LOAN        | Bank Debit       | 26      |
------------------------------------------------------
| PAYDAY LOAN WEEKLY    | Self-Pay         | 1       |

  .......
and so on ...

到目前为止我尝试过的解决方案:

QUERY #1:
对于这个查询,我只是想找回所有的客户,而不需要计算特定的还款类型;它适用于那种情况。

SELECT CONCAT(client.FIRSTNAME, ' ', client.LASTNAME) AS Client, 
    CONCAT(user.FIRSTNAME, ' ', user.LASTNAME) AS Originator, 
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY 
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY 
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND customfieldvalue.VALUE = "Bank Debit"
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME

QUERY #1.1
在上面 添加 anOR不会产生任何结果:

SELECT CONCAT(client.FIRSTNAME, ' ', client.LASTNAME) AS Client, 
    CONCAT(user.FIRSTNAME, ' ', user.LASTNAME) AS Originator, 
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY 
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY 
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND customfieldvalue.VALUE = "Bank Debit" OR customfieldvalue.VALUE = "Self-Pay"
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME

QUERY #2
我什至尝试使用CASE语句执行此操作,但它只返回Clientsas的所有行NULL并且需要大约 12.1 秒才能完成。

SELECT
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType,
    CASE
       WHEN customfieldvalue.VALUE = "Salary Deduction" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
       WHEN customfieldvalue.VALUE = "Bank Standing Order" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
       WHEN customfieldvalue.VALUE = "Self-Pay" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
       WHEN customfieldvalue.VALUE = "Post Dated Cheques" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
       WHEN customfieldvalue.VALUE = "Bank Debit" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
    END AS Clients
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON  loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME

我在这里做错了什么,这阻止了我得到想要的结果?提前致谢。

4

3 回答 3

1

对于查询 2,您需要添加括号,或使用in语法:

AND customfieldvalue.VALUE in ('Bank Debit', 'Self-Pay')

与正确使用 where 语法一样重要的是修复子句join中的语法:from

FROM client join
     user
     on client.ASSIGNEDUSERKEY = user.ENCODEDKEY join
     loanproduct
     ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY and
        loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY CROSS JOIN
    customFieldValue
where loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31" and
      customfieldvalue.value in ('Bank Debit')

天哪,您会立即看到可能有问题。您通常不希望在设计良好的数据仓库中交叉连接表。CustomFieldValue 如何连接到其他表?

于 2013-01-21T03:50:57.897 回答
0

报价(操作顺序)在这里非常重要,你错过了它们,这就是问题所在。

SELECT CONCAT(client.FIRSTNAME, ' ', client.LASTNAME) AS Client, 
    CONCAT(user.FIRSTNAME, ' ', user.LASTNAME) AS Originator, 
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY 
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY 
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND (customfieldvalue.VALUE = "Bank Debit" OR customfieldvalue.VALUE = "Self-Pay")
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME

另一种方法是使用“IN()”

SELECT CONCAT(client.FIRSTNAME, ' ', client.LASTNAME) AS Client, 
    CONCAT(user.FIRSTNAME, ' ', user.LASTNAME) AS Originator, 
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY 
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY 
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND customfieldvalue.VALUE IN("Bank Debit", "Self-Pay")
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME
于 2013-01-21T03:48:40.670 回答
0

有几个项目脱颖而出。首先,您似乎正在尝试加入 4 个文件,但只定义了 1 个连接 - 在构建复杂连接时,一次添加 1 个文件有时会有所帮助。

此外,您正在使用 GROUP BY 而不汇总任何内容 - 您确定这是您想要的吗?如果您想要一个可以使用的计数count(*),那么所有其他列都需要包含在 GROUP BY 子句中。

于 2013-01-21T03:56:30.337 回答