每行中的总计数[重复]

问题描述 投票:0回答:1

我有一个要求,我有不同的 ID,但我想显示每行表中的总行数。

示例: 我的表中共有 5 行

ID  |   Date    |   Name
1       1 Jul       A
2       1 Jul       B
3       1 Jul       C 
4       1 Jul       D 
5       1 Jul       E  

我想在名称列之后显示每行中所有行的计数,如下所示

ID  |   Date    |   Name    |   Count
1       1 Jul       A            5
2       1 Jul       B            5
3       1 Jul       C            5 
4       1 Jul       D            5 
5       1 Jul       E            5    

我尝试使用 Count(*) 执行此操作,但它需要一个 group by 子句,该子句不会输出所需的结果。

任何帮助将不胜感激。

sql oracle plsql
1个回答
0
投票

按照评论中的建议尝试这个

select id, date_1, name ,  count(*) over () as cnt  FROM ( 
    select 1 as id ,      to_date('07/01/2024','mm/dd/yyyy') as date_1,  'A' as name from dual
    UNION ALL select 2 as id ,      to_date('07/01/2024','mm/dd/yyyy') as date_1,  'b' as name from dual
    UNION ALL select 3 as id ,      to_date('07/01/2024','mm/dd/yyyy') as date_1,  'c' as name from dual
    UNION ALL select 4 as id ,      to_date('07/01/2024','mm/dd/yyyy') as date_1,  'd' as name from dual
    UNION ALL select 5 as id ,      to_date('07/01/2024','mm/dd/yyyy') as date_1,  'e' as name from dual
    );
© www.soinside.com 2019 - 2024. All rights reserved.