Learning Netezza: Create View Syntax and Examples

In this article, we will delve into the process of creating views in Netezza, a high-performance data warehouse database system. Views serve as virtual tables based on the result-set of an SQL statement. They can simplify complex queries, enhance security, and promote better organization of your database schema.

Syntax

The fundamental syntax for creating a view in Netezza is as follows:

        CREATE VIEW view_name AS
            SELECT column1, column2, ...
            FROM table_name
            WHERE condition;
      

Example 1 - Simple View

Consider a table named `sales` with columns `region`, `product`, and `amount`. To create a view that displays the total sales for each region, we can employ the following SQL statement:

        CREATE VIEW total_sales AS
            SELECT region, SUM(amount) as total_sales
            FROM sales
            GROUP BY region;
      

Example 2 - View with Joins

Suppose we have another table named `customers` with columns `region`, `customer_name`, and let's see the top 5 customers in each region. We can create a view that joins both tables:

        CREATE VIEW top_5_customers AS
            SELECT region, customer_name, amount
            FROM sales
            JOIN customers ON sales.region = customers.region
            WHERE rownum <= 5
            GROUP BY region, customer_name;
      

Note

Netezza does not support the `AS` keyword when renaming columns in a view. Instead, you should use a subquery to achieve column renames:

        CREATE VIEW sales_view AS
            SELECT region, product, SUM(amount) as total_sales_per_product
            FROM sales
            GROUP BY region, product;

        -- To query the view and rename columns:
        SELECT region, product as item, total_sales_per_product as sales
        FROM sales_view;
      

Checking for Obsolete Views in Netezza

If you alter or change the underlying view table, the view becomes obsolete and unusable until you re-compile it. IBM has provided a script to check if a view is obsolete. The general location of this script is /nz/support/bin, but you should consult your Netezza admin team for the exact location. Below is the script and its usage:

        nz_check_views [database] [-replace ]