Merge two unrelated views into a single view

0

Please login or register to vote for this query.

(click on this box to dismiss)

Drupal Answers

Q&A for Drupal developers and administrators

declare @view_1 table (ClothingID int, Shoes varchar(10), Shirts varchar(10))
declare @view_2 table (ClothingID int, Shoes varchar(10), Shirts varchar(10))


insert into @view_1
select 1, 'v1aaaa3', 'v1aaa2' union
select 3, 'v1aabb3', 'v1aaf1' union
select 7, 'v1aacc3', 'v1aaf9' union
select 10, 'v1aadd5', 'v1aaf7'


insert into @view_2
select 5, 'v2ffaa3', 'v2ada2' union
select 8, 'v2ffbb3', 'v2adf1' union
select 9, 'v2hhcc3', 'v2adf9'


select v1.ClothingID, v2.ClothingID as ClothingID2, v1.Shoes, v2.Shoes as Shoes2,
    v1.Shirts, v2.Shirts as Shirts2
from (
    select *, (select count(*) from @view_1 t1 
        where t1.ClothingID <= t.ClothingID) as row
    from @view_1 t
) v1
full outer join (
    select *, (select count(*) from @view_2 t2
        where t2.ClothingID <= t.ClothingID) as row
    from @view_2 t
) v2 on v1.row = v2.row

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results