PostgreSQL HAVING


ULASHISH

EGA

HAVING bandi SQL’ga qo‘shilgan, chunki WHERE bandini agregat funksiyalar bilan ishlatib bo‘lmaydi.

Agregat funksiyalar ko‘pincha GROUP BY bandi bilan qo‘llaniladi va HAVING qo‘shish orqali biz WHERE bandidagi kabi shart yozishimiz mumkin.

Misol

Faqat 5 martadan ortiq vakili bo‘lgan davlatlarni sanab o‘ting:

SELECT COUNT(customer_id), country
FROM customers
GROUP BY country
HAVING COUNT(customer_id) > 5;
Misolni ishga tushirish »

Ko‘proq misollar

Quyidagi SQL operatorida faqat umumiy narxi 400$ yoki undan yuqori bo‘lgan buyurtmalar ro‘yxati keltirilgan:

Misol

SELECT order_id, SUM(order_details.quantity * products.price)
FROM order_details
LEFT JOIN products on products.product_id = order_details.product_id
GROUP BY order_id
HAVING SUM(order_details.quantity * products.price) > 400.00;
Misolni ishga tushirish »

1000$ yoki undan ortiq tartib bergan mijozlar ro‘yxati:

Misol

SELECT customers.customer_name,
SUM(order_details.quantity * products.price)
FROM order_details
INNER JOIN products ON order_details.product_id = products.product_id
INNER JOIN orders ON order_details.order_id = orders.order_id
INNER JOIN customers ON orders.customer_id = customers.customer_id
GROUP BY customers.customer_id, customers.customer_name
HAVING SUM(order_details.quantity * products.price) >= 1000;
Misolni ishga tushirish »



W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!