반응형

Database 81

SQL Server - Lead & Lag 함수

Lead & Lag functions - 2012년에 소개된 함수 - Lead 함수는 후속행에 올 데이터를 현재행과 함께 쓸 수 있다. - Lag함수는 이전행 데이터를 현재행 데이터와 함께 쓸 수 있다. - ORDER BY clause is required - PARTITION BY clause is optional Syntax: LEAD(Column_Name, Offset, Default_Value) OVER (ORDER BY Col1, Col2, ...) LAG(Column_Name, Offset, Default_Value) OVER (ORDER BY Col1, Col2, ...) --Offset : Number of rows to lead or lag --Default_Value : The defa..

Database/SQL Server 2020.12.18

SQL Server - NTILE 함수

NTILE function - SQL Server 2005에 소개 되어진 함수 - ORDER BY 절이 필수로 요구된다. ORDER BY clause is required - PARTITION BY 절은 선택적으로 요구된다. PARTITION BY clause is optional - 지정한 숫자만큼의 그룹으로 행을 나눈다. Distributes the rows into a specified number of groups. - 만약 지정한 숫자가 정확하게 나누어지지 않는 값이라면 그룹의 크기가 다를 수 있다. If the number of rows is not divisible by number of groups, you may have groups of two different sizes. - 이런 경..

Database/SQL Server 2020.12.18

SQL Server - 러닝 합계 Running Total

파티션 없이 러닝 합계 계산하는 쿼리문 SQL Query to compute running total without partitions. SELECT Name, Gender, Salary, SUM(Salary) OVER (ORDER BY ID) AS RunningTotal FROM Employees 파티션을 나눈 쿼리 SELECT Name, Gender, Salary, SUM(Salary) OVER (PARTITION BY Gender ORDER BY ID) AS RunningTotal FROM Employees RunningTotal이 파티션이 있는 부분에서 다시 시작하는 것을 볼 수 있음. 여기서 ORDER BY ID를 하는 것은 Unique key이기 때문이다. 유니크 하지 않은 것으로 ORDER..

Database/SQL Server 2020.12.18

SQL Server - RANK & DENSE_RANK 함수

RANK and DENSE_RANK functions - 2005년에 소개되었다.Introduced in SQL Server 2005 - ORDER BY절에서 지정한 행 순서에 따라 1부터 시작하는 순위를 리턴한다. Return a rank starting at 1 based on the ordering of rows imposed by the ORDER BY clause - ORDER BY clause is required - PARTITION BY clause is optional - 파티션으로 분리되어있으면 파티션이 바뀔때 1로 리셋이 된다.When the data is partitioned, rank is reset to 1 when the partition changes Difference be..

Database/SQL Server 2020.12.16

SQL Server - Over 절 과 ROW_NUMBER() 함수

Over clause 오버 절은 PARTITION BY와 함께 쓰이며 데이터를 파티션으로 나누는데 쓰인다. The OVER clause combined with PARTITION BY is used to break up data into partitions. The specified function operates for each partition. Syntax: function (...) OVER (PARTITION BY Col1, Col2, ...) COUNT(), AVG(), SUM(), MIN(), MAX(), ROW_NUMBER(), RANK(), DENSE_RANK() etc 어떤 함수와도 함께 쓸 수 있다. Row_Number Function - 2005년에 소개된 함수이다.Introduce..

Database/SQL Server 2020.12.16

SQL Server - 그룹핑, 그룹핑_아이디 GROUPING, GROUPING_ID

그룹핑은 무엇인가 ? What is Grouping function 그룹핑(열)은 컬럼이 GROUP BY 목록에 집계되었나 안되었나 의 여부를 나타내는 것이다. 집계된 행에는 1로 표시가 되고 아니면 0이라고 표시된다. Grouping(Column) indicates whether the column in a GROUP BY list is aggregated or not. Grouping returns 1 for aggregated or 0 for not aggregated in the result set. SELECT Continent, Country, City, SUM(SaleAmount) AS TotalSales, GROUPING(Continent) as GP_Continent, GROUPING(C..

Database/SQL Server 2020.12.16

SQL Server - 소계, 합계[롤업, 큐브] Rollup, Cube

ROLLUP이 쓰이는 예제를 보면서 이해하자. 아래의 예시 쿼리는 모두다 같은 결과를 나타낸다 --1 SELECT Country, SUM(Salary) as TotalSalary FROM Employees GROUP BY ROLLUP(Country) --2 SELECT Country, SUM(Salary) as TotalSalary FROM Employees GROUP BY Country with ROLLUP --3 SELECT Country, SUM(Salary) as TotalSalary FROM Employees GROUP BY Country UNION ALL SELECT NULL, SUM(Salary) as TotalSalary FROM Employees --4 바로 전에 배운 GROUPING S..

Database/SQL Server 2020.12.15
반응형