非常抱歉,我之前的回答仍然有误。导致出现SQL命令未正确结束的错误的原因是在查询语句的最后缺少了一个分号。以下是修正后的示例代码:
SELECT
subquery.name,
subquery.org_name,
subquery.billtypename,
subquery.count_distinct,
subquery.count_total,
CASE
WHEN subquery.count_total = 0 THEN NULL
ELSE (subquery.count_distinct / subquery.count_total) * 100
END AS percentage
FROM
(
SELECT
d.name AS name,
org.name AS org_name,
c.BILLTYPENAME AS billtypename,
COUNT(DISTINCT a.billno) AS count_distinct,
COUNT(a.billno) AS count_total
FROM
ncc.ssctp_flowpath a
LEFT JOIN ncc.bd_billtype c ON c.pk_billtypeid = a.pk_tradetype
LEFT JOIN ncc.ssctp_workinggroup d ON d.pk_workinggroup = a.pk_sscgroup
LEFT JOIN ncc.org_orgs org ON org.pk_org = a.pk_org
WHERE
a.operatetime >= '2023-01-01 00:00:00'
GROUP BY
d.name, org.name, c.BILLTYPENAME
) AS subquery;
请注意,在内部查询和外部查询之间需要加上分号以结束语句。执行这段修正后的代码,应该可以得到正确的结果。非常抱歉给您带来困扰。如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |