在 Oracle SQL 中使用 DECODE 进行 GROUP BY (ORA-00979)
GROUP BY with DECODE in Oracle SQL (ORA-00979)
我刚刚在 Whosebug 和其他网站上阅读了很多关于我的问题的已解决问题,但我仍然没有明白。我无法执行附加的代码,我收到 "ORA-00979" 错误,但我不明白为什么。我读到我不必在我的 GROUP BY 订单中列出 DECODE 或 COUNT 子句。我仍然得到错误。有谁知道为什么?
SELECT DISTINCT
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1,
DECODE(receipt.table_name, 'PAYMENT', SUM(COUNT(receipt.receipt_id))) as inpayment
FROM
company
JOIN customer ON company.company_id = customer.company_id
JOIN debtor ON customer.customer_id = debtor.customer_id
JOIN debtortrunk ON debtor.debtor_id = debtortrunk.debtor_id
JOIN receipt ON debtor.customer_id = receipt.customer_id AND debtor.deb_id = receipt.deb_id
WHERE
receipt.created >= '24.01.2018' AND
receipt.created <= '28.01.2018'
GROUP BY
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1
您不能使用包含另一个聚合函数的聚合函数。
如果我没理解错你可以试试用条件聚合函数来做。
SELECT
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1,
SUM(CASE WHEN receipt.table_name = 'PAYMENT' AND receipt.receipt_id IS NOT NULL THEN 1 ELSE 0 END) as inpayment
FROM
company
JOIN customer ON company.company_id = customer.company_id
JOIN debtor ON customer.customer_id = debtor.customer_id
JOIN debtortrunk ON debtor.debtor_id = debtortrunk.debtor_id
JOIN receipt ON debtor.customer_id = receipt.customer_id AND debtor.deb_id = receipt.deb_id
WHERE
receipt.created >= '24.01.2018' AND
receipt.created <= '28.01.2018'
GROUP BY
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1
如果您将聚合函数与 group by
一起使用,则 distinct 没有意义,因此可能会被删除。
你可以用 LEFT JOIN
做你想做的事,在 SELECT
中没有条件逻辑:
SELECT co.company_id, co.companyname_1,
cu.customer_id, cu.customername_1,
COUNT(r.receipt_id) as inpayment
FROM company co JOIN
customer cu
ON co.company_id = cu.company_id JOIN
debtor d
ON cu.customer_id = d.customer_id JOIN
debtortrunk dt
ON d.debtor_id = dt.debtor_id LEFT JOIN
receipt r
ON d.customer_id = r.customer_id AND
d.deb_id = r.deb_id AND
r.table_name = 'PAYMENT'
WHERE r.created >= DATE '2018-01-24' AND
r.created <= DATE '2018-01-28'
GROUP BY co.company_id, co.companyname_1,
cu.customer_id, cu.customername_1;
备注:
- 您可能不需要
debtortrunk
table。它似乎没有在查询中使用。
- 使用 table 别名,使查询更易于编写和阅读。
- 使用
DATE
引入日期常量。这样,文字值就有了正确的类型。
ORA-00979: not a group by expression
表示在您的 SELECT 中有一些表达式未作为组表达式包含,也未参与聚合函数。在这种情况下,它肯定是 receipt.table_name
引用。
最直接的解决方案是将其显式包含为聚合字段:
GROUP BY
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1,
receipt.table_name
我刚刚在 Whosebug 和其他网站上阅读了很多关于我的问题的已解决问题,但我仍然没有明白。我无法执行附加的代码,我收到 "ORA-00979" 错误,但我不明白为什么。我读到我不必在我的 GROUP BY 订单中列出 DECODE 或 COUNT 子句。我仍然得到错误。有谁知道为什么?
SELECT DISTINCT
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1,
DECODE(receipt.table_name, 'PAYMENT', SUM(COUNT(receipt.receipt_id))) as inpayment
FROM
company
JOIN customer ON company.company_id = customer.company_id
JOIN debtor ON customer.customer_id = debtor.customer_id
JOIN debtortrunk ON debtor.debtor_id = debtortrunk.debtor_id
JOIN receipt ON debtor.customer_id = receipt.customer_id AND debtor.deb_id = receipt.deb_id
WHERE
receipt.created >= '24.01.2018' AND
receipt.created <= '28.01.2018'
GROUP BY
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1
您不能使用包含另一个聚合函数的聚合函数。
如果我没理解错你可以试试用条件聚合函数来做。
SELECT
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1,
SUM(CASE WHEN receipt.table_name = 'PAYMENT' AND receipt.receipt_id IS NOT NULL THEN 1 ELSE 0 END) as inpayment
FROM
company
JOIN customer ON company.company_id = customer.company_id
JOIN debtor ON customer.customer_id = debtor.customer_id
JOIN debtortrunk ON debtor.debtor_id = debtortrunk.debtor_id
JOIN receipt ON debtor.customer_id = receipt.customer_id AND debtor.deb_id = receipt.deb_id
WHERE
receipt.created >= '24.01.2018' AND
receipt.created <= '28.01.2018'
GROUP BY
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1
如果您将聚合函数与 group by
一起使用,则 distinct 没有意义,因此可能会被删除。
你可以用 LEFT JOIN
做你想做的事,在 SELECT
中没有条件逻辑:
SELECT co.company_id, co.companyname_1,
cu.customer_id, cu.customername_1,
COUNT(r.receipt_id) as inpayment
FROM company co JOIN
customer cu
ON co.company_id = cu.company_id JOIN
debtor d
ON cu.customer_id = d.customer_id JOIN
debtortrunk dt
ON d.debtor_id = dt.debtor_id LEFT JOIN
receipt r
ON d.customer_id = r.customer_id AND
d.deb_id = r.deb_id AND
r.table_name = 'PAYMENT'
WHERE r.created >= DATE '2018-01-24' AND
r.created <= DATE '2018-01-28'
GROUP BY co.company_id, co.companyname_1,
cu.customer_id, cu.customername_1;
备注:
- 您可能不需要
debtortrunk
table。它似乎没有在查询中使用。 - 使用 table 别名,使查询更易于编写和阅读。
- 使用
DATE
引入日期常量。这样,文字值就有了正确的类型。
ORA-00979: not a group by expression
表示在您的 SELECT 中有一些表达式未作为组表达式包含,也未参与聚合函数。在这种情况下,它肯定是 receipt.table_name
引用。
最直接的解决方案是将其显式包含为聚合字段:
GROUP BY
company.company_id,
company.companyname_1,
customer.customer_id,
customer.customername_1,
receipt.table_name