13. HAVING: Aggregate नंतर groups filter करा
13.4 Practice: Target पार करणाऱ्या branches आणि busy accounts
का (Why). ही practice bank manager दर महिन्याला विचारतो त्या दोन प्रश्नांची उत्तरं देते: कुठल्या branch च्या कुठल्या महिन्यांनी target पार केलं, आणि कुठली accounts खूप busy आहेत.
काय (What). प्रत्येक branch चं स्वतःचं monthly_target branches table मध्ये आहे. आधी target वाचा, आणि मग त्या branch साठी तो HAVING मध्ये वापरा. Busy accounts म्हणजे 4 किंवा जास्त transactions असलेली accounts.
कसं (How). Targets बघा, मग Mumbai ला त्याच्या 9,00,000 rupees target शी तपासा, आणि मग busy accounts list करा.
पायऱ्या
- प्रत्येक branch चं target आणि currency वाचायला पहिलं उदाहरण run करा.
- Mumbai चे महिने 9,00,000 rupees शी तपासायला दुसरं उदाहरण run करा.
- 4 किंवा जास्त transactions असलेली accounts आणि त्यांची total amount list करायला तिसरं उदाहरण run करा.
- Manager ला कुठले महिने आणि accounts दाखवाल ते लिहून ठेवा.
उदाहरणे
उदाहरण 1 · dataset: northstar_bank
SELECT branch_id, branch_name, currency, monthly_target
FROM branches
ORDER BY branch_id;Output
| branch_id | branch_name | currency | monthly_target |
|---|---|---|---|
| 1 | Northstar London Bridge | GBP | 30000 |
| 2 | Northstar Manhattan | USD | 40000 |
| 3 | Northstar Fort | INR | 900000 |
| 4 | Northstar FC Road | INR | 600000 |
Meaning. प्रत्येक branch चं target त्याच्या स्वतःच्या currency मध्ये आहे. Mumbai चं target महिन्याला 9,00,000 rupees आहे.
उदाहरण 2 · dataset: northstar_bank
SELECT strftime('%Y-%m', posted_at) AS month,
SUM(amount) AS total_inr
FROM transactions
WHERE branch_id = 3
GROUP BY month
HAVING SUM(amount) > 900000
ORDER BY month;Output
| month | total_inr |
|---|
Meaning. एकही row येत नाही, म्हणजे Mumbai ने कुठल्याच महिन्यात 9,00,000 rupees चं target पार केलं नाही. रिकामा result हे पण एक उत्तरच आहे.
उदाहरण 3 · dataset: northstar_bank
SELECT account_id,
COUNT(*) AS transactions,
SUM(amount) AS total_amount
FROM transactions
GROUP BY account_id
HAVING COUNT(*) >= 4
ORDER BY transactions DESC, account_id;Output
| account_id | transactions | total_amount |
|---|---|---|
| 103 | 4 | 51700.0 |
Meaning. फक्त account 103, म्हणजे New York ची Grace Miller, हिच्याकडे 4 किंवा जास्त transactions आहेत. तिचा 51,700 चा total तिच्या branch च्या currency मध्ये, म्हणजे US dollars मध्ये आहे.
Ravindra Bagale's Tip
When a HAVING query returns no rows, check the target and the currency before you assume the query is wrong.
Ravindra Bagale's Tip – मराठी
HAVING query एकही row देत नसेल, तर query चुकली असं समजण्याआधी target आणि currency तपासा.
Ravindra Bagale's Tip – हिंदी
HAVING query कोई row न दे, तो query गलत है यह मानने से पहले target और currency जाँचिए।