How to create a View in SQL Server by unifying all columns of 3 tables?

Asked

Viewed 413 times

0

I have 3 tables with the same columns but with different records of different periods and no Pks.

I’d like to unify them into one view to analyze the data, have some command in SQL that helps me in this

I’m pretty new to SQL

tried the code:

create view vwEstudantes
select * FROM dbo.ESTUDANTES_2012;
select * FROM dbo.ESTUDANTES_2015_1;
select * FROM dbo.ESTUDANTES_2015_2;
  • From what I can understand what you want is UNION and not JOIN. Try: create view vwEstudantes AS
select * FROM dbo.ESTUDANTES_2012;
UNION ALL
select * FROM dbo.ESTUDANTES_2015_1;
UNION ALL
select * FROM dbo.ESTUDANTES_2015_2;.

  • Thank you very much was that, I only had to take the ; that worked, saved my night!

2 answers

0

Try to use the function UNION ALL between queries. In this example you passed will use twice. Abçs.

0

Use

(CREATE VIEW vwEstudantes AS
(SELECT * FROM dbo.ESTUDANTES_2012
UNION
SELECT * FROM dbo.ESTUDANTES_2015_1
UNION
SELECT * FROM dbo.ESTUDANTES_2015_2));

Reference

Note that having different tables to present the same data is not usually the ideal solution. The most correct approximation in this case would be to have only one table called ESTUDANTES with a field called SEMESTRE to differentiate them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.