مرکزی مواد پر جائیں
eLearner.app
ماڈیول 13 · سبق 4 از 4کورس میں 52/57~12 min
ماڈیول اسباق (4/4)

شفٹیں: وقفہ اور لیڈ

Shifts: LAG and LEAD

In sequential and time-based data, you often need to compare the current row with the one from the previous day. In "standard" SQL, doing so requires very dangerous (and very slow) self-joins.

The LAG() (previous row) and LEAD() (next row) functions solve exactly this problem with extreme agility and elegance.

Looking to the Past and the Future

SQL
SELECT
  created_at,
  total_amount,
  LAG(total_amount) OVER(ORDER BY created_at) AS importo_ordine_precedente
FROM orders;

In the example, each row will conveniently carry the amount produced by the same table, shifted forward by one row (backwards in time). We can easily subtract total_amount - LAG(...) to compute the difference or month-over-month percentage changes!

Likewise, LEAD() will pull the value from the row right below the current one.

ورزش#sql.m13.l4.e1
کوششیں: 0لوڈ ہو رہا ہے…

From the 'products' table, extract 'id', 'launched_on', 'price' and add 'prev_price', i.e. the 'price' of the product logically considered the 'previous' one, in ascending 'launched_on' order.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

Use LAG(price) OVER(ORDER BY launched_on ASC)

3 کوششوں کے بعد حل دستیاب ہے۔

ورزش#sql.m13.l4.e2
کوششیں: 0لوڈ ہو رہا ہے…

How would you compute the rating jumps per individual user? Simulate it: from the 'reviews' table extract 'customer_id', 'created_at', 'rating' and 'next_rating' (the next review of the same customer over time). So partition by 'customer_id', order by 'created_at' and use the right window function to peek ahead.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

LEAD() together with PARTITION BY and ORDER BY in the OVER.

3 کوششوں کے بعد حل دستیاب ہے۔