如何计算按列分组的列的行数但忽略 Oracle 中 table 的其他列?

How to count the rows of a column grouping by a column but omitting the others columns of the table in Oracle?

我想按第一列进行计数分组,但忽略分组依据中的其他列。让我解释一下:

我有一个 table 包含这些列

所以,我想要得到的是一个新列,其中包含按仪器分类的工单总数,如下所示:

我该怎么做?因为如果我这样计算:

SELECT 仪器、工单、日期、计数(*) 从表 1 按仪器、工单、日期分组;

我明白了:

只需使用一个window函数:

select t.*,
       count(*) over (partition by instrument) as instrument_count
from table1 t;

虽然 Gordon 给出的答案很完美,但还有另一种选择,即使用 group by 和子查询。您也可以向此查询添加日期列

SELECT * FROM 
(
SELECT A.INSTRUMENT, B.TOTAL_COUNT_BY_INSTRUMENT  
FROM work_order A, 
(SELECT COUNT(1) AS TOTAL_COUNT_BY_INSTRUMENT, 
INSTRUMENT
FROM WORK_ORDER 
GROUP BY INSTRUMENT
) B
WHERE A.INSTRUMENT = B.instrument);