🛢️
PostgreSQL
Grokking Algorithm
  • Content
  • Environment
  • Getting Started
    • Introduction
      • What is PostgreSQL
      • Introduction to PostgreSQL sample database
    • Postgres on Windows
      • Install PostgreSQL on Windows
      • Connect to PostgreSQL database server
      • Load the sample database into the PostgreSQL database server
    • Postgres on Linux
    • Install PostgreSQL on macOS
  • Basic
    • Querying Data
      • SELECT
      • Column alias
      • ORDER BY
      • SELECT DISTINCT
    • Filtering Data
      • WHERE
      • AND
      • OR
      • LIMIT
      • FETCH
      • IN
      • BETWEEN
      • LIKE
      • IS NULL
    • Joining Multiple tables
      • Joins
      • Table aliases
      • INNER JOIN
      • LEFT JOIN
      • RIGHT JOIN
      • SELF JOIN
      • FULL OUTER JOIN
      • CROSS JOIN
      • NATURAL JOIN
    • Grouping data
      • GROUP BY
      • HAVING
    • Set Operations
      • UNION
      • INTERSECT
      • EXCEPT
    • Grouping sets, Cube, and Rollup
      • Grouping sets
      • CUBE
      • ROLLUP
    • Subquery
      • Subquery
      • ANY
      • ALL
      • EXISTS
    • Common Table Expressions
      • PostgreSQL CTE
      • Recursive query using CTEs
    • Modifying Data
      • Insert
      • Insert multiple rows
      • Update
      • Update join
      • Delete
      • Upsert
    • Transactions
      • PostgreSQL Transaction
    • Import & Export Data
      • Import CSV file into Table
      • Export PostgreSQL Table to CSV file
    • Managing Tables
      • Data types
      • Create a table
      • Select into
      • Create table as
      • Auto-increment
      • Sequences
      • Identity column
      • Alter table
      • Rename table
      • Add column
      • Drop column
      • Change column data type
      • Rename column
      • Drop table
      • Truncate table
      • Temporary table
      • Copy a table
    • Understanding PostgreSQL constraints
      • Primary key
      • Foreign key
      • UNIQUE constraint
      • CHECK constraint
      • NOT NULL constraint
    • PostgreSQL Data Types in Depth
      • Boolean
      • CHAR, VARCHAR and TEXT
      • NUMERIC
      • Integer
      • DATE
      • Timestamp
      • Interval
      • TIME
      • UUID
      • Array
      • hstore
      • JSON
      • User-defined data types
  • Advanced
  • PG-PGSQL
  • Functions
  • Adminstration
  • API
Powered by GitBook
On this page
  • HAVING vs. WHERE
  • PostgreSQL HAVING bandiga misollar
  • 1. SUM() funktsiyasi misolida PostgreSQL HAVING bandidan foydalanish
  • 2. COUNT() misol bilan PostgreSQL HAVING bandi

Was this helpful?

Edit on GitHub
  1. Basic
  2. Grouping data

HAVING

PreviousGROUP BYNextSet Operations

Last updated 1 year ago

Was this helpful?

Ushbu qo'llanmada siz guruh yoki agregat uchun qidiruv shartini belgilash uchun PostgreSQL HAVING bandidan qanday foydalanishni o'rganasiz.

HAVING bandi guruh yoki agregat uchun qidiruv shartini belgilaydi. HAVING bandi ko'pincha GROUP BY bandi bilan guruhlar yoki agregatlarni belgilangan shart asosida filtrlash uchun ishlatiladi.

Quyidagi bayonot HAVING bandini asosiy sintaksisini ko'rsatadi:

SELECT
	column1,
	aggregate_function (column2)
FROM
	table_name
GROUP BY
	column1
HAVING
	condition;

Ushbu sintaksisda band bo'yicha guruhlash column1 bo'yicha guruhlangan qatorlarni qaytaradi. HAVING bandi guruhlarni filtrlash shartini belgilaydi.

SELECT iborasining JOIN, LIMIT, FETCH kabi boshqa bandlarini qo'shish mumkin.

PostgreSQL HAVING bandini FROM, WHERE, GROUP BY dan keyin va SELECT, DISTINCT, ORDER BY va LIMIT bandlaridan oldin baholaydi.

HAVING bandi SELECT bandidan oldin baholanganligi sababli, HAVING bandida ustun taxalluslaridan foydalana olmaysiz. Chunki HAVING bandini baholash vaqtida SELECT bandida ko'rsatilgan ustun taxalluslari mavjud emas.

HAVING vs. WHERE

WHERE bandi belgilangan shart asosida qatorlarni filtrlash imkonini beradi. Biroq, HAVING bandi belgilangan shart bo'yicha qatorlar guruhlarini filtrlash imkonini beradi.

PostgreSQL HAVING bandiga misollar

1. SUM() funktsiyasi misolida PostgreSQL HAVING bandidan foydalanish

Quyidagi so'rov har bir mijozning umumiy miqdorini topish uchun SUM() funksiyasi bilan GROUP BY bandidan foydalanadi:

SELECT
	customer_id,
	SUM (amount)
FROM
	payment
GROUP BY
	customer_id;

Quyidagi bayonotda 200 dan ortiq pul sarflagan yagona mijozlarni tanlash uchun HAVING bandi qo'shilgan:

SELECT
	customer_id,
	SUM (amount)
FROM
	payment
GROUP BY
	customer_id
HAVING
	SUM (amount) > 200;

2. COUNT() misol bilan PostgreSQL HAVING bandi

Namuna ma'lumotlar bazasidan quyidagi customer jadvaliga qarang:

Quyidagi so'rovda GROUP BY bandidan do'konga to'g'ri keladigan xaridorlar sonini topish uchun foydalaniladi:

SELECT
	store_id,
	COUNT (customer_id)
FROM
	customer
GROUP BY
	store_id

Quyidagi bayonotda 300 dan ortiq xaridorga ega bo'lgan do'konni tanlash uchun HAVING bandi qo'shiladi:

SELECT
	store_id,
	COUNT (customer_id)
FROM
	customer
GROUP BY
	store_id
HAVING
	COUNT (customer_id) > 300;

GROUP BY bandi tomonidan qaytarilgan guruh yoki agregat uchun qidiruv shartini belgilash uchun HAVING bandidan foydalaning.

Keling, ma'lumotlar bazasidagi payment jadvalini ko'rib chiqaylik.

©

namunaviy
postgresqltutorial.com
output
table
output
output
table
output
output