Professional Documents
Culture Documents
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
IBM Systems Lab Services and Training
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
SQL 1 SQL 2 SQL 3
Query QTEMP Query QTEMP Query
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Query Query Query
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Problem to Solve
The list of customers who were in the “top 10” for two
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Using QTEMP – Logical Step-by-Step and Work
Tables
DECLARE GLOBAL TEMPORARY TABLE t1 AS
(SELECT customer_name, SUM(sales) FROM SALES
WHERE YEAR=2018
GROUP BY customer_name
ORDER BY SUM(sales) DESC
FETCH FIRST 10 ROWS ONLY);
SELECT T1.customer_name,
T1.total_sales AS sales2018, T2.total_sales AS sales2019
FROM T1 INNER JOIN T2
ON T1.customer_name = T2.customer_name;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
CTEs – Thinking in Sets …
• What if you want the list of customers who were in the “top 10” for two
consecutive years? Think in sets …
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
SQE Plan Cache
SQL Program A
Access
Plan 1 Statement 1
Statement 2
Access
Statement 3
Plan 2
Access
Plan 3
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
SQE Plan Cache
SQL Program A
Access
Plan 1 Statement 1
Statement 2
Access
Statement 3
Plan 2
Access
Plan 3
SQL Program B
Access
Plan 4 Statement 4
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation