Window Functions परिचय
Window functions related rows के आर-पार values compute करते हैं बिना इन्हें एक row में collapse किए।
In this page:
Syntax
SELECT columns,
function_name() OVER (PARTITION BY column ORDER BY column)
FROM table_name;
Window Functions Intro
एक window function OVER (PARTITION BY ... ORDER BY ...) इस्तेमाल करता है। ROW_NUMBER, RANK, और DENSE_RANK rows number करते हैं, SUM और AVG running totals produce कर सकते हैं, और LAG और LEAD neighbouring rows peek करते हैं।
GROUP BY के उलट, हर input row output में रहती है। PostgreSQL पूरा window function feature set support करता है।
Note:
बिना arguments के OVER () function को पूरे result set पर apply करता है।
उदाहरण: Window functions intro
CREATE TABLE scores (id INTEGER PRIMARY KEY, team TEXT, name TEXT, points INTEGER);
INSERT INTO scores VALUES (1,'red','Ada',30),(2,'red','Cy',50),(3,'blue','Bob',50),(4,'blue','Di',10),(5,'red','Ed',20);
SELECT name, team, points, ROW_NUMBER() OVER (PARTITION BY team ORDER BY points DESC) AS rank_in_team FROM scores ORDER BY team, rank_in_team;
SELECT name, points, SUM(points) OVER (ORDER BY id) AS running_total FROM scores ORDER BY id;
-- Output:
-- name | team | points | rank_in_team
-- Bob | blue | 50 | 1
-- Di | blue | 10 | 2
-- Cy | red | 50 | 1
-- Ada | red | 30 | 2
-- Ed | red | 20 | 3
-- name | points | running_total
-- Ada | 30 | 30
-- Cy | 50 | 80
-- Bob | 50 | 130
-- Di | 10 | 140
-- Ed | 20 | 160
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- यह उम्मीद करना कि window functions rows कम करेंगे
- running totals के लिए OVER के अंदर ORDER BY भूल जाना
- इन्हें WHERE में इस्तेमाल करना
चैप्टर सारांश
- OVER window define करता है
- PARTITION BY इसे groups में split करता है
- Rows collapse नहीं होतीं
- ROW_NUMBER, RANK, LAG, running SUM jaise functions मिलते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: