You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

统计特定范围内客户数量及占比的SQL报错(ORA-01747)排查

问题:统计特定范围内运营商数量及占比(ORA-01747错误排查)

需求:统计总部特定范围内的运营商数量,并展示其占比。需统计距离≤3AU的运营商总数、对应数量及占比,期望输出格式如下:

Total OperatorsOperators Within 3 AUPercentage Within 3 AU
1001010%

当前执行的SQL及错误信息如下:

错误SQL

SELECT
    DISTINCT COUNT(OperatorAU.*) AS "Total Operators",
100 * (COUNT(OperatorAU.UnderAU) / COUNT(OperatorAU.*) AS "Operators Within 3 AU (%)"
FROM
(
    SELECT DISTINCT 
        Operators.DestionationID AS DestinationID,
        Operators.OperatorName AS OperatorName,
        (ABS(Destinations.SolarCoordinateX) / 5) + (ABS(Destinations.SolarCoordinateY) / 5) AS DistanceInAU,
        CASE
            WHEN (ABS(Destinations.SolarCoordinateX) / 5) + (ABS(Destinations.SolarCoordinateY) / 5) < 3 THEN 1
            ELSE 0
        END AS UnderAU
    FROM Operators
    INNER JOIN Destinations ON Operators.DestinationID = Destinations.DestinationID
    ORDER BY DistanceInAU ASC
) AS OperatorAU;

错误信息

Error at Command Line : 2 Column : 31
Error report -
SQL Error: ORA-01747: invalid user.table.column, table.column, or column specification
01747. 00000 - "invalid user.table.column, table.column, or column specification"
*Cause:
*Action:

已核对所有ID与表匹配,请求排查错误并修正SQL。


错误分析及修正SQL

错误原因

  1. 语法缺失:外层SELECT中100 * (COUNT(OperatorAU.UnderAU) / COUNT(OperatorAU.*)缺少闭合右括号,导致列定义语法混乱,触发ORA-01747。
  2. DISTINCT滥用:外层SELECT中DISTINCT与聚合函数COUNT()混用无意义,聚合函数本身返回单一统计值,无需去重。
  3. 子查询冗余:子查询中的ORDER BY作为派生表时无意义(Oracle不允许派生表带ORDER BY,除非配合ROWNUM),且重复计算距离表达式可优化。
  4. 统计逻辑错误:COUNT(OperatorAU.UnderAU)会统计所有非NULL值(这里UnderAU是1或0,都会被计数),应使用SUM(UnderAU)统计符合条件的数量。
  5. 拼写错误:子查询中Operators.DestionationID拼写错误,应为DestinationID。

修正后的SQL

SELECT
    COUNT(*) AS "Total Operators",
    SUM(OperatorAU.UnderAU) AS "Operators Within 3 AU",
    ROUND(100 * SUM(OperatorAU.UnderAU) / COUNT(*), 1) || '%' AS "Percentage Within 3 AU"
FROM
(
    SELECT DISTINCT 
        Operators.DestinationID,
        Operators.OperatorName,
        CASE
            WHEN (ABS(Destinations.SolarCoordinateX) / 5) + (ABS(Destinations.SolarCoordinateY) / 5) < 3 THEN 1
            ELSE 0
        END AS UnderAU
    FROM Operators
    INNER JOIN Destinations ON Operators.DestinationID = Destinations.DestinationID
) AS OperatorAU;

修正说明

  • 补全外层SELECT缺失的右括号,修复语法结构。
  • 移除外层无意义的DISTINCT。
  • 将COUNT(UnderAU)改为SUM(UnderAU),通过求和1的数量统计符合距离条件的运营商。
  • 用ROUND()控制百分比精度,拼接%符号匹配期望输出格式。
  • 移除子查询中冗余的ORDER BY和不需要的DistanceInAU列。
  • 修正子查询中DestinationID的拼写错误。

内容的提问来源于stack exchange,提问作者LeeMo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 18:20:34