Example: Using a HAVING Clause to Aggregate a Join - Analytics Database - Teradata Vantage

SQL Data Manipulation Language

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Teradata Vantage
Release Number
17.20
Published
June 2022
Language
English (United States)
Last Update
2024-04-05
dita:mapPath
pon1628111750298.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
esx1472246586715
lifecycle
latest
Product Category
Teradata Vantage™

The columns named price and sales_qty are from two different tables, sales_hist as table_1 and unit_price_cost as table_2. Use the following SELECT statement to find which category of items is sold for a profit margin greater than $1000.

     SELECT table_1.category,
     (table_2.price - table_2.cost) * SUM (table_1.sales_qty) AS margin
     FROM sales_hist AS table_1, unit_price_cost AS table_2
     WHERE table_1.prod_no=table_2.prodno
     GROUP BY table_1.category, table_2.price, table_2.cost
     HAVING margin > 1000;

A subquery can have a join on a view with an aggregate operation and a HAVING clause that references more than one table.