Aggregate Functions: avg(), max(), min(), sum(), count(), first(), last()

Aggregate functions ઘણી rows ને એક જવાબમાં નિચોવે છે, SUM સરવાળો કરે છે, AVG સરેરાશ કાઢે છે, COUNT ગણે છે, MAX/MIN છેડા શોધે છે, અને GROUP BY સાથે જોડાય ત્યારે એ આખા table માટે એકના બદલે per category એક જવાબ આપે છે.

11 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

હજાર rows માંથી એક number

Meera દરેક sale જોવા નથી માંગતી, એ કુલ સરવાળો માંગે છે. કે સરેરાશ price. કે એ કેટલા items રાખે છે. એક number જે આખા table ને summarise કરે.

એ એક aggregate function છે: એ ઘણી rows લે છે અને એક જવાબ પાછો આપે છે. અને તમે એમને Excel માં મળી ચૂક્યા છો, SUM, AVERAGE, COUNT, MAX, MIN, હવે SQL માં, અને એ એક મહાશક્તિ પામે છે: GROUP BY સાથે જોડાઈને, એ 'બધા માટે' ના બદલે 'per category' જવાબ આપે છે.

અહીં જ એક database એક calculator ને પછાડે છે: એક line દસ લાખ rows ને summarise કરે છે.

Theory

Data માટે એક juicer

એક juicer સંતરાનો ઢગલો લે છે અને એક ગ્લાસ રસ બનાવે છે, ઘણી વસ્તુઓ અંદર, એક વસ્તુ બહાર. Aggregate functions rows માટે એક juicer છે: 500 sale rows રેડો, એક કુલ સરવાળો, કે એક સરેરાશ, કે એક count બહાર મેળવો. અને GROUP BY અલગ જગ વાળો juicer છે: એક ગ્લાસ grocery-રસ, એક dairy-રસ, આખા ઢગલા માટે એકના બદલે per group એક summary.

Practical

Summarise the sales table

-- Whole-table aggregates (one answer each)
SELECT SUM(qty)   AS total_qty,   -- 20+15+30+25+40 = 130
       AVG(price) AS avg_price,   -- (45+120+60+35+30)/5 = 58
       COUNT(*)   AS num_items,   -- 5
       MAX(price) AS costliest,   -- 120
       MIN(price) AS cheapest     -- 30
FROM sales;

-- Per-category with GROUP BY
SELECT category, SUM(qty) AS qty_sold
FROM sales
GROUP BY category;
-- Grocery 50, Beverage 15, Personal 25, Dairy 40

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

GROUP BY અને HAVING

એકલો, એક aggregate આખા table ને summarise કરે છે. GROUP BY ઉમેરો અને એ દરેક group ને summarise કરે છે:

SELECT category, SUM(qty) FROM sales GROUP BY category કુલ જથ્થો per category આપે છે.

અને groups ને છાંટવા? WHERE ન કરી શકે, એ grouping પહેલા rows છાંટે છે. તમને HAVING જોઈએ, જે aggregation પછી છાંટે છે:

... GROUP BY category HAVING SUM(qty) > 30

ફક્ત એ categories રાખે છે જેમનો કુલ 30 થી વધુ હોય. WHERE rows છાંટે છે; HAVING groups છાંટે છે. એ જોડી એક ક્લાસિક exam મુદ્દો છે.

Quiz

એક column માં 5 rows છે: 3 માં values છે, 2 NULL છે. COUNT(*) અને COUNT(column) શું પાછું આપે છે?

  1. COUNT(*) = 5, COUNT(column) = 3 (એ NULLs છોડે છે)
  2. બંને 5 પાછું આપે છે
  3. બંને 3 પાછું આપે છે
  4. COUNT(*) = 3, COUNT(column) = 5
Show the answer

COUNT(*) = 5, COUNT(column) = 3 (એ NULLs છોડે છે)

*COUNT() બધી rows ગણે છે (5), NULLs સહિત. COUNT(column) એ column માં ફક્ત non-NULL values ગણે છે (3). આ બરાબર Unit 2 વાળો Excel COUNT વિરુદ્ધ COUNTA* ભેદ છે, SQL માં પુનર્જન્મ પામ્યો. COUNT()-વિરુદ્ધ-COUNT(col) ફાંદો આખા SQL unit ના સૌથી ભરોસાપાત્ર exam સવાલોમાંનો એક છે.

Think first

WHERE કે HAVING?

Meera 'એ categories જેમનો વેચાયેલો કુલ જથ્થો 30 થી વધુ હોય' માંગે છે. એ WHERE SUM(qty) > 30 અજમાવે છે અને એક error મેળવે છે. WHERE આ કેમ ન કરી શકે, અને એણે શું વાપરવું જોઈએ?

Show the answer

WHERE grouping પહેલા અલગ rows છાંટે છે, તો એ એક group નો SUM ન જોઈ શકે (જે એ તબક્કે હજી મોજૂદ જ નથી), એટલે error. એને HAVING જોઈએ, જે GROUP BY aggregation પછી છાંટે છે: GROUP BY category HAVING SUM(qty) > 30. નિયમ: rows માટે WHERE, groups/aggregates માટે HAVING. એમને ભેળવવા એ નંબર-એક GROUP BY ભૂલ છે.

Watch out

Marks ક્યાં કપાય છે

*COUNT() બધી rows ગણે છે; COUNT(col) NULLs છોડે છે (COUNTA સમાંતર). aggregates છાંટવા WHERE વાપરવું, એ HAVING નું કામ છે (WHERE grouping પહેલા ચાલે છે). એ ભૂલવું કે એક aggregate rows ને સમેટે છે એક value માં (તો તમે એક aggregate સાથે એક સાદો column SELECT ન કરી શકો જ્યાં સુધી એ GROUP BY માં ન હોય). અને નોંધો FIRST/LAST** નો મર્યાદિત/બિન-માનક support છે, એમના પર આધાર ન રાખો. AVG, SUM, COUNT, MAX, MIN ભરોસાપાત્ર પાંચ છે.

Theory

તમે આ પહેલા કર્યું છે, બે વાર

SUM, AVG, COUNT, MAX, MIN, આ Unit 2 ના Excel lesson ના એ જ functions છે, અને GROUP BY બરાબર Consolidate છે. તમે એમને એક spreadsheet માં click કર્યા; તમે એમને SQL માં type કરો છો; idea એકસમાન છે. એ સ્થાનાંતરણ જ આ subject ને spreadsheet-પહેલા બનાવવાનો આખો હેતુ છે. બે lessons બચ્યા: auto-numbering માટે sequences, અને views. આગળ: scalar functions જે દરેક value ને એક-એક કરીને બદલે છે.

Summary

Key takeaways

  • Aggregate functions ઘણી rows ને એક value માં સમેટે છે: SUM, AVG, COUNT, MAX, MIN.
  • COUNT(*) બધી rows ગણે છે; COUNT(column) ફક્ત non-NULL values ગણે છે (Excel COUNTA વિરુદ્ધ COUNT ની જેમ).
  • GROUP BY per group એક aggregate આપે છે (SUM(qty) per category).
  • HAVING aggregation પછી groups છાંટે છે; WHERE એ પહેલા rows છાંટે છે.
  • FIRST/LAST નો મર્યાદિત support છે; મુખ્ય પાંચ પર ભરોસો કરો.
  • યાદ રાખવાની યુક્તિ: એક juicer (ઘણી rows અંદર, એક જવાબ બહાર); GROUP BY અલગ જગ આપે છે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of SQL and Queries (Single Table only)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Aggregate Functions: avg(), max(), min(), sum(), count(), first(), last() · Data Processing and Analysis (DPA) · Gri-Learn