计算一列中不同值的数量

时间:2016-12-08 10:05:51

标签: sql oracle count

with table1 as
(
  select CTN_MAIN, SUBS_KEY, BAN_KEY, count(CTN_MAIN) as c
  from 
  (
    select distinct CTN_MAIN, SUBS_KEY, BAN_KEY
    from STG_SDB_LOAD SDB
  )
)select * from table1;

我正在尝试获得这3列CTN_MAIN,SUBS_KEY,BAN_KEY以及出现不同CTN_MAIN的次数。如果我这样做

select count(CTN_MAIN)

我得到了计数主要字段,但我也需要其他列。我该怎么办?

错误是

ORA-00937. 00000 -  "not a single-group group function"
*Cause:    
*Action:
Error at Line: 3 Column: 10

2 个答案:

答案 0 :(得分:1)

试试这个

with table1 as
(
  select CTN_MAIN, SUBS_KEY, BAN_KEY, count(CTN_MAIN) as c
  from 
  (
    select distinct CTN_MAIN, SUBS_KEY, BAN_KEY
    from STG_SDB_LOAD SDB
  ) group by CTN_MAIN, SUBS_KEY, BAN_KEY
)select * from table1;

答案 1 :(得分:0)

使用分析功能

with table1 as
(
  select CTN_MAIN, SUBS_KEY, BAN_KEY, COUNT(DISTINCT CTN_MAIN) OVER (PARTITION BY NULL) as c
  from 
  (
    select distinct CTN_MAIN, SUBS_KEY, BAN_KEY
    from STG_SDB_LOAD SDB
  )
)select * from table1;