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 मोजा.
पायऱ्या
- पहिलं उदाहरण run करा आणि year, month आणि day columns वाचा.
- प्रत्येक महिन्यासाठी एक row मिळवायला दुसरं उदाहरण run करा.
- Month column YYYY-MM format मध्ये असल्यामुळे बरोबर sort होतो ते तपासा.
- हा 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_at | year | month | day |
|---|---|---|---|
| 2026-08-19 14:40:00 | 2026 | 08 | 19 |
| 2026-09-14 13:10:00 | 2026 | 09 | 14 |
| 2026-10-20 16:45:00 | 2026 | 10 | 20 |
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
| month | transactions |
|---|---|
| 2026-08 | 7 |
| 2026-09 | 9 |
| 2026-10 | 9 |
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.
Ravindra Bagale's Tip – मराठी
Monthly reports साठी फक्त '%m' ऐवजी '%Y-%m' वापरा. फक्त '%m' वापरलं तर दोन वेगवेगळ्या वर्षांचे September एकाच group मध्ये जातात.
Ravindra Bagale's Tip – हिंदी
Monthly reports के लिए सिर्फ़ '%m' की जगह '%Y-%m' इस्तेमाल कीजिए। सिर्फ़ '%m' से दो अलग सालों के September एक ही group में चले जाते हैं।