Ravindra BagaleCourses & study guides Track your progress

9. Analysts साठी Date आणि Time queries

9.2 SQLite मध्ये strftime() ने वर्ष, महिना आणि दिवस काढा

का (Why). Managers महिन्यानुसार किंवा वर्षानुसार totals मागतात. त्यानुसार group करायला महिना वेगळी value म्हणून हवा.

काय (What). strftime(format, date) date चा एक भाग text म्हणून देतं. '%Y' ने वर्ष, '%m' ने 01 ते 12 असा महिना, '%d' ने दिवस, आणि '%Y-%m' ने 2026-09 सारखे वर्ष आणि महिना एकत्र मिळतात.

कसं (How). काही dates चे भाग करा, आणि मग GROUP BY ने प्रत्येक महिन्याचे transactions मोजा.

पायऱ्या

  1. पहिलं उदाहरण run करा आणि year, month आणि day columns वाचा.
  2. प्रत्येक महिन्यासाठी एक row मिळवायला दुसरं उदाहरण run करा.
  3. Month column YYYY-MM format मध्ये असल्यामुळे बरोबर sort होतो ते तपासा.
  4. हा month column तुमच्या monthly report मध्ये वापरा.

उदाहरणे

उदाहरण 1 · dataset: northstar_bank

SELECT posted_at,
       strftime('%Y', posted_at) AS year,
       strftime('%m', posted_at) AS month,
       strftime('%d', posted_at) AS day
FROM transactions
WHERE txn_id IN (2, 9, 18);

Output

posted_atyearmonthday
2026-08-19 14:40:0020260819
2026-09-14 13:10:0020260914
2026-10-20 16:45:0020261020

Meaning. प्रत्येक date चे वर्ष, महिना आणि दिवस असे भाग होतात. हे भाग text आहेत, म्हणून महिना 8 नाही तर 08 असा दिसतो.

उदाहरण 2 · dataset: northstar_bank

SELECT strftime('%Y-%m', posted_at) AS month,
       COUNT(*)                     AS transactions
FROM transactions
GROUP BY month
ORDER BY month;

Output

monthtransactions
2026-087
2026-099
2026-109

Meaning. प्रत्येक महिन्यासाठी एक row आहे. August मध्ये 7, September मध्ये 9 आणि October मध्ये 9 transactions आहेत.

Ravindra Bagale's Tip

Use '%Y-%m' rather than '%m' alone for monthly reports. With '%m' alone, September of two different years would join into one group.