JG Story

[ Postgres ] 데이터 조회 ( Select 문 활용_1 ) 본문

IT/DB

[ Postgres ] 데이터 조회 ( Select 문 활용_1 )

JG_lee 2022. 3. 29. 00:26

 

1. 시간 기준 가장 먼저 ~한 대상 조회

ex) select name from animal_ins where datetime = (select min(datetime) from animal_ins);

     -> animal_ins 테이블에서 가장 먼저 들어온 동물 이름 조회

 

2. 중복된 데이터 제거 후 갯수 조회

ex) select count(distinct(name)) as name_count from animal_ins;

     -> animal_ins 테이블에서 중복된 이름은 하나로 취급하고, null 은 집계하지 않도록 하여 총 이름 개수 조회

 

3. 중복된 데이터 대상 갯수 조회

ex) select name, count(name) from animal_ins

     where name is not null

     group by name

      having count(name)>1

      order by name

     -> 동물의 이름 중 두 번 이상 쓰인 이름과 해당 이름이 쓰인 횟수 조회 ( 이름이 없는 동물은 집계에서 제외 )

 

 

[뜬금 정리]

group by ~ + having

-> 그룹의 결과에 다시 조건을 줄 때 having을 사용

 

join ~ + on

-> 조인의 결과를 조건을 줄 때 on 사용

 

 

 

 

 

 

 

 

 

 

Comments