难以返回所需的结果。
这是我的问题:
SELECT
DATABASE()AS INVOICES_Range_$0,
count(
DISTINCT invoice_lines.invoice_header_id
)AS 'Invoice Header Count',
sum(invoice_lines.accounting_total)AS 'Dollar_Value',
100 * COUNT(
DISTINCT invoice_lines.invoice_header_id
)/(
SELECT
COUNT(
DISTINCT invoice_lines.invoice_header_id
)
FROM
invoice_lines
WHERE
(
invoice_lines. STATUS NOT LIKE '%new%'
)
AND(
invoice_lines. STATUS NOT LIKE '%voided%'
)
)AS 'Percent of All Invoices',
COUNT(approvals.approvable_id)/ count(
DISTINCT invoice_lines.invoice_header_id
)AS 'AVG_APPROVALS'
FROM
invoice_lines
LEFT JOIN approvals ON invoice_lines.invoice_header_id = approvals.approvable_id
WHERE
(
invoice_lines.accounting_total = 0
)
AND(
invoice_lines. STATUS NOT LIKE '%new%'
)
AND(
invoice_lines. STATUS NOT LIKE '%voided%'
);
此查询返回任何发票行的值为$ 0的结果。
作为参考,我们可能有一张发票,其中一行是$ 0但其他行总计$ 600。
我想只在上面的查询中包含所有发票行的总数等于$ 0。
我试过了:
SELECT
DATABASE()AS INVOICES_Range_$0,
count(
DISTINCT invoice_lines.invoice_header_id
)AS 'Invoice Header Count',
sum(invoice_lines.accounting_total)AS 'Dollar_Value',
100 * COUNT(
DISTINCT invoice_lines.invoice_header_id
)/(
SELECT
COUNT(
DISTINCT invoice_lines.invoice_header_id
)
FROM
invoice_lines
WHERE
(
invoice_lines. STATUS NOT LIKE '%new%'
)
AND(
invoice_lines. STATUS NOT LIKE '%voided%'
)
)AS 'Percent of All Invoices',
COUNT(approvals.approvable_id)/ count(
DISTINCT invoice_lines.invoice_header_id
)AS 'AVG_APPROVALS'
FROM
invoice_lines
LEFT JOIN approvals ON invoice_lines.invoice_header_id = approvals.approvable_id
WHERE
(
invoice_lines. STATUS NOT LIKE '%new%'
)
AND(
invoice_lines. STATUS NOT LIKE '%voided%'
)
HAVING
SUM(
invoice_lines.accounting_total = 0
);
然而,返回相同的结果。此外,修改为
HAVING (SUM(invoice_lines.accounting_total) < 500 )
它会返回所有发票和总金额。
我非常感谢任何帮助,因为我无法确定将结果限制为invoice_header_id的正确方法,只计算所有行总和等于0的发票。
答案 0 :(得分:1)
HAVING
SUM(
invoice_lines.accounting_total = 0
);
可能想成为
HAVING
SUM(
invoice_lines.accounting_total
) = 0
答案 1 :(得分:0)
解决方案是使用&#39; Dollar Value&#39;在WHERE子句中进行评估。在查询的前面确定。
我将SUM(invoice_lines.accounting_total)更改为TOTAL
然后在WHERE子句中我添加了AND(TOTAL = 0);
像冠军一样工作。