Home > Software engineering >  Create a view in SQL based on 2 tables
Create a view in SQL based on 2 tables

Time:06-29

I have 2 tables in SQL like below

Table 1: Order_Number , Cost
Table 2: ID_Number, Order_Number

I want to create a view in SQL to have a table like below:

ID_Number, Cost

What is the SQL code?

Thanks

CodePudding user response:

You need to write a query like below

CREATE OR ALTER VIEW dbo.MyView
AS
Select ID_Number, Sum(Cost) as Cost
from Table1 t1
Join Table2 t2 on t1.Order_Number = t2.Order_Number
Group by ID_Number

CodePudding user response:

To be a beginner is one thing. This is a totally different thing. Your question reveals you failed to do any research before coming here. That said, I will leave you some basic links that can get you started.

In order to create a view from two tables you use JOINS. Depending on your SQL dialect, creating a view syntax may vary a little bit, please refer to creating views in SQL.

  • Related