반응형
DATEDIFF, DATEPART
SELECT DATEDIFF(DAY, '2021-06-12', '2021-07-13') AS [일차이]
, DATEDIFF(MONTH, '2021-06-12', '2021-07-13') AS [개월차이]
, DATEDIFF(YEAR, '2021-06-12', '2021-07-13') AS [년차이]
LAG, LEAD
https://learn.microsoft.com/en-us/sql/t-sql/functions/lag-transact-sql?view=sql-server-ver16
SELECT empno
, ename
, job
, sal
, LAG(sal) OVER(PARTITION BY job ORDER BY job, sal) AS sal_prev
, LEAD(sal) OVER(PARTITION BY job ORDER BY job, sal) AS sal_next
FROM emp
WHERE job IN ('MANAGER', 'ANALYST', 'SALESMAN')
ORDER BY job, sal
CREATE TABLE T (a INT, b INT, c INT);
GO
INSERT INTO T VALUES (1, 1, -3), (2, 2, 4), (3, 1, NULL), (4, 3, 1), (5, 2, NULL), (6, 1, 5);
SELECT b, c,
LAG(2*c, b*(SELECT MIN(b) FROM T), -c/2.0) OVER (ORDER BY a) AS i
FROM T;
https://learn.microsoft.com/en-us/sql/t-sql/functions/lead-transact-sql?view=sql-server-ver16
CREATE TABLE T (a INT, b INT, c INT);
GO
INSERT INTO T VALUES (1, 1, -3), (2, 2, 4), (3, 1, NULL), (4, 3, 1), (5, 2, NULL), (6, 1, 5);
SELECT b, c,
LEAD(2*c, b*(SELECT MIN(b) FROM T), -c/2.0) OVER (ORDER BY a) AS i
FROM T;
반응형
'프로그래밍 > DataBase' 카테고리의 다른 글
[MSSQL] IF…ELSE IF…ELSE (0) | 2023.07.14 |
---|---|
[MSSQL] 월의 마지막 날짜 구하기 (0) | 2023.07.03 |
[MSSQL] identity 를 설정한 칼럼에 insert 하기 (0) | 2023.06.19 |
MSSQL 탭( char(9) )과 엔터값( char(10) + char(13) ) 찾아보기 (0) | 2023.06.09 |
[MSSQL] 정렬조건없이 순번 매기기 , row_number() rank, dense_rank (0) | 2023.06.05 |