I want to join a first table to same combination of joins: X,Y,Z. The first table can be different table types, but they all join to same set of tables. Table A may have 8 columns of different data types, Table B may have 15 columns of different table types. How do I code for this without repetition? The tables join to other tables later. How would I save this combinations of joins in a view or table function, etc, if the first table is always different?
We have 100+ queries like this, and eventually may want to add more tables to saved X-Y-Z combination, maybe 7 in the future. S-T-U-V-X-Y-Z. The question becomes more of code versatility. Would like to change in 1 spot, rather than 100 different queries.
Note: Table X,Y,Z have different columns and data types, except on ProductId and ProductType.
Select a.*, x.*,y.*,z.*
from TableA a
left join DimShelf x
on b.ProductId = x.ProductId
and b.ProductTypeId = x.ProductTypeId -- 1
left join DimDesk y
on b.ProductId = y.ProductId
and b.ProductTypeId = y.ProductTypeId -- 2
left join DimCouch
on b.ProductId = z.ProductId
and b.ProductTypeId = z.ProductTypeId -- 3
left join other items of variation with where clauses at end...
Select b.*, x.*,y.*,z.*
from Tableb b
left join DimShelf x
on b.ProductId = x.ProductId
and b.ProductTypeId = x.ProductTypeId -- 1
left join DimDesk y
on b.ProductId = y.ProductId
and b.ProductTypeId = y.ProductTypeId -- 2
left join DimCouch
on b.ProductId = z.ProductId
and b.ProductTypeId = z.ProductTypeId -- 3
left join other items of variation with where clauses at end...
create table dbo.DimChair
(
DimChairId int primary key identity(1,1),
ProductTypeid int,
ProductId int,
Color varchar(55),
LegNumber int
)
create table dbo.DimShelf
(
DimShelfId int primary key identity(1,1),
ProductTypeid int,
ProductId int,
Length float,
Height float,
NumberofShelves varchar(55)
)
create table dbo.DimCouch
(
DimCouchId int primary key identity(1,1),
ProductTypeid int,
ProductId int,
FabricType varchar(255),
)