Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi i am trying to combine two views into 1.The thing is that both views have the same column names.Lets say View 1 has a column named Test and View 2 has a column named Test too.I need to make an new view to contain only 1 Test column but with values from both previous views.
Posted
Updated 1-Aug-18 14:56pm

Name the views:
SQL
SELECT a.Col1, b.Col1, a.Col2, b.Col2, a.Col3 FROM View1 a
JOIN View2 b
ON a.Col1 = b.Col1
 
Share this answer
 
I don't see any problem doing that.
Something like following should work.
Your first view
SQL
CREATE VIEW [TestView1]
AS
SELECT 1 AS Id, 'Test 1' AS Value
UNION
SELECT 2,'Test 2'

GO

OUTPUT 1
Id	Value
______________
1	Test 1
2	Test 2


Your second view
SQL
CREATE VIEW [TestView2]
AS

SELECT 1 AS Id, 'Test 3' AS Value
UNION
SELECT 2,'Test 4'

GO

OUTPUT 2
Id	Value
______________
1	Test 3
2	Test 4


Your third view
SQL
CREATE VIEW [TestView3]
AS

SELECT Value FROM TestView1 
UNION --'UNION ALL' if you want to include duplicate records also
SELECT Value FROM TestView2

GO

OUTPUT 3
Value
______
Test 1
Test 2
Test 3
Test 4


There is no need to apply INNER JOIN which is there for some other purposes.

Hope, it helps :)
In case this doesn't resolve your problem, please let me know.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900