본문 바로가기
postgresql

Sql-server의 윈도우함수들이 postgresql에서 사용가능한지 확인하고 사용가능한 것과 대체해서 사용할 수 있는 것을 리스트로 정리하고 내용도 정리

by 루에 2025. 1. 19.
반응형

SQL Server의 윈도우 함수들은 대부분 PostgreSQL에서도 사용 가능합니다. 두 데이터베이스 시스템 모두 ANSI SQL 표준을 따르기 때문에 많은 윈도우 함수들이 유사하게 구현되어 있습니다. 다음은 SQL Server의 윈도우 함수들과 PostgreSQL에서의 사용 가능 여부 및 대체 방법을 정리한 리스트입니다:

## 사용 가능한 윈도우 함수

1. ROW_NUMBER()
2. RANK()
3. DENSE_RANK()
4. NTILE()
5. LAG()
6. LEAD()
7. FIRST_VALUE()
8. LAST_VALUE()
9. SUM()
10. AVG()
11. COUNT()
12. MIN()
13. MAX()

이 함수들은 SQL Server와 PostgreSQL에서 동일한 이름과 유사한 구문으로 사용할 수 있습니다.

## 대체 가능한 윈도우 함수

1. PERCENT_RANK(): PostgreSQL에서도 동일한 이름으로 사용 가능
2. CUME_DIST(): PostgreSQL에서도 동일한 이름으로 사용 가능

## 내용 정리

1. 구문 차이:
   - SQL Server와 PostgreSQL 모두 `OVER` 절을 사용하여 윈도우를 정의합니다.
   - `PARTITION BY`와 `ORDER BY` 절도 두 시스템에서 동일하게 사용됩니다.

2. 프레임 지정:
   - SQL Server와 PostgreSQL 모두 `ROWS` 또는 `RANGE`를 사용하여 윈도우 프레임을 지정할 수 있습니다[10].

3. 성능 최적화:
   - PostgreSQL은 윈도우 함수 실행을 최적화하기 위해 내부적으로 다양한 기술을 사용합니다[3].

4. 추가 기능:
   - PostgreSQL은 `WINDOW` 절을 제공하여 여러 윈도우 함수에서 동일한 윈도우 정의를 재사용할 수 있게 합니다[10][41].

   예시:
   ```sql
   SELECT sum(salary) OVER w, avg(salary) OVER w
   FROM empsalary
   WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);
   ```

5. 확장성:
   - PostgreSQL은 사용자 정의 윈도우 함수를 만들 수 있는 기능을 제공합니다[3].

6. JSON 지원:
   - PostgreSQL은 JSON 데이터에 대한 윈도우 함수 사용을 지원합니다[27].

7. 집계 함수:
   - 대부분의 집계 함수(SUM, AVG, COUNT 등)는 두 시스템 모두에서 윈도우 함수로 사용할 수 있습니다[3][6].

결론적으로, SQL Server의 윈도우 함수들은 대부분 PostgreSQL에서도 사용 가능하며, 구문과 기능이 매우 유사합니다. 두 시스템 간의 주요 차이점은 성능 최적화 방식과 일부 추가 기능(예: PostgreSQL의 WINDOW 절)에 있습니다. 따라서 SQL Server에서 PostgreSQL로 마이그레이션할 때 윈도우 함수 관련 코드는 대부분 그대로 사용하거나 약간의 수정만으로 사용할 수 있습니다.

출처
[1] SQL Window Functions Series: RANK() and DENSE_RANK() https://www.sqlservercentral.com/articles/sql-window-functions-series-rank-and-dense_rank
[2] How to Use Window Functions? - SQL Server - DbSchema https://dbschema.com/2023/07/10/sqlserver/window-functions/
[3] PostgreSQL Window Functions: The Ultimate Guide - Neon https://neon.tech/postgresql/postgresql-window-function
[4] Window Functions - PostgreSQL - Codecademy https://www.codecademy.com/resources/docs/postgresql/window-functions
[5] Window functions for ANSI SQL - SQL Server to Aurora PostgreSQL ... https://docs.aws.amazon.com/dms/latest/sql-server-to-aurora-postgresql-migration-playbook/chap-sql-server-aurora-pg.sql.windowfunctions.html
[6] Data Processing With PostgreSQL Window Functions - Timescale https://www.timescale.com/learn/postgresql-window-functions
[7] Postgre Window Functions - GeeksforGeeks https://www.geeksforgeeks.org/postgre-window-functions/
[8] SQL Window Functions in SQL Server, Oracle and PostgreSQL https://www.mssqltips.com/sqlservertip/6796/sql-window-functions-sql-server-oracle-postgresql/
[9] Documentation: 17: 3.5. Window Functions - PostgreSQL https://www.postgresql.org/docs/current/tutorial-window.html
[10] Documentation: 17: 3.5. Window Functions - PostgreSQL https://www.postgresql.org/docs/current/tutorial-window.html
[11] Guide to Using SQL Window Functions (with examples) - Coginiti https://www.coginiti.co/tutorials/intermediate/sql-window-functions/
[12] The Ultimate Guide to SQL Window Functions - StrataScratch https://www.stratascratch.com/blog/the-ultimate-guide-to-sql-window-functions/
[13] SELECT - WINDOW clause (Transact-SQL) - Microsoft Learn https://learn.microsoft.com/en-us/sql/t-sql/queries/select-window-transact-sql?view=sql-server-ver16
[14] SQL Server Window Functions - javatpoint https://www.javatpoint.com/sql-server-window-functions
[15] SQL Window Functions Cheat Sheet - DataCamp https://www.datacamp.com/cheat-sheet/sql-window-functions-cheat-sheet
[16] OVER Clause (Transact-SQL) - SQL Server | Microsoft Learn https://learn.microsoft.com/en-us/sql/t-sql/queries/select-over-clause-transact-sql?view=sql-server-ver16&redirectedfrom=MSDN
[17] SQL Window Function Example With Explanations - LearnSQL.com https://learnsql.com/blog/sql-window-functions-examples/
[18] How to use Window functions in SQL Server - SQLShack https://www.sqlshack.com/use-window-functions-sql-server/
[19] [PDF] SQL Window Functions Cheat Sheet - LearnSQL.com https://learnsql.com/blog/sql-window-functions-cheat-sheet/Window_Functions_Cheat_Sheet.pdf
[20] SQL Window Functions | Advanced SQL - Mode Analytics https://mode.com/sql-tutorial/sql-window-functions/
[21] Data Processing With PostgreSQL Window Functions - Timescale https://www.timescale.com/learn/postgresql-window-functions
[22] Documentation: 17: 9.22. Window Functions - PostgreSQL https://www.postgresql.org/docs/current/functions-window.html
[23] [PostgreSQL, Greenplum] Window Functions, 윈도우 함수 https://rfriend.tistory.com/737
[24] Window Functions - postgresql.kr https://postgresql.kr/docs/9.6/functions-window.html
[25] [postgresql] Window Function(윈도우함수)에 대해 알아보자! https://chiefcoder.tistory.com/35
[26] Equivalent of Postgres' Window Alias in SQL Server - Stack Overflow https://stackoverflow.com/questions/67205303/equivalent-of-postgres-window-alias-in-sql-server
[27] PostgreSQL vs SQL Server (MSSQL): Feature-by-Feature Comparison https://read.careercredentials.in/blog/PostgreSQL-vs-SQL-Server-MSSQL-Feature-by-Feature-Comparison/
[28] A Complete Comparison of PostgreSQL vs Microsoft SQL Server | EDB https://www.enterprisedb.com/blog/microsoft-sql-server-mssql-vs-postgresql-comparison-details-what-differences
[29] PostgreSQL vs. SQL Server: What's the difference? - Google Cloud https://cloud.google.com/learn/postgresql-vs-sql
[30] PostgreSQL vs SQL Server: 16 Critical Differences - Kinsta https://kinsta.com/blog/postgresql-vs-sql-server/
[31] How can I generate a row_number without using a window function? https://dba.stackexchange.com/questions/159634/how-can-i-generate-a-row-number-without-using-a-window-function/159658
[32] LAG and/or LEAD alternative without window function in Pstgresql https://stackoverflow.com/questions/74916041/lag-and-or-lead-alternative-without-window-function-in-pstgresql
[33] Practicing SQL without window functions : r/dataengineering - Reddit https://www.reddit.com/r/dataengineering/comments/p9mhom/practicing_sql_without_window_functions/
[34] Window Function Examples for SQL Server - Brent Ozar Unlimited® https://www.brentozar.com/sql-syntax-examples/window-function-examples-sql-server/
[35] Window Functions in SQL - GeeksforGeeks https://www.geeksforgeeks.org/window-functions-in-sql/
[36] 14.20.2 Window Function Concepts and Syntax https://dev.mysql.com/doc/refman/8.4/en/window-functions-usage.html
[37] A Beginners Guide to SQL Window Functions - DbVisualizer https://www.dbvis.com/thetable/a-beginners-guide-to-sql-window-functions/
[38] SQL Server Window Functions https://www.sqlservertutorial.net/sql-server-window-functions/
[39] Window functions in PostgreSQL: The secret weapon of SQL ninjas https://www.linkedin.com/pulse/window-functions-postgresql-secret-weapon-sql-ninjas-doug-ortiz--x7cxe
[40] Why & How to Use Window Functions to Aggregate Data in Postgres https://coderpad.io/blog/development/window-functions-aggregate-data-postgres/
[41] 82강 SQL 함수 정리 : PostgreSQL Window Functions https://doitsql.tistory.com/entry/82%EA%B0%95-SQL-%ED%95%A8%EC%88%98-%EC%A0%95%EB%A6%AC-PostgreSQL-Window-Functions
[42] Two Important Differences Between SQL Server and PostgreSQL https://www.brentozar.com/archive/2018/08/two-important-differences-between-sql-server-and-postgresql/
[43] PostgreSQL vs. SQL Server: Which Is Better For You? - Airbyte https://airbyte.com/data-engineering-resources/postgresql-vs-sql-server

반응형