5

I have two Views I want to merge into single view for example

View 1 is defined as:

CREATE VIEW view1 AS
select DV.demValueId,DV.value,DE.demElementId,DE.name,
       DES.demElementSetGroupId,DR.demRecordId,DR.name as Role,
       DR.demClusterSetGroupRef,L.EmailId 
from DemographicsDataValue DV 
inner join DemographicsElement DE 
      on DV.demValueId=DE.demValueRef 
inner join DemographicsElementSet DES 
      on DE.demElementId=DES.demElementRef 
inner join DemographicsRecord DR 
     on DR.demElementSetGroupRef=DES.demElementSetGroupId 
inner join LoginDetail L 
     on DR.demRecordId=L.demRecordIdRef

View 2 is defined as:

CREATE VIEW view2 
AS
select DV.valueId, DV.value,E.ElementId,E.name,
       ES.ElementSetGroupId,ET.entryId,ET.name as E_Name,
       ET.SystemDateTime,C.compositionId,C.linkSetRef 
from DataValue DV 
inner join Element E 
      on DV.valueId=E.valueRef 
inner join ElementSet ES 
      on E.ElementId=ES.ElementRef 
inner join Entry ET 
      on ES.elementSetGroupId=ET.elementSetGroupRef 
inner join EntrySet ESG 
      on ESG.entryRef=ET.entryId 
inner Join Section S 
      on s.entrySetGroupRef=ESG.entrySetGroupId 
inner join SectionSet SS 
      on SS.sectionRef=S.sectionId 
where S.name='Appointment'

How do I combine them into a single view select * from View1 Select * from View2

output into single query

please help

12
  • 1
    Are the columns the same in each view? Assuming so, use union...
    – sgeddes
    Nov 6, 2015 at 13:40
  • no both view have different column Nov 6, 2015 at 13:41
  • treat them as tables and join them on a suitable field.
    – Dan Bracuk
    Nov 6, 2015 at 13:42
  • To combine the results of the views, you'd need to have the same columns. If not, how do you combine them and what are your expected results? If you post the queries for each view, we might be able to help you more. As written, very difficult to offer any further assistance.
    – sgeddes
    Nov 6, 2015 at 13:42
  • @SarfarazAli you need to give more details of the columns in each view and what output you are expecting
    – Steve Ford
    Nov 6, 2015 at 13:43

2 Answers 2

13

if you want the common fields you can do:

select * FROM View1
inner join View2 -- or you can do a left join here
on View1.Column = View2.Column -- and ... add more if needed

If the table structure is the same for both and you don't care about duplicates you can do an union all if you care about the duplicates then union

select * from View1
union all
select * from View2
1
  • @CM@K: In my both view have different column name with different value Nov 6, 2015 at 14:12
0

Use union if you have homogeneous columns. Otherwise use Join on a suitable field

SELECT *
FROM view1
INNER JOIN view2 ON view1.id = view2.ref;

More details are needed to know which join to use and on which fields.

4
  • I have different column name with different value in both view they don't match each other.then what I do Nov 6, 2015 at 14:11
  • 1
    @SarfarazAli please show your columns and data types of your views and your expected output, otherwise we are just guessing!
    – Steve Ford
    Nov 6, 2015 at 14:13
  • In view1 Query as "select DV.demValueId, DV.value,DE.demElementId,DE.name,DES.demElementSetGroupId,DR.demRecordId,DR.name as Role,DR.demClusterSetGroupRef,L.EmailId from DemographicsDataValue DV inner join DemographicsElement DE on DV.demValueId=DE.demValueRef inner join DemographicsElementSet DES on DE.demElementId=DES.demElementRef inner join DemographicsRecord DR on DR.demElementSetGroupRef=DES.demElementSetGroupId inner join LoginDetail L on DR.demRecordId=L.demRecordIdRef" Nov 6, 2015 at 14:20
  • select DV.valueId, DV.value,E.ElementId,E.name,ES.ElementSetGroupId,ET.entryId,ET.name as E_Name,ET.SystemDateTime,C.compositionId,C.linkSetRef from DataValue DV inner join Element E on DV.valueId=E.valueRef inner join ElementSet ES on E.ElementId=ES.ElementRef inner join Entry ET on ES.elementSetGroupId=ET.elementSetGroupRef inner join EntrySet ESG on ESG.entryRef=ET.entryId inner Join Section S on s.entrySetGroupRef=ESG.entrySetGroupId inner join SectionSet SS on SS.sectionRef=S.sectionId where S.name='Appointment' Nov 6, 2015 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.