奇数“分组依据”使用Oracle时子查询出错

发布于 2024-11-10 04:55:12 字数 1116 浏览 4 评论 0原文

简单的背景知识——我需要找到通过我们公司实验室的案件的终止率,并按案件类型和月份分组。到目前为止,我想出了这个:

SELECT BPI.TYPE,
       EXTRACT(MONTH FROM CS.RECEIVED_DATE) MONTH,
       COUNT(*) termed_cases
  FROM CELL_SOURCE cs
  JOIN BASIC_PATHOLOGY_INFO bpi ON CS.CELL_SOURCE_ID = BPI.CELL_SOURCE_ID
  JOIN RECENT_CELL_SOURCE_STATUS rcss ON CS.CELL_SOURCE_ID = RCSS.CELL_SOURCE_ID
 WHERE type IS NOT NULL
  AND CS.RECEIVED_DATE > to_date('03/01/2011', 'MM/DD/YYYY/')
  AND RCSS.STATUS like 'Term%'
GROUP BY BPI.TYPE, EXTRACT(MONTH FROM CS.RECEIVED_DATE)
ORDER BY month, type

这很容易找到所有已被命名的案例。然而,当我想找到汇率时,我遇到了一些问题。我尝试使用子查询来捕获每种类型的案例总数,无论其状态如何,如下所示:

COUNT(*)/(SELECT COUNT(*) 
            FROM CELL_SOURCE cs_1 
            JOIN BASIC_PATHOLOGY_INFO bpi_1 ON CS_1.CELL_SOURCE_ID = BPI_1.CELL_SOURCE_ID
          WHERE BPI_1.TYPE = BPI.TYPE 
            AND EXTRACT(month from CS_1.RECEIVED_DATE) = EXTRACT(MONTH FROM CS.RECEIVED_DATE)) termed_cases

但是,这会引发 ORA-00979: not a GROUP BY expression 错误,并突出显示 <来自子查询的 code>BPI.TYPE。

有人知道我的错误实际上可能是什么吗?另外,分析函数在这里会比聚合函数更好吗?

A quick background -- I need to find the termination rates of cases that go through our company lab grouped by case type and month. So far I came up with this:

SELECT BPI.TYPE,
       EXTRACT(MONTH FROM CS.RECEIVED_DATE) MONTH,
       COUNT(*) termed_cases
  FROM CELL_SOURCE cs
  JOIN BASIC_PATHOLOGY_INFO bpi ON CS.CELL_SOURCE_ID = BPI.CELL_SOURCE_ID
  JOIN RECENT_CELL_SOURCE_STATUS rcss ON CS.CELL_SOURCE_ID = RCSS.CELL_SOURCE_ID
 WHERE type IS NOT NULL
  AND CS.RECEIVED_DATE > to_date('03/01/2011', 'MM/DD/YYYY/')
  AND RCSS.STATUS like 'Term%'
GROUP BY BPI.TYPE, EXTRACT(MONTH FROM CS.RECEIVED_DATE)
ORDER BY month, type

This finds all cases that have been termed, easy enough. However, when I want to find the rate, I get a bit of a problem. I tried using a sub-query to catch the total amount of cases per type regardless of it's status, as such:

COUNT(*)/(SELECT COUNT(*) 
            FROM CELL_SOURCE cs_1 
            JOIN BASIC_PATHOLOGY_INFO bpi_1 ON CS_1.CELL_SOURCE_ID = BPI_1.CELL_SOURCE_ID
          WHERE BPI_1.TYPE = BPI.TYPE 
            AND EXTRACT(month from CS_1.RECEIVED_DATE) = EXTRACT(MONTH FROM CS.RECEIVED_DATE)) termed_cases

However, this throws an ORA-00979: not a GROUP BY expression error, and highlights BPI.TYPE from the sub-query.

Anyone have any idea what my error could actually be? Also, would an analytical function work better here than an aggregate function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-11-17 04:55:12

因此,您需要两个计数:当月所有案例的总数和仅术语案例的总数。最简单的方法是使用 CASE() 函数执行条件计数,如下所示:

SELECT BPI.TYPE,
       EXTRACT(MONTH FROM CS.RECEIVED_DATE) MONTH,
       COUNT(*) all_cases,
       sum(case when RCSS.STATUS like 'Term%' then 1 else 0 end ) termed_cases
  FROM CELL_SOURCE cs
  JOIN BASIC_PATHOLOGY_INFO bpi ON CS.CELL_SOURCE_ID = BPI.CELL_SOURCE_ID
  JOIN RECENT_CELL_SOURCE_STATUS rcss ON CS.CELL_SOURCE_ID = RCSS.CELL_SOURCE_ID
 WHERE tumor_type IS NOT NULL
  AND CS.RECEIVED_DATE > to_date('03/01/2011', 'MM/DD/YYYY/')
GROUP BY BPI.TUMOR_TYPE, EXTRACT(MONTH FROM CS.RECEIVED_DATE)
ORDER BY month, tumor_type

请注意,我已从 WHERE 子句中删除了 LIKE 过滤器。

So, you need two counts: a total for all cases in the month and a total for just the Termed cases. The easiest way of doing this is to use a CASE() function to execute a conditional count, like this:

SELECT BPI.TYPE,
       EXTRACT(MONTH FROM CS.RECEIVED_DATE) MONTH,
       COUNT(*) all_cases,
       sum(case when RCSS.STATUS like 'Term%' then 1 else 0 end ) termed_cases
  FROM CELL_SOURCE cs
  JOIN BASIC_PATHOLOGY_INFO bpi ON CS.CELL_SOURCE_ID = BPI.CELL_SOURCE_ID
  JOIN RECENT_CELL_SOURCE_STATUS rcss ON CS.CELL_SOURCE_ID = RCSS.CELL_SOURCE_ID
 WHERE tumor_type IS NOT NULL
  AND CS.RECEIVED_DATE > to_date('03/01/2011', 'MM/DD/YYYY/')
GROUP BY BPI.TUMOR_TYPE, EXTRACT(MONTH FROM CS.RECEIVED_DATE)
ORDER BY month, tumor_type

Note that I have removed the LIKE filter from the WHERE clause.

林空鹿饮溪 2024-11-17 04:55:12
select bpi.type , month, 
       termed_cases / 
   (select count(*) 
    from CELL_SOURCE cs_1 
    inner join BASIC_PATHOLOGY_INFO bpi_1 
        on CS_1.CELL_SOURCE_ID = BPI_1.CELL_SOURCE_ID
    where BPI_1.TUMOR_TYPE = BPI.TUMOR_TYPE 
    and extract(month from CS_1.RECEIVED_DATE) = extract(MONTH FROM CS.RECEIVED_DATE)
    )
from (
select BPI.TYPE,
extract(MONTH FROM CS.RECEIVED_DATE) MONTH,
count(*) termed_cases
from CELL_SOURCE cs
inner join BASIC_PATHOLOGY_INFO bpi
    on CS.CELL_SOURCE_ID = BPI.CELL_SOURCE_ID
inner join RECENT_CELL_SOURCE_STATUS rcss
    on CS.CELL_SOURCE_ID = RCSS.CELL_SOURCE_ID
where tumor_type is not null
and CS.RECEIVED_DATE > to_date('03/01/2011', 'MM/DD/YYYY/')
and RCSS.STATUS like 'Term%'
group by BPI.TYPE, extract(MONTH FROM CS.RECEIVED_DATE)
)
order by month, type
select bpi.type , month, 
       termed_cases / 
   (select count(*) 
    from CELL_SOURCE cs_1 
    inner join BASIC_PATHOLOGY_INFO bpi_1 
        on CS_1.CELL_SOURCE_ID = BPI_1.CELL_SOURCE_ID
    where BPI_1.TUMOR_TYPE = BPI.TUMOR_TYPE 
    and extract(month from CS_1.RECEIVED_DATE) = extract(MONTH FROM CS.RECEIVED_DATE)
    )
from (
select BPI.TYPE,
extract(MONTH FROM CS.RECEIVED_DATE) MONTH,
count(*) termed_cases
from CELL_SOURCE cs
inner join BASIC_PATHOLOGY_INFO bpi
    on CS.CELL_SOURCE_ID = BPI.CELL_SOURCE_ID
inner join RECENT_CELL_SOURCE_STATUS rcss
    on CS.CELL_SOURCE_ID = RCSS.CELL_SOURCE_ID
where tumor_type is not null
and CS.RECEIVED_DATE > to_date('03/01/2011', 'MM/DD/YYYY/')
and RCSS.STATUS like 'Term%'
group by BPI.TYPE, extract(MONTH FROM CS.RECEIVED_DATE)
)
order by month, type
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文