简体   繁体   中英

Oracle 11g count distinct products group by user

I have a table with following fields

ID PRODUCT STATUS USERID Date
1 100 1 10 01-01-2023
2 101 1 10 01-01-2023
2 102 2 10 01-01-2023
3 100 2 20 02-01-2023
4 102 1 30 02-01-2023
4 100 1 10 03-01-2023

Desired output

Distinct product scan by each userid between 01-01-2023 to 03-01-2023 and count by status for each user

USERID PRODUCT SCAN STATUS1 CNT STATUS2 CNT
10 3 2 1
20 1 0 1
30 1 1 0

Use COUNT(DISTINCT column_name) .

Query

select userid, count(distinct product) as productScan
from table_name
where date between '2023-01-01' and '2023-01-03'
group by userid;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM