본문 바로가기
개발/SQL

[SQL] SQL 숫자 연산

by WaDDak 2024. 8. 9.

 

SQL에서 숫자 연산을 통해 새로운 테이블을 생성해낼 수 있다.

select food_preparation_time,
       delivery_time,
       food_preparation_time + delivery_time as total_time
from food_orders

food_preparation_time + delivery_time 를 계산해서 total_time 에 그 값을 넣어 테이블을 만들어 준다.

+ 이외의 다른 연산자들도 사용 가능하다.

 

 

SQL 에서는 계산의 편의를 위해 함수를 제공하고 있습니다.

사용방법은 엑셀과 유사하고, 유일하게 다른 점은 데이터의 범위가 아닌 계산할 '컬럼'을 적어준다는 것입니다.

select sum(food_preparation_time) total_food_preparation_time, //food_preparation_time의 
                              // 모든 합을 total_food_preparation_time를 별명으로해서 보여줘.
       avg(delivery_time) avg_delivery_time
from food_orders

 

  • SUM        : 합계
  • AVG         : 평균
  • COUNT   : 컬럼의 갯수 세어주기  
select count(1) count_of_orders,  // count() 인자 1이나 *이면 모든 데이터라는 의미
       count(distinct customer_id) count_of_customers
       // distinct는 컬럼에 몇개의 값을 가지고 있는지 구해주는 명령어. 
       // customer_id의 종류가 몇개인지.
from food_orders

 

  • MIN   : 최소값
  • MAX  : 최대값
select min(price) min_price,
       max(price) max_price
from food_orders

 

'개발 > SQL' 카테고리의 다른 글

[SQL] BCNF 데이터베이스 정규화  (0) 2024.10.07
[SQL] 에러 메세지  (0) 2024.08.09
[SQL] Between, In, Like  (0) 2024.08.09
[SQL] null 판독  (0) 2024.08.08
[SQL] select from where (as 별명짓기)  (0) 2024.08.08