Hi,
Please find attached the complete query
DECLARE @BoxId int =1471
DECLARE @PackagingSectionId int = 1
DECLARE @LanguageId int = 77
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @noLocation varchar(20) = 'Hide phone and email' --'noLocation'
declare @hiddenContact varchar(20) = 'Hide map localisation' --'hiddenContact'
declare @PhysicalProductFormatId int = 1
declare @BoxUniverse int
declare @BoxCode int, @BoxCodeS int, @BoxVersionS int
declare @BoxCountryId int = (SELECT DISTINCT fkCountryId FROM ProductCountry WHERE fkProductId = @BoxId)
declare @MasterBoxCode int
declare @ItalyCountryId INT = 109
declare @RomaMarketingRegionId INT = (SELECT MarketingRegionId FROM MarketingRegion WHERE MarketingRegionName = 'Roma')
declare @MilanoMarketingRegionId INT = (SELECT MarketingRegionId FROM MarketingRegion WHERE MarketingRegionName = 'Milano')
declare @WellnessUniverseId INT = 8
declare @GastronomyUniverseId INT = 2
select @MasterBoxCode = dbo.udf_GetMasterProductByProductId(@BoxId)
select @BoxCode = dbo.udf_GetPhysicalProductIdForLanguage(@BoxId, @LanguageId),
@BoxCodeS = dbo.udf_GetLatestSpecimenVersionId(@BoxId),
@BoxVersionS = dbo.udf_GetLatestSpecimenVersion(@BoxId)
select @BoxUniverse = fkUniverseId from Product inner join ProductUniverse on Product.ProductId = ProductUniverse.fkProductId
where Product.ProductId = @BoxId
if @BoxUniverse != 6
Begin
Select P.ShortName,[ProductVersion].[VersionNumber] as BoxVersion ,BXL.LanguageName as BoxLanguage,BXL.SBLanguageCode,BU.UniverseName as BoxUniverse,P.ProductName as BoxTitle,
B.BrandName as BoxBrand,C.CountryName as BoxCountry,
PR.PartnerId, PR.PartnerName,PR.PartnerCode,
CASE WHEN fvloc.value=1 THEN null ELSE PL.AddressLine1 END as AddressLine1,
PL.AddressLine2,PL.PostCode,PL.City,
PL.SubRegion as ParterSubRegion,
PartnerRegion = case when isnull(C.CountryName,'') <> PL.Country then isnull(CL.CountryName,PL.Country) else PL.Region end,
PL.Country,
CASE WHEN fv.value=1 THEN null
ELSE CASE WHEN left(isnull(PL.PhoneNo,''),1) <> '0' and PL.PhoneNo is not null THEN '0'+ PL.PhoneNo
ELSE PL.PhoneNo
END
END as PhoneNo,
CASE WHEN fv.value = 1 THEN null ELSE PL.Email END as Email,
PL.Website,PL.Longitude,PL.Latitude,
CanBeBooked = isnull((SELECT top 1 P.isReservable FROM Product P
JOIN RelatedProduct RP ON P.ProductId = RP.fkProduct1Id
WHERE RP.fkRelationTypeId = 40 AND RP.fkProductId = CP.ProductId
and P.isReservable = 1 ),0),
PL.MarketingRegion as LocationName,
EU.UniverseName, CP.ProductId AS ExperienceId, CP.ProductCode,
ExperienceCount = DENSE_RANK() OVER (PARTITION BY P.ProductId,PR.PartnerId ORDER BY ExperienceRank desc,CP.Productid),
EPT.ProductTemplateId,
PPT.PartnerTemplateId,
MarketingRegionClassification = case when isnull(C.CountryName,'') <> PL.Country then isnull(CL.CountryName,PL.Country) else MRU.MarketingRegion END,
ExperienceWeb = case when exists(select RelatedProductPackagingSectionId from RelatedProductPackagingSection RPPS where RPPS.fkRelatedProductId = RP.RelatedProductId and RPPS.fkPackagingSectionId = 1) then 'Yes' else 'No' end,
ExperienceSpecimen = case when exists(select RelatedProductPackagingSectionId from RelatedProductPackagingSection RPPS where RPPS.fkRelatedProductId = RP.RelatedProductId and RPPS.fkPackagingSectionId = 3) then 'Yes' else 'No' end,
ExperienceBooklet = case when exists(select RelatedProductPackagingSectionId from RelatedProductPackagingSection RPPS where RPPS.fkRelatedProductId = RP.RelatedProductId and RPPS.fkPackagingSectionId = 4) then 'Yes' else 'No' end,
SubRegionNumber = substring(isnull(PL.SubRegionISOCode,''), 4, 10) ,
BoxCode = @BoxCode,
BoxCodeS = @BoxCodeS,
BoxVersionS = @BoxVersionS,
MasterBoxCode = @MasterBoxCode,
MinNumberOfPeople = (select top 1 FV.Value from ProductFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 45 and PFV.fkProductId = CP.ProductId),
MaxNumberOfPeople = (select top 1 FV.Value from ProductFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 44 and PFV.fkProductId = CP.ProductId),
[Michelin Rating] = isnull((select top 1 FV.Value from PartnerFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 61 and PFV.fkPartnerId = PR.PartnerId),''),
[TripAdvisor Rating] = isnull((select top 1 FV.Value from PartnerFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 35 and PFV.fkPartnerId = PR.PartnerId),''),
[TripAdvisor Certificate of Excellence] = isnull((select top 1 FV.Value from PartnerFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 34 and PFV.fkPartnerId = PR.PartnerId),''),
[TripAdvisor Travellers Choice] = isnull((select top 1 FV.Value from PartnerFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 36 and PFV.fkPartnerId = PR.PartnerId),''),
[Logis de France] = isnull((select top 1 FV.Value from PartnerFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 23 and PFV.fkPartnerId = PR.PartnerId),''),
[Gault et Millaud] = isnull((select top 1 FV.Value from PartnerFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 14 and PFV.fkPartnerId = PR.PartnerId),''),
[Bib gourmand] = isnull((select top 1 FV.Value from PartnerFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 62 and PFV.fkPartnerId = PR.PartnerId),''),
[Plate] = isnull((select top 1 FV.Value from PartnerFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 63 and PFV.fkPartnerId = PR.PartnerId),''),
[Hotel di charme] = isnull((select top 1 FV.Value from PartnerFacetValue PFV
Inner Join FacetValue FV on FV.FacetValueId = PFV.fkFacetValueId
where FV.fkFacetId = 18 and PFV.fkPartnerId = PR.PartnerId),''),
[IndexPageNumber] = (
SELECT TOP 1 NumberOfIndexPages FROM [EditionBrief]
INNER JOIN [EditionBriefRegionalIndexPages] ON [EditionBriefRegionalIndexPages].[fkEditionBriefId] = [EditionBrief].[EditionBriefId]
WHERE [fkBoxId] = [P].[ProductId] AND fkMarketingRegionsId =
CASE
WHEN cntry.CountryId <> @BoxCountryId THEN mr2.MarketingRegionId
WHEN (@BoxCountryId = @ItalyCountryId AND BU.UniverseId IN (@WellnessUniverseId, @GastronomyUniverseId)) THEN
CASE PL.City
WHEN 'Roma' THEN @RomaMarketingRegionId
WHEN 'Milano' THEN @MilanoMarketingRegionId
ELSE mr1.MarketingRegionId
END
ELSE mr1.MarketingRegionId
END
AND EditionBrief.fkSpecimenId = @BoxCodeS
),
[BoxFormat] = STUFF((SELECT ',' + [Format].[Name] FROM [AvailableProductFormat] INNER JOIN [Format] ON [Format].[FormatId] = [AvailableProductFormat].[fkFormatId] WHERE [fkProductId]=[P].[ProductId] ORDER BY [PriorityForConcept] ASC FOR XML PATH('')),1,1,''),
[IndexLayout] = (SELECT TOP 1 edb.IndexLayout FROM EditionBrief as edb WHERE fkBoxId = @BoxId AND fkSpecimenId = @BoxCodeS),
[PartnerPagesLayout] = (SELECT TOP 1 edb.PartnerPagesLayout FROM EditionBrief as edb WHERE fkBoxId = @BoxId),
[BrandPartnership] = (SELECT ISNULL([BrandPartnership].[Name],'none') FROM [Product] LEFT JOIN [BrandPartnership] ON [BrandPartnership].[BrandPartnershipId] = [Product].[fkBrandPartnershipId] WHERE [Product].[ProductId] = [P].[ProductId])
Into #BoxExtract2
From Product P
Inner Join RelatedProduct RP with (nolock) on RP.fkProductId = P.ProductId and RP.Active = 1
Inner Join PartnerProduct PP with (nolock) on PP.fkProductId = RP.fkProduct1Id and PP.Active = 1
Inner Join Partner PR with (nolock) on PR.PartnerId = PP.fkPartnerId and PR.PartnerStatus = 1
Inner Join PartnerLocation PL with (nolock) on PL.fkPartnerId = PR.PartnerId
-- BEGINNING Modification DB
inner join Country cntry ON cntry.CountryName = PL.Country
left join MasterLocations ml1 ON ml1.MarketingSubRegion = PL.MarketingSubRegion
left JOIN MasterLocations ml2 ON ml2.SubRegionISO = PL.SubRegionISOCode
left JOIN MasterLocations ml3 ON ml3.RegionName = PL.Region
left join MarketingRegionClassification mrc on mrc.fkUniverseId = @BoxUniverse and mrc.fkBoxCountryId = @BoxCountryId AND mrc.fkMasterLocationId IS NULL AND mrc.fkPartnerCountryId = cntry.CountryId
left join MarketingRegionClassification mrc2 on mrc2.fkUniverseId = @BoxUniverse and mrc2.fkBoxCountryId = @BoxCountryId AND mrc2.fkMasterLocationId = isnull(ml1.MasterLocationsId,isnull(ml2.MasterLocationsId,ml3.MasterLocationsId)) AND mrc2.fkPartnerCountryId = cntry.CountryId
left join MarketingRegionClassification mrc3 on mrc3.fkUniverseId = @BoxUniverse and mrc3.fkBoxCountryId = @BoxCountryId AND mrc3.fkMasterLocationId is null AND mrc3.fkPartnerCountryId = cntry.CountryId
left join MarketingRegion mr1 on mr1.MarketingRegionId = mrc2.fkMarketingRegionId
left join MarketingRegion mr2 on mr2.MarketingRegionId = mrc3.fkMarketingRegionId
-- END Modification DB
Inner Join ProductUniverse PU with (nolock) on PU.fkProductId = P.ProductId
left join ProductBrand PB with (nolock) on PB.fkProductId= P.ProductId
left join Brand B with (nolock) on B.BrandId=PB.fkBrandId
Left Join Universe BU with (nolock) on BU.UniverseId = P.fkUniverseBoxId
Left Join MarketingRegionUniverse MRU with (nolock) on MRU.UniverseId = P.fkUniverseBoxId and MRU.SubregionISOCode = PL.SubRegionISOCode
Inner Join Product CP with (nolock) on CP.ProductId = RP.fkProduct1Id and CP.fkTriggerStateId = 4
Inner Join ProductUniverse CPU with (nolock) on CPU.fkProductId = CP.ProductId
Inner Join Universe EU with (nolock) on EU.UniverseId = CPU.fkUniverseId
Inner Join ProductLanguage BL with (nolock) on BL.fkProductId = P.ProductId
Left join Country PLC with (nolock) on PLC.CountryName = PL.Country
Left join CountryLocale CL with (nolock) on CL.fkCountryId = PLC.CountryId and BL.fkLanguageId = CL.fkLanguageId
Inner Join Language BXL with (nolock) on BXL.LanguageId= BL.fkLanguageId
Left join ProductCountry PC with (nolock) on PC.fkProductId = P.ProductId
Left join Country C with (nolock) on C.CountryId = PC.fkCountryId
Inner join ProductDataTemplate PDT with (nolock) on PDT.fkProductId = p.ProductId AND PDT.fkProductTypeId = 2 and PDT.fkUniverseId is null
Inner join ProductDataTemplate PDPT with (nolock) on PDPT.fkProductId = p.ProductId AND PDPT.isPartnerTemplate = 1 and PDPT.fkUniverseId is null
Inner Join ProductTemplate EPT with (nolock) on EPT.fkProductId = CP.ProductId and EPT.fkLanguageId = BL.fkLanguageId and EPT.IsDeleted = 0 and EPT.fkProductTemplateId is null and EPT.fkTriggerStateId = 14 and EPT.fkDataTemplateId = PDT.fkDataTemplateId
Inner Join PartnerTemplate PPT with (nolock) on PPT.fkPartnerId = PR.PartnerId and PPT.fkLanguageId = BL.fkLanguageId and PPT.IsDeleted = 0 and PPT.fkPartnerTemplateId is null and PPT.fkTriggerStateId = 14 and PPT.fkDataTemplateId = PDPT.fkDataTemplateId
Left join facet f on f.FacetName = @hiddenContact
Left join PartnerFacetValue PFV on PFV.fkPartnerId = ppt.fkPartnerId and pfv.fkFacetId = f.facetid
Left join facetValue fv on fv.facetvalueid = pfv.fkfacetvalueid
Left join facet floc on floc.FacetName = @noLocation
Left join PartnerFacetValue PFVloc on PFVloc.fkPartnerId = ppt.fkPartnerId and PFVloc.fkFacetId = floc.facetid
Left join facetValue fvloc on fvloc.facetvalueid = PFVloc.fkfacetvalueid
Left Join (Select sum( ps.rank) as ExperienceRank,RPPS.fkRelatedProductId from RelatedProductPackagingSection RPPS with (nolock)
inner join packagingsection ps with (nolock) on ps.packagingsectionid= rpps.fkpackagingsectionid group by RPPS.fkRelatedProductId ) as ERank on rp.RelatedProductId = Erank.fkRelatedProductId
LEFT JOIN [ProductVersion] WITH (NOLOCK) ON [ProductVersion].[fkProductId] = [P].[ProductId] AND [ProductVersion].[ProductVersionId] = (SELECT MAX([ProductVersionId]) FROM [ProductVersion] [prv] WHERE [prv].[fkProductId] = [P].[ProductId] AND fkProductVersionFormatId = @PhysicalProductFormatId)
Where P.ProductId = @BoxId and BL.fkLanguageId = @LanguageId--and PR.PartnerId = 8725
Order by P.ProductId,PR.PartnerId,CP.ProductId
create nonclustered index idx_tempindex on #BoxExtract2 (PartnerCode, ExperienceCount)
create nonclustered index idx_tempindex2 on #BoxExtract2 ([IndexPageNumber]) INCLUDE ([PartnerId])
delete b1
from #BoxExtract2 b1
join #BoxExtract2 b2 on b2.PartnerId=b1.PartnerId and b2.IndexPageNumber is not null
where b1.IndexPageNumber is null
select a.*,isnull(PTA.ProductContents,'') ExperienceA_Contents,isnull(PTB.ProductContents,'') ExperienceB_Contents,isnull(PTC.ProductContents,'') ExperienceC_Contents,isnull(PTT.PartnerContents,'') Partner_Contents from (
Select distinct
ShortName,
MasterBoxCode,
BoxCode,
BoxCodeS,
BoxVersion,
BoxVersionS,
[IndexPageNumber],
[BoxFormat],
IndexLayout,
PartnerPagesLayout,
BoxBrand,
BoxCountry,
BoxLanguage,
BoxUniverse,
[BrandPartnership],
[BoxTitle],
ExperienceUniverse = isnull((select top 1 B2.UniverseName from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 1),'') ,
Web = isnull((select top 1 B2.ExperienceWeb from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount in (1,2,3) and B2.ExperienceWeb = 'Yes'),'No') ,
Specimen = isnull((select top 1 B2.ExperienceSpecimen from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount in (1,2,3) and B2.ExperienceSpecimen = 'Yes'),'No') ,
Booklet = isnull((select top 1 B2.ExperienceBooklet from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount in (1,2,3) and B2.ExperienceBooklet = 'Yes'),'No') ,
PartnerId, PartnerName,PartnerCode,replace(isnull(AddressLine1,''),',','') as AddressLine1,isnull(AddressLine2,'') as AddressLine2,isnull(PostCode,'') as PostCode,isnull(City,'') as City,
isnull(PartnerRegion,'') as RegionName,
isnull(ParterSubRegion,'') AS SubRegionName /*as [Parnter Subregion Name]*/,
SubRegionNumber,
isnull(Country,'') as Country,
PhoneNo as 'PhoneNumber',
isnull(Email,'') as Email,isnull(Website,'') as Website,Longitude,Latitude,
CanBeBooked = cast(isnull((select top 1 B2.CanBeBooked from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 1),0) as int) ,
MaxNumberOfPeople = isnull((select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount in (1,2,3) order by B2.MaxNumberOfPeople),'') ,
MinNumberOfPeople = isnull((select top 1 B2.MinNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount in (1,2,3) order by B2.MinNumberOfPeople),'') ,
ExperienceA_Id = isnull((select top 1 B2.ExperienceId from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 1),'') ,
ExperienceA_Code = isnull((select top 1 B2.ProductCode from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 1),'') ,
ExperienceA_Template = isnull((select top 1 B2.ProductTemplateId from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 1),'') ,
ExperienceB_Id = isnull((select top 1 B2.ExperienceId from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 2),'') ,
ExperienceB_Code = isnull((select top 1 B2.ProductCode from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 2),'') ,
ExperienceB_Template = isnull((select top 1 B2.ProductTemplateId from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 2),'') ,
ExperienceC_Id = isnull((select top 1 B2.ExperienceId from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 3),'') ,
ExperienceC_Code = isnull((select top 1 B2.ProductCode from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 3),'') ,
ExperienceC_Template = isnull((select top 1 B2.ProductTemplateId from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 3),'') ,
ExperienceA_Entitles = dbo.udf_LocaleData(case when (select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 1) is not null then 'Pour ' + isnull((select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 1),'0') + ' personne' + case when isnull((select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 1),'1') = '1' then '.' else 's.' end else '' end,SBLanguageCode),
ExperienceB_Entitles = dbo.udf_LocaleData(case when (select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 2) is not null then 'Pour ' + isnull((select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 2),'0') + ' personne' + case when isnull((select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 2),'1') = '1' then '.' else 's.' end else '' end,SBLanguageCode),
ExperienceC_Entitles = dbo.udf_LocaleData(case when (select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 3) is not null then 'Pour ' + isnull((select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 3),'0') + ' personne' + case when isnull((select top 1 B2.MaxNumberOfPeople from #BoxExtract2 B2 Where B2.PartnerCode = Box.PartnerCode and B2.ExperienceCount = 3),'1') = '1' then '.' else 's.' end else '' end,SBLanguageCode),
isnull(PartnerTemplateId,'') as PartnerTemplateId,
isnull(MarketingRegionClassification,'') as [Marketing Region Classification],
[Michelin Rating],[TripAdvisor Rating],[TripAdvisor Certificate of Excellence],[TripAdvisor Travellers Choice],[Logis de France],[Gault et Millaud],
[Bib gourmand],[Plate],[Hotel di charme]
From #BoxExtract2 BOX
) a
left join ProductTemplate PTA on PTA.ProductTemplateId = a.ExperienceA_Template
left join ProductTemplate PTB on PTB.ProductTemplateId = a.ExperienceB_Template
left join ProductTemplate PTC on PTC.ProductTemplateId = a.ExperienceC_Template
left join PartnerTemplate PTT on PTT.PartnerTemplateId = a.PartnerTemplateId
where (case when @PackagingSectionId = 3 and a.Specimen = 'Yes' then 1
when @PackagingSectionId = 4 and a.Booklet = 'Yes' then 1
when @PackagingSectionId = 1 and a.Web = 'Yes' then 1
else 0 end) = 1
order by PartnerCode
end