Database/SQL practice

그룹 테이블 함수 조작

보늴 2022. 3. 7. 15:47

데이터셋

DROP TABLE IF EXISTS review;
CREATE TABLE review (
    user_id    varchar(255)
  , product_id varchar(255)
  , score      numeric
);

INSERT INTO review
VALUES
    ('U001', 'A001', 4.0)
  , ('U001', 'A002', 5.0)
  , ('U001', 'A003', 5.0)
  , ('U002', 'A001', 3.0)
  , ('U002', 'A002', 3.0)
  , ('U002', 'A003', 4.0)
  , ('U003', 'A001', 5.0)
  , ('U003', 'A002', 4.0)
  , ('U003', 'A003', 4.0)
;

 

 

 

 

SELECT count(*) AS total_count
, count(DISTINCT user_id) AS user_count
, count(DISTINCT product_id) AS product_count
,sum(score) AS sum 
,avg(score) AS avg 
, max(score)AS max 
, min(score) AS min 
FROM review;