Window functions

SQL में बिज़नेस डेटा का विश्लेषण

Michel Semaan

Data Scientist

Window functions - अवलोकन

  • Window functions: वर्तमान पंक्ति से संबंधित पंक्तियों के सेट पर ऑपरेशन चलाती हैं
  • उदाहरण
    • रनिंग टोटल निकालें
    • पिछली या अगली पंक्ति का मान लें
SQL में बिज़नेस डेटा का विश्लेषण

Running total

Running total: किसी वैरिएबल के अब तक के मानों का संचयी योग

उदाहरण

x    x_rt
---  ----
1    1
2    3
3    6
4    11
5    16
SQL में बिज़नेस डेटा का विश्लेषण

Registrations का रनिंग टोटल - क्वेरी

WITH reg_dates AS (
  SELECT
    user_id,
    MIN(order_date) AS reg_date
  FROM orders
  GROUP BY user_id),

registrations AS ( SELECT DATE_TRUNC('month', reg_date) :: DATE AS delivr_month, COUNT(DISTINCT user_id) AS regs FROM reg_dates GROUP BY delivr_month)
SELECT delivr_month, regs, SUM(regs) OVER (ORDER BY delivr_month ASC) AS regs_rt FROM registrations ORDER BY delivr_month ASC LIMIT 3;
SQL में बिज़नेस डेटा का विश्लेषण

Registrations का रनिंग टोटल - परिणाम

delivr_month  regs  regs_rt
------------  ----  -------
2018-06-01    123    123
2018-07-01    140    263
2018-08-01    157    420
SQL में बिज़नेस डेटा का विश्लेषण

Lagged MAU - क्वेरी

WITH maus AS (
  SELECT
    DATE_TRUNC('month', order_date) :: DATE AS delivr_month,
    COUNT(DISTINCT user_id) AS mau
  FROM orders
  GROUP BY delivr_month)

SELECT delivr_month, mau, COALESCE( LAG(mau) OVER (ORDER BY delivr_month ASC), 1) AS last_mau FROM maus ORDER BY delivr_month ASC LIMIT 3;
SQL में बिज़नेस डेटा का विश्लेषण

Lagged MAU - परिणाम

delivr_month  mau  last_mau
------------  ---  --------
2018-06-01    123  1
2018-07-01    226  123
2018-08-01    337  226
SQL में बिज़नेस डेटा का विश्लेषण

Window functions

SQL में बिज़नेस डेटा का विश्लेषण

Preparing Video For Download...