Ravindra BagaleCourses & study guides Track your progress

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 करा.

पायऱ्या

  1. प्रत्येक branch चं target आणि currency वाचायला पहिलं उदाहरण run करा.
  2. Mumbai चे महिने 9,00,000 rupees शी तपासायला दुसरं उदाहरण run करा.
  3. 4 किंवा जास्त transactions असलेली accounts आणि त्यांची total amount list करायला तिसरं उदाहरण run करा.
  4. Manager ला कुठले महिने आणि accounts दाखवाल ते लिहून ठेवा.

उदाहरणे

उदाहरण 1 · dataset: northstar_bank

SELECT branch_id, branch_name, currency, monthly_target
FROM branches
ORDER BY branch_id;

Output

branch_idbranch_namecurrencymonthly_target
1Northstar London BridgeGBP30000
2Northstar ManhattanUSD40000
3Northstar FortINR900000
4Northstar FC RoadINR600000

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

monthtotal_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_idtransactionstotal_amount
103451700.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.