Ravindra BagaleCourses & study guides Track your progress

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 मोजा.

पायऱ्या

  1. प्रत्येक table साठी एक वाक्य लिहा: customers मधला एक row म्हणजे एक customer.
  2. उदाहरण run करा आणि प्रत्येक customer च्या shipments चा आकडा वाचा.
  3. Customer 206 दिसत नाही हे बघा, कारण shipments मध्ये त्याचा एकही row नाही.
  4. हे लक्षात ठेवा: या 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_idshipments
2012
2022
2032
2042
2052

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.