Introduction
As we know, View is just a group of SQL statement which holds the data from one or more than one table. It is also called virtual table. So far, we know that View can contain rows and columns only, but View is different in Snowflake. In Snowflake, there are two types of Views.
- Non-Materialized view, also referred to as simply View
- Materialize View
We can also create a secure view by just adding secure keywork while creating Non-Materialized or Materialized View.
In this article, we are working with Non-Materialized View, and you will learn about creating Views to populate json data from one and more than one table. This real time scenario we are mostly using when we use View as API. I have also implemented the stored procedure in Snowflake to achieve the real-time scenario.
You can have a look at the
article.
To work on Views, I have three problem statements here,
- Create View which will return table data in the form of rows and columns.
- Create View which will return the data in Json format
- Create View which will return the data from two tables.
As we know that the snowflake query will be executed under the context and Context will have four major things like Role, warehouse, database, and schema, I have already created warehouse, database, and schema. In our scenario, I will be using the following context,
- Role-sysadmin
- Warehouse-compute_wh
- Database-employeemanagement
- Schema-EM
Let us set up the required table first.
Step 1
Set up the context. Execute the following query to set up the context.
- use role sysadmin;
- use warehouse compute_wh;
- use database employeeManagement;
- use schema EM;
Step 2
Create a table called employee. Execute the below query.
- create or replace table employee(emp_id int, emp_name varchar,emp_address varchar);
Step 3
Insert the record into employee table
- insert into employee values(1,'Nitesh','Hyderabad');
- insert into employee values(2,'Sam','Hyderabad');
- insert into employee values(3,'Kam','Pune');
Step 4
Create employee_skill table. Perform the below query
- create or replace table employee_skill(
- skill_id number,
- emp_id number,
- skill_name varchar(50),
- skill_level varchar(50)
- );
Step 5
Insert a few records. Execute the below query.
- insert into employee_skill values(1,1,'C#','Advance');
- insert into employee_skill values(2,1,'Python','Basic');
- insert into employee_skill values(3,1,'SQL','Intermediate');
- insert into employee_skill values(1,2,'C#','Advance');
Please check the inserted data by running the select command
Step 6
Execute the below query to verify the data
Step 7
Execute the below query to verify the data
- Select * from employee_skill;
Now, we have set up all the requirements. Let’s go ahead and work on the problem statement.
Scenario 1
Create View which will return table data in the form of rows and columns.
Solution
This is the normal scenario, where we create a view to populate data in the form of rows and columns.
Step 1
Create the View by running the below query
- create or replace view employee_view
- as select emp_id,emp_name, emp_address from employee;
Step 2
Run the View by executing the below query
Scenario 2
Create View which will return the data in Json format
Solution
In this problem state, we use the object_construct() and array_agg() function to populate data in Json format.
a. Object_construct() - It is a semi-structure data function. It returns the object is constructed from an argument. The data will be in key-value format. The result will be omitted if the key or value is NULL.
b. Array_agg() - It is an aggregate function for semi-structured data. It returns the input value, pivoted into array.
The output of the view will be in Json format and in this example, we will have the list of employees in Json object.
Step 1
Create json view with object_construct function
- CREATE OR replace VIEW employee_json_view
- AS
- WITH dc
- AS (SELECT Object_construct('employee_list', Array_agg(Object_construct(
- 'emp_id',
- emp_id,
- 'emp_name',
- emp_name,
- 'emp_address'
- ,
- emp_address)))
- FROM employee)
- SELECT *
- FROM dc;
Step 2
Execute the select query to verify the data.
- select * from employee_json_view;
Step 3
Click on result Row. You will see the Json format data.
Scenario 3
Create View which will return the data from two tables.
Solution
In this problem statement, I have tried to implement the real-time scenario where we have an employee with their number of skills. The data of employee is stored in the two tables. The first table will have data of employee and another table will have skill data for that employee. Normally, we use join to populate data if you want in the form of rows and columns. But in our case, it is a little bit different. Again we will be using object_construct() and array_agg() function to populate data in Json format.
Step 1
Create the View as per the below query.
- CREATE OR replace VIEW employee_skill_json_view
- AS
- WITH dc
- AS (SELECT emp_id,
- Array_agg(Object_construct('skill_id', skill_id, 'skill_name',
- skill_name,
- 'skill_level',
- skill_level)) skills
- FROM employee_skill
- GROUP BY emp_id),
- es
- AS (SELECT Object_construct('result_set', Array_agg(Object_construct(
- 'emp_id', e.emp_id,
- 'emp_name', e.emp_name,
- 'emp_address',
- e.emp_address,
- 'employee_skill',
- dc.skills)))
- FROM employee e
- inner join dc
- ON e.emp_id = dc.emp_id)
- SELECT *
- FROM es;
Step 2
Execute the select query to verify the view data
- Select * from employee_skill_json_view;
Step 3
Click on the Result Row to see the details
Conclusion
In Snowflake, we can use View for API purposes and populate the data in Json format as per our requirement. We can pull data from one or more than one table.
I hope you understand the use of Snowflake View with Json result.
Happy Learning!