简体   繁体   English

如何在子查询之外使用 have 子句?

[英]How can I use having clause outside the subquery?

List item项目清单

I attached my query below and I need to filter the data based on below condition.我在下面附上了我的查询,我需要根据以下条件过滤数据。 The output is display only when the parameter is passed from the respective table.仅当参数从相应表中传递时才显示输出。

 SELECT 
    (SELECT 'Static Field') AS account_type,
    (SELECT 
            COALESCE(SUM(ln.amount), 0)
        FROM
             ln
        WHERE
                AND ln.brch_id = $P{PARA_ID}
                AND ln.date < $P{PARAM_FROM_DATE}
                AND $P{PARA_Account_Type} = 'Static Field') AS debit_amount,
    (SELECT 
            COALESCE(SUM(pd.paid_amount), 0)
        FROM
         pd,
             ln
        WHERE
            pd.loan_id = ln.id AND ln.brch_id = $P{PARA_ID}
                AND pd.paid_date < $P{PARAM_FROM_DATE}
                AND $P{PARA_Account_Type} = 'Static Field') AS credit_amount
    having 
    account_type = 'Static Field'

Output should be: I expect the sum of the values based on above different condition only when the respective parameter is passed if it does not passed the respective parameter it will not display the row输出应该是:我期望基于上述不同条件的值的总和仅当传递各自的参数时,如果不传递各自的参数,则不会显示该行

Your query is interesting because it is a SELECT without a FROM and that's why you cannot use a WHERE or a HAVING to filter.您的查询很有趣,因为它是一个没有 FROM 的 SELECT,这就是为什么您不能使用 WHERE 或 HAVING 进行过滤的原因。

When your query is a SELECT without any FROM it always returns exactly 1 record.当您的查询是不带任何 FROM 的 SELECT 时,它总是准确返回 1 条记录。

A quick fix is to just add a simple FROM with 1 row as follows to your query which then enables you to filter with a HAVING.一个快速的解决方法是在您的查询中添加一个包含 1 行的简单 FROM,然后您可以使用 HAVING 进行过滤。

FROM  ( select 1 as justarow ) r
HAVING account_type = 'Static Field'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM