본문 바로가기
IT Develop/SQL

MSSQL WITH 구문에 대하여 알아봅시다

by K-popcorn 2023. 5. 3.
반응형

Microsoft SQL Server의 WITH 구문(Common Table Expressions(CTE))은 쿼리에서 여러 번 참조할 수 있는 임시 결과 집합을 만들 수 있는 강력한 기능입니다. CTE는 복잡한 쿼리를 읽기 쉽게 만들 수 있으며 조인 및 하위 쿼리 수를 줄임으로써 쿼리 성능을 향상시킬 수 있습니다.

다음은 MSSQL에서 WITH 구문을 사용하는 간단한 예입니다:

다음 구조의 직원  TABLE가 있다고 가정합니다:

CREATE TABLE employees (
  emp_id INT PRIMARY KEY,
  emp_name VARCHAR(50) NOT NULL,
  manager_id INT
);


manager_id 열에는 직원의 관리자 ID가 포함되어 있습니다. 각 직원의 이름과 해당 직원의 관리자 이름을 검색하려고 합니다. 이렇게 하려면 CTE를 사용하여 직원 이름과 관리자 ID가 포함된 임시 결과 집합을 만든 다음 해당 결과 집합을 직원 테이블과 함께 사용하여 관리자 이름을 검색할 수 있습니다.

WITH 구문을 사용한 쿼리는 다음과 같습니다:

WITH emp_mgr (emp_id, mgr_id) AS (
  SELECT emp_id, manager_id
  FROM employees
)
SELECT e.emp_name, m.emp_name AS manager_name
FROM employees e
JOIN emp_mgr em ON e.emp_id = em.emp_id
JOIN employees m ON em.mgr_id = m.emp_id;


이 쿼리에서 WITH 절은 직원 테이블에서 emp_id 및 manager_id 열을 선택하는 emp_mgr이라는 CTE를 정의합니다. 기본 쿼리는 emp_mgr CTE와 함께 직원 테이블에 두 번 결합합니다. 한 번은 직원의 관리자 ID를 검색하고 다시 한 번은 관리자 이름을 검색합니다. 결과 집합에는 emp_name 및 manager_name 두 개의 열이 포함됩니다.

이러한 방식으로 WITH 구문을 사용하면 특히 조인 및 하위 쿼리가 많은 복잡한 쿼리의 경우 쿼리를 더 쉽게 읽고 이해할 수 있습니다. 또한 동일한 테이블에 참여해야 하는 횟수를 줄임으로써 쿼리 성능을 향상시킬 수 있습니다.

 

(Eng.Ver)

The WITH syntax in Microsoft SQL Server, also known as Common Table Expressions (CTEs), is a powerful feature that allows you to create temporary result sets that can be referenced multiple times in a query. CTEs can make complex queries easier to read and can improve query performance by reducing the number of joins and subqueries.

Here's a simple example of how to use the WITH syntax in MSSQL:

Suppose you have a table of employees with the following structure:

CREATE TABLE employees (
  emp_id INT PRIMARY KEY,
  emp_name VARCHAR(50) NOT NULL,
  manager_id INT
);


The manager_id column contains the ID of the employee's manager. You want to retrieve the name of each employee and their manager's name. To do this, you can use a CTE to create a temporary result set that contains the employee name and their manager's ID, and then join that result set with the employees table to retrieve the manager name.

Here's the query using the WITH syntax:

WITH emp_mgr (emp_id, mgr_id) AS (
  SELECT emp_id, manager_id
  FROM employees
)
SELECT e.emp_name, m.emp_name AS manager_name
FROM employees e
JOIN emp_mgr em ON e.emp_id = em.emp_id
JOIN employees m ON em.mgr_id = m.emp_id;


In this query, the WITH clause defines a CTE named emp_mgr, which selects the emp_id and manager_id columns from the employees table. The main query joins the employees table with the emp_mgr CTE twice, once to retrieve the employee's manager ID and again to retrieve the manager's name. The result set contains two columns: emp_name and manager_name.

Using the WITH syntax in this way can make queries easier to read and understand, especially for complex queries with many joins and subqueries. It can also improve query performance by reducing the number of times you need to join the same table.

 

WITH common_table_expression

반응형

댓글