all groups > sql server mseq > november 2004 >
You're in the

sql server mseq

group:

Optional Inner Join


Optional Inner Join we7313
11/27/2004 1:03:02 PM
sql server mseq: I have a select proc that will take a bunch or criteria parameters. Based on
how many are not null I would like to decide how many inner joins to do.
for example:

select H1.Priceid as HotelPriceId,H2.Priceid as AirPriceId,
H1.VendorPackageId from
(
select HA.PriceId, HA.VendorPackageId from Criteria HA Inner Join
(
select VendorPackageId from ValidVendorPackages
where Vendor = 'SBT'
and Sitecode = 'system'
and PackageType = 'AHCF'
)HB on HA.VendorPackageId = HB.VendorPackageId
and
(
CriteriaId in
(
select CriteriaID from ValidItemCriteria
where Destination = 'LAS'
and LengthOfStay = 5
and Ages = 'A2'
and ComponentType = @ComponentType_1
and ValidItemType = @ValidItemType_1
and ItemValue = @ItemValue_1
)
)

)H1 INNER JOIN
(
select HA.PriceId, HA.VendorPackageId from Criteria HA Inner Join
(
select VendorPackageId from ValidVendorPackages
where Vendor = 'SBT'
and Sitecode = 'system'
and PackageType = 'AHCF'
)HB on HA.VendorPackageId = HB.VendorPackageId
and
(
CriteriaId in
(
select CriteriaID from ValidItemCriteria
where Destination = 'LAS'
and LengthOfStay = 5
and Ages = 'A2'
and ComponentType = @ComponentType_2
and ValidItemType = @ValidItemType_2
and ItemValue = @ItemValue_2
)
)
)H2 on H1.Priceid = H2.priceId Inner Join
(
select HA.PriceId, HA.VendorPackageId from Criteria HA Inner Join
(
select VendorPackageId from ValidVendorPackages
where Vendor = 'SBT'
and Sitecode = 'system'
and PackageType = 'AHCF'
)HB on HA.VendorPackageId = HB.VendorPackageId
and
(
CriteriaId in
(
select CriteriaID from ValidItemCriteria
where Destination = 'LAS'
and LengthOfStay = 5
and Ages = 'A2'
and ComponentType = @ComponentType_3
and ValidItemType = @ValidItemType_3
and ItemValue = @ItemValue_3
)
)
)H3 on H2.Priceid = H3.priceId

if values are only passed in from
@ComponentType_1,@ValidItemType_1,@ItemValue_1 I dont want to do any inner
joins.

If its passed in for @ComponentType_1,@ValidItemType_1,@ItemValue_1 &
@ComponentType_2,@ValidItemType_2,@ItemValue_2 I want to do the first Inner
Join.

and of course if I get all 3 sets of criteria I want to do both the inner
joins.
I know I can cut and past this thing 3 times with an if statement but that
isn't going to be practical.
--
Re: Optional Inner Join Anith Sen
11/29/2004 3:49:46 PM
With the posted information, it is hard to understand your requirements. Can
you post your table structures, sample data & expected results? For details,
refer to www.aspfaq.com/5006

--
Anith

AddThis Social Bookmark Button