多次计数

nzkunb0c  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(276)

我有这张table:

user product log_date 

1    aaa     02-02-2020
2    aaa     05-01-2020
1    aaa     03-02-2020
2    aaa     06-01-2020
3    ccc     09-08-2020

我想询问一下

product total_entries  total_users
aaa     4              2
ccc     1              1

我做到了:

SELECT 
 product,
 count(*) total_entries,
 count(*) over(partition by product,user ) total_users
from table 
group by product;

我得到这个错误

ORA-00979: not a GROUP BY expression

有什么帮助吗?

ijnw1ujt

ijnw1ujt1#

with t(userid, product, log_date ) as (
select 1,    'aaa',     '02-02-2020' from dual union all
select 2,    'aaa',     '05-01-2020' from dual union all
select 1,    'aaa',     '03-02-2020' from dual union all
select 2,    'aaa',     '06-01-2020' from dual union all
select 3,    'ccc',     '09-08-2020' from dual
)
select product, count(*) total_entries, count(distinct userid) total_users
from t
group by product;

PRODUCT TOTAL_ENTRIES TOTAL_USERS
------- ------------- -----------
ccc                 1           1
aaa                 4           2
xtupzzrd

xtupzzrd2#

请尝试以下操作:

SELECT 
product,
count(*) OVER (partition by product) total_entries,
count(*) over(partition by product,user ) total_users
from table;

相关问题