LightMySky
به انگلیسی

Window Functions and Running Calculations

A window function calculates for each row using rows from its partition, without collapsing those rows. Partitioning and ordering support ranks, running totals and previous-row comparisons. Aggregate frames define which rows contribute to each sum.

شروع یادگیری این موضوعبه حساب کاربری نیازی نیست. پیشرفت در همین مرورگر ذخیره می‌شود.

کارهایی که یادگیرنده بعد از این درس می‌تواند انجام دهد

  • Compute a repeatable running total per customer using a stated order and an explicit row frame
  • Rank rows within a partition and handle ties deliberately
  • Explain how a window keeps individual orders while grouping only by customer produces customer summaries

۱ · بخوان

You have used queries to choose which rows appear. A window function adds a calculation to each row that reaches the window stage; it does not collapse those rows. GROUP BY customer instead produces one summary row per customer. OVER defines the window. PARTITION BY c separates customers. SUM(a) OVER(PARTITION BY c), without window ordering, gives each customer's full total beside each of their rows.

Try it together

Sales(c,id,a) stores customer, unique non-null order id and amount. Here id follows order time. SELECT c,id,a,SUM(a) OVER(PARTITION BY c ORDER BY id ROWS UNBOUNDED PRECEDING) FROM Sales adds a running total. A's rows (1,4) and (2,7) get totals 4 and 11. B's row (3,6) gets 6. Each customer has a separate sum.

A id1: 4 -> total 4A id2: 7 -> total 11B id3: 6 -> total 6
Each cell shows a customer, order id, amount and running total. B's total excludes A's amounts.

These examples use PostgreSQL. ROWS UNBOUNDED PRECEDING means a frame from the partition's first row through the current row, in window order. With ORDER BY but no frame, the default aggregate frame also includes all rows tied with the current row. A unique window ordering key makes row-by-row totals repeatable. ORDER BY outside OVER sorts the output; it does not set the window order.

RANK() OVER(PARTITION BY c ORDER BY score DESC) ranks each customer's scores, highest first. Scores 90,90,75 get 1,1,3: ties share rank and leave gaps. DENSE_RANK() gives 1,1,2. ROW_NUMBER() gives distinct numbers 1,2,3. Add a unique id to its ORDER BY for repeatable numbering within tied scores. Do not add id to RANK or DENSE_RANK when equal scores must share a rank.

90: rank 1, dense 190: rank 1, dense 175: rank 3, dense 2
Within one customer partition, rank means RANK and dense means DENSE_RANK. Equal scores share a rank; only RANK skips a number.

LAG(a) OVER(PARTITION BY c ORDER BY id) returns the previous row's amount within each customer partition. For A's amounts 4 then 7, the previous amounts are NULL then 4. B's only amount, 6, has NULL. With no default supplied, LAG returns NULL when there is no previous row. It looks one row back in window order, not for an id exactly one less.

Windows keep detail rows. Partitions separate calculations, row frames define running sums, and ranking ties need a stated rule.

The lesson, questions and flashcards were written for this dataset. The SQL examples use PostgreSQL conventions.

۲ · ببین

به انگلیسی

دور از صفحه‌نمایش

چاپ کاربرگکاغذ A4، با یک صفحهٔ جواب‌ها برای بزرگ‌ترها. بدون صفحه‌نمایش، بدون اینترنت.

جایگاه این موضوع

اول این‌ها را یاد بگیر

پیوندهای اختیاری

این موضوع راه این‌ها را باز می‌کند

سپس تمرین

۸ سؤال پشت این درس منتظرند، هر کدام با پاسخ توضیح‌داده‌شده. هر پاسخ آسمان را پر می‌کند: ستاره‌ها هنگام یادگیری روشن می‌شوند و وقتی زمان برگشتن است کمرنگ می‌شوند.

شروع یادگیری این موضوع