获取错误:第11/13行错误:ORA-00979:不是Oracle SQL中的GROUP BY表达式

0tdrvxhp  于 2023-01-12  发布在  Oracle
关注(0)|答案(1)|浏览(135)

我收到错误:第11/13行错误:ORA-00979:不是以下代码的GROUP BY表达式。

SELECT p.project_name,
       CASE
         WHEN :P42_DATE_RANGES = 'Monthly' THEN To_char(b.date_sys, 'Month')
         WHEN :P42_DATE_RANGES = 'Daily' THEN To_char(b.date_sys, 'MM/DD/YYYY')
         WHEN :P42_DATE_RANGES = 'Weekly' THEN To_char(Trunc(b.date_sys, 'IW'), 'MM/DD/YYYY')
       END AS my_date,
       CASE
         WHEN :P42_DATE_RANGES = 'Monthly' THEN Trunc(b.date_sys, 'MM')
         WHEN :P42_DATE_RANGES = 'Daily' THEN Trunc(b.date_sys)
         WHEN :P42_DATE_RANGES = 'Weekly' THEN Trunc(b.date_sys, 'IW')
       END AS Dates_,
       Count(DISTINCT i.id)       AS count_of_ingested,
       SUM(hrc.highlighted_count) AS count_of_highlighted,
       SUM(hrc.redacted_count)    AS count_of_redacted
FROM   customer c
       inner join project p
               ON c.id = p.id_customer
       inner join batch b
               ON p.id = b.id_project
       left outer join ingested i
                    ON b.id = i.id_batch
       left outer join (SELECT hc.id AS id_ingested,
                               highlighted_count,
                               redacted_count
                        FROM  (SELECT i.id,
                                      Count(DISTINCT h.id) AS highlighted_count
                               FROM   ingested i
                                      left outer join highlighted h
                                                   ON i.id = h.id_ingested
                               GROUP  BY i.id) hc
                              join (SELECT i.id,
                                           Count(DISTINCT r.id) AS
                                           redacted_count
                                    FROM   ingested i
                                           left outer join redacted r
                                                        ON i.id = r.id_ingested
                                    GROUP  BY i.id) rc
                                ON hc.id = rc.id) hrc
                    ON i.id = hrc.id_ingested
WHERE  b.date_sys BETWEEN To_date(:P42_START_DATE) AND To_date(:P42_END_DATE)
GROUP  BY p.project_name,
          CASE
            WHEN :P42_DATE_RANGES = 'Monthly' THEN To_char(b.date_sys, 'Month')
            WHEN :P42_DATE_RANGES = 'Daily' THEN
            To_char(b.date_sys, 'MM/DD/YYYY')
            WHEN :P42_DATE_RANGES = 'Weekly' THEN
            To_char(Trunc(b.date_sys, 'IW'), 'MM/DD/YYYY')
          END

当我将Dates_添加到group by的最后时,这似乎也没有帮助,因为我得到了错误:第42/26行错误:ORA-00904:"日期":无效标识符

mrfwxfqh

mrfwxfqh1#

不能将列alias添加到group by子句中;使用原始列名或整个表达式:

SELECT 
       -- these are non-aggregated columns (or expressions) which should be
       -- part of the GROUP BY clause:
         p.project_name,
         CASE
            WHEN :p42_date_ranges = 'Monthly'
            THEN
               TO_CHAR (b.date_sys, 'Month')
            WHEN :p42_date_ranges = 'Daily'
            THEN
               TO_CHAR (b.date_sys, 'MM/DD/YYYY')
            WHEN :p42_date_ranges = 'Weekly'
            THEN
               TO_CHAR (TRUNC (b.date_sys, 'IW'), 'MM/DD/YYYY')
         END AS my_date,
         CASE
            WHEN :p42_date_ranges = 'Monthly' THEN TRUNC (b.date_sys, 'MM')
            WHEN :p42_date_ranges = 'Daily' THEN TRUNC (b.date_sys)
            WHEN :p42_date_ranges = 'Weekly' THEN TRUNC (b.date_sys, 'IW')
         END AS dates_,
       --
       -- aggregates
         COUNT (DISTINCT i.id) AS count_of_ingested,
         SUM (hrc.highlighted_count) AS count_of_highlighted,
         SUM (hrc.redacted_count) AS count_of_redacted
    FROM customer c
         INNER JOIN project p ON c.id = p.id_customer
         INNER JOIN batch b ON p.id = b.id_project
         LEFT OUTER JOIN ingested i ON b.id = i.id_batch
         LEFT OUTER JOIN
         (SELECT hc.id AS id_ingested, highlighted_count, redacted_count
            FROM (  SELECT i.id, COUNT (DISTINCT h.id) AS highlighted_count
                      FROM ingested i
                           LEFT OUTER JOIN highlighted h ON i.id = h.id_ingested
                  GROUP BY i.id) hc
                 JOIN
                 (  SELECT i.id, COUNT (DISTINCT r.id) AS redacted_count
                      FROM ingested i
                           LEFT OUTER JOIN redacted r ON i.id = r.id_ingested
                  GROUP BY i.id) rc
                    ON hc.id = rc.id) hrc
            ON i.id = hrc.id_ingested
   WHERE b.date_sys BETWEEN TO_DATE ( :p42_start_date)
                        AND TO_DATE ( :p42_end_date)

group by子句:

GROUP BY p.project_name,
         CASE
            WHEN :p42_date_ranges = 'Monthly'
            THEN
               TO_CHAR (b.date_sys, 'Month')
            WHEN :p42_date_ranges = 'Daily'
            THEN
               TO_CHAR (b.date_sys, 'MM/DD/YYYY')
            WHEN :p42_date_ranges = 'Weekly'
            THEN
               TO_CHAR (TRUNC (b.date_sys, 'IW'), 'MM/DD/YYYY')
         END,   -- AS my_date,
         CASE
            WHEN :p42_date_ranges = 'Monthly' THEN TRUNC (b.date_sys, 'MM')
            WHEN :p42_date_ranges = 'Daily' THEN TRUNC (b.date_sys)
            WHEN :p42_date_ranges = 'Weekly' THEN TRUNC (b.date_sys, 'IW')
         END;   -- AS dates_

相关问题