14. Tables join का होतात: Keys आणि Relationships
14.2 One-to-Many: Customers आणि Shipments (एक row म्हणजे काय)
का (Why). काहीही join किंवा total करण्याआधी एक row कशासाठी आहे ते माहीत हवं. Report मध्ये चुकीचा आकडा येण्याचं हे सगळ्यात नेहमीचं कारण आहे.
काय (What). Table चं grain म्हणजे एक row चा अर्थ. Customers मध्ये एक row म्हणजे एक customer. Shipments मध्ये एक row म्हणजे एक shipment. एका customer च्या अनेक shipments असू शकतात, म्हणून हा one-to-many relationship आहे. एखाद्या customer ची एकही shipment नसू शकते, जसं Marina Spices.
कसं (How). अजून काहीही join न करता, shipments table मध्येच प्रत्येक customer id च्या shipments मोजा.
पायऱ्या
- प्रत्येक table साठी एक वाक्य लिहा: customers मधला एक row म्हणजे एक customer.
- उदाहरण run करा आणि प्रत्येक customer च्या shipments चा आकडा वाचा.
- Customer 206 दिसत नाही हे बघा, कारण shipments मध्ये त्याचा एकही row नाही.
- हे लक्षात ठेवा: या table सोबत join केला की प्रत्येक customer त्याच्या प्रत्येक shipment साठी एकदा परत येईल.
उदाहरणे
उदाहरण 1 · dataset: swiftship_logistics
SELECT customer_id,
COUNT(*) AS shipments
FROM shipments
GROUP BY customer_id
ORDER BY customer_id;Output
| customer_id | shipments |
|---|---|
| 201 | 2 |
| 202 | 2 |
| 203 | 2 |
| 204 | 2 |
| 205 | 2 |
Meaning. 201 ते 205 पैकी प्रत्येक customer च्या 2 shipments आहेत. Customer 206 या result मध्ये नाही, कारण shipment नसलेल्या customer चा इथे एकही row नसतो.
Ravindra Bagale's Tip
Write the grain as a comment at the top of your query, for example: one row = one shipment. It makes wrong totals much easier to spot.
Ravindra Bagale's Tip – मराठी
Query च्या वर grain comment म्हणून लिहा, उदाहरणार्थ: one row = one shipment. मग चुकीचे totals खूप सहज दिसतात.
Ravindra Bagale's Tip – हिंदी
Query के ऊपर grain comment में लिखिए, जैसे: one row = one shipment. इससे गलत totals बहुत आसानी से पकड़ में आते हैं।