我有这个查询
SELECT
invoice.number,
invoice.amount,
invoice.paid,
iif (invoice.paid='Y', Max(payments.datePay), '') as invoice_paid
FROM
payments
INNER JOIN
invoice
ON (invoices.number = payments.numberDoc AND payments.type = 'Invoice')
WHERE
payments.type= 'Invoice'
GROUP BY
number,
amount,
paid
运行良好。
示例结果:
number | amount | paid | invoice_paid
00000003 | 347,94€ | Y | 23/05/2014
00000004 | 462,65€ | N |
00000005 | 462,65€ | Y | 13/08/2014
00000006 | 453,88€ | Y | 03/09/2014
00000007 | 155,57€ | Y | 04/09/2014
00000008 | 500,00€ | N |
但现在我需要过滤“invoice_paid”日期。
如果我执行此操作:
SELECT
invoice.number,
invoice.amount,
invoice.paid,
iif (invoice.paid='Y', Max(payments.datePay), '') as invoice_paid
FROM
payments
INNER JOIN
invoice
ON (invoices.number = payments.numberDoc AND payments.type = 'Invoice')
WHERE
payments.type= 'Invoice' AND invoice_paid=#01/01/2014#
GROUP BY
number,
amount,
paid
显示窗口:“引入参数invoice_paid”。
如何根据iif函数返回的结果过滤结果?
答案 0 :(得分:3)
其他答案是准确的,但让我试着为你澄清一些事情,然后问一个问题......
您为“invoice_paid”计算的列不能在其子句的别名结果列的where子句中使用,您必须使用表本身的原始原始日期,例如
WHERE payments.type= 'Invoice' AND payments.datePay=#01/01/2014#
并且你不能在where子句中使用MAX(payments.datepay)之类的聚合,这必须在用户iDevlop注意到的GROUPING子句之后的HAVING子句中。
您有特定日期的过滤器(基于=),但您的数据都没有显示这样的日期。因此,这不仅可能不会通过WHERE或HAVING方法返回结果(或非常少)。如果您正在寻找特定日期的付款,我会直接查询并显示。
现在,问题。您是否只想要一个日期,或者您是否真的想要获得具有最大(日期支付)大于或等于2014年1月1日的发票的任何内容?如果是,请将相等测试更改为> =#01/01/2014#
答案 1 :(得分:0)
您必须在WHERE条件中使用整个表达式,只需使用别名将无效,但将导致输入参数值框。所以,
SELECT
invoice.[number],
invoice.[amount],
invoice.[paid],
IIF (invoice.paid='Y', Max(payments.datePay), '') as invoice_paid
FROM
payments
INNER JOIN
invoice
ON (invoices.number = payments.numberDoc AND payments.type = 'Invoice')
WHERE
payments.type= 'Invoice'
AND
IIF (invoice.paid='Y', Max(payments.datePay), '') = #01/01/2014#
GROUP BY
[number],
[amount],
[paid]
或创建一个Query,并根据此Query创建另一个Query以获取正确的数据。或SubQuery。
答案 2 :(得分:0)
我认为您不希望WHERE
在此处,而是HAVING
,因为您希望在聚合后过滤值。
尝试使用:
where payments.type='Invoice'
GROUB BY ...
HAVING iif(invoice.paid='Y',max(payments.datePay),'') = #01/01/2014#