我正在使用SQL Server 2012。 我有以下查询从几个不同的视图中提取各种字段。每当ContentSetName列中有新组时,查询就会动态插值三个不同的值。另请注意,Cover的值仅在每组中出现一次。这一切都运行正常,但我现在需要更改ContentSetName列中用于report_SSRS的硬编码值以使用动态派生值。 请参阅此前有关此查询的Stackoverflow帖子
Dynamically interpolate extra rows in SQL query
此后,查询已演变为:
WITH Data AS
( SELECT b.ReportHeading1,
bb.ReportHeading1 as RepHeading1,
bb.ReportHeading2 as RepHeading2,
bb.ReportHeading3 as RepHeading3,
p.DisplayOrder,
p.MemberCode,
m.PortfolioGroupCode as DistGroup,
m.MemberCode as Packgroup,
g.purpose,
p.PortfolioGroupID as pid,
m.PortfolioGroupID as mid,
lb.value as repset,
RowNumber = ROW_NUMBER() OVER(PARTITION BY p.portfoliogroupid ORDER BY p.DisplayOrder)
FROM [APXFirm].[AdvApp].[vPortfolioGroupMemberFlattened] p
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioBase] b
ON b.PortfolioBaseID = p.PortfolioGroupID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioBase] bb
ON bb.PortfolioBaseID = p.MemberID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioGroupMember] m
on m.MemberID = p.PortfolioGroupID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioGroup] g
ON g.PortfolioGroupID = m.PortfolioGroupID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioBaseLabels] lb
on p.MemberID = lb.PortfolioBaseID
WHERE m.PortfolioGroupCode like '%_Dist%'
and (g.Purpose like '%_ind' or g.Purpose like '%_group')
and lb.Label = '$repset'
)
SELECT
d.repset,
d.DistGroup,
d.Packgroup,
t.ContentSetName,
CONCAT(d.pid,'_',d.DisplayOrder,'_',t.ContentSetName,'_') as DistributionName,
CONCAT(d.RepHeading1,'_',d.RepHeading2,'_', d.RepHeading3) as DistributionCustom,
'False' as IsForFunctionalGroup,
'True' as IsLandscapePageNum,
1 as NumOfCopies,
d.purpose as RecipientCode,
d.ReportHeading1 as RecipientFullName,
d.MemberCode as ReportingEntityCode,
'Daily' as RunEvent
FROM Data d
CROSS JOIN
( VALUES
('Cover_SSRS'),
('Separator_docx'),
('Report_SSRS')
) t (ContentSetName)
WHERE d.RowNumber = 1
OR t.ContentSetNAme != 'Cover_SSRS'
这将生成以下样本结果集。
repset DistGroup Packgroup ContentSetName DistributionName DistributionCustom IsForFunctionalGroup IsLandscapePageNum NumOfCopies RecipientCode RecipientFullName ReportingEntityCode RunEvent
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Cover_SSRS 11221_5251_Cover_SSRS_ Test Trust - Third Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test1 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Separator_docx 11221_5251_Separator_docx_ Test Trust - Third Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test1 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Report_SSRS 11221_5251_Report_SSRS_ Test Trust - Third Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test1 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Separator_docx 11221_5679_Separator_docx_ Test Trust - Fourth Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test2 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Report_SSRS 11221_5679_Report_SSRS_ Test Trust - Fourth Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test2 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Separator_docx 11221_5727_Separator_docx_ The Test Family Trust One__ FALSE TRUE 1 JonesWendy_ind Test Trusts test3 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Report_SSRS 11221_5727_Report_SSRS_ The Test Family Trust One__ FALSE TRUE 1 JonesWendy_ind Test Trusts test3 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Separator_docx 11221_5779_Separator_docx_ The Test Family Trust Three__ FALSE TRUE 1 JonesWendy_ind Test Trusts test4 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Report_SSRS 11221_5779_Report_SSRS_ The Test Family Trust Three__ FALSE TRUE 1 JonesWendy_ind Test Trusts test4 Daily
DVAStandard Wendy_Jones_Dist Wendy_Jones_Pack_2 Cover_SSRS 11222_5309_Cover_SSRS_ John P. Smith_Custodian for Jordan V. Smith UTMA_ FALSE TRUE 1 JonesWendy_ind John Smith test5 Daily
DVAStandard Wendy_Jones_Dist Wendy_Jones_Pack_2 Separator_docx 11222_5309_Separator_docx_ John P. Smith_Custodian for Jordan V. Smith UTMA_ FALSE TRUE 1 JonesWendy_ind John Smith test5 Daily
DVAStandard Wendy_Jones_Dist Wendy_Jones_Pack_2 Report_SSRS 11222_5309_Report_SSRS_ John P. Smith_Custodian for Jordan V. Smith UTMA_ FALSE TRUE 1 JonesWendy_ind John Smith test5 Daily
这是期望的结果集
repset DistGroup Packgroup ContentSetName DistributionName DistributionCustom IsForFunctionalGroup IsLandscapePageNum NumOfCopies RecipientCode RecipientFullName ReportingEntityCode RunEvent
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Cover_SSRS 11221_5251_Cover_SSRS_ Test Trust - Third Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test1 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Separator_docx 11221_5251_Separator_docx_ Test Trust - Third Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test1 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 DVAAnnual 11221_5251_DVAAnnual_ Test Trust - Third Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test1 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Separator_docx 11221_5679_Separator_docx_ Test Trust - Fourth Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test2 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 DVAAnnual 11221_5679_DVAAnnual_ Test Trust - Fourth Generation__ FALSE TRUE 1 JonesWendy_ind Test Trusts test2 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 Separator_docx 11221_5727_Separator_docx_ The Test Family Trust One__ FALSE TRUE 1 JonesWendy_ind Test Trusts test3 Daily
DVAAnnual Wendy_Jones_Dist Wendy_Jones_Pack_3 DVAAnnual 11221_5727_DVAAnnual_ The Test Family Trust One__ FALSE TRUE 1 JonesWendy_ind Test Trusts test3 Daily
DVAValues Wendy_Jones_Dist Wendy_Jones_Pack_3 Separator_docx 11221_5779_Separator_docx_ The Test Family Trust Three__ FALSE TRUE 1 JonesWendy_ind Test Trusts test4 Daily
DVAValues Wendy_Jones_Dist Wendy_Jones_Pack_3 DVAValues 11221_5779_DVAValues_ The Test Family Trust Three__ FALSE TRUE 1 JonesWendy_ind Test Trusts test4 Daily
DVAStandard Wendy_Jones_Dist Wendy_Jones_Pack_2 Cover_SSRS 11222_5309_Cover_SSRS_ John P. Smith_Custodian for Jordan V. Smith UTMA_ FALSE TRUE 1 JonesWendy_ind John Smith test5 Daily
DVAStandard Wendy_Jones_Dist Wendy_Jones_Pack_2 Separator_docx 11222_5309_Separator_docx_ John P. Smith_Custodian for Jordan V. Smith UTMA_ FALSE TRUE 1 JonesWendy_ind John Smith test5 Daily
DVAStandard Wendy_Jones_Dist Wendy_Jones_Pack_2 DVAStandard 11222_5309_DVAStandard_ John P. Smith_Custodian for Jordan V. Smith UTMA_ FALSE TRUE 1 JonesWendy_ind John Smith test5 Daily
当前和期望结果集之间的差异是,不是第三个插值行的静态值“Report_SSRS”,而是想要拉出d.repset的值,在本例中,d.repset的值在DVAAnnual,DVAValues和DVAStandard之间变化。
我还在DistributionName列中连接d.repset列(当前为t.contentsetname),并希望使用d.repset的值而不是Report_SSRS的硬编码值。
谢谢
答案 0 :(得分:1)
我认为您只需将查询更改为:
WITH Data AS
( SELECT b.ReportHeading1,
bb.ReportHeading1 as RepHeading1,
bb.ReportHeading2 as RepHeading2,
bb.ReportHeading3 as RepHeading3,
p.DisplayOrder,
p.MemberCode,
m.PortfolioGroupCode as DistGroup,
m.MemberCode as Packgroup,
g.purpose,
p.PortfolioGroupID as pid,
m.PortfolioGroupID as mid,
lb.value as repset,
RowNumber = ROW_NUMBER() OVER(PARTITION BY p.portfoliogroupid ORDER BY p.DisplayOrder)
FROM [APXFirm].[AdvApp].[vPortfolioGroupMemberFlattened] p
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioBase] b
ON b.PortfolioBaseID = p.PortfolioGroupID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioBase] bb
ON bb.PortfolioBaseID = p.MemberID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioGroupMember] m
on m.MemberID = p.PortfolioGroupID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioGroup] g
ON g.PortfolioGroupID = m.PortfolioGroupID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioBaseLabels] lb
on p.MemberID = lb.PortfolioBaseID
WHERE m.PortfolioGroupCode like '%_Dist%'
and (g.Purpose like '%_ind' or g.Purpose like '%_group')
and lb.Label = '$repset'
)
SELECT
d.repset,
d.DistGroup,
d.Packgroup,
t.ContentSetName,
CONCAT(d.pid,'_',d.DisplayOrder,'_',t.ContentSetName,'_') as DistributionName,
CONCAT(d.RepHeading1,'_',d.RepHeading2,'_', d.RepHeading3) as DistributionCustom,
'False' as IsForFunctionalGroup,
'True' as IsLandscapePageNum,
1 as NumOfCopies,
d.purpose as RecipientCode,
d.ReportHeading1 as RecipientFullName,
d.MemberCode as ReportingEntityCode,
'Daily' as RunEvent
FROM Data d
CROSS APPLY
( VALUES
('Cover_SSRS'),
('Separator_docx'),
(d.repset)
) t (ContentSetName)
WHERE d.RowNumber = 1
OR t.ContentSetNAme != 'Cover_SSRS'
区别在于使用:
CROSS JOIN
( VALUES
('Cover_SSRS'),
('Separator_docx'),
('Report_SSRS')
) t (ContentSetName)
已更改为
CROSS APPLY
( VALUES
('Cover_SSRS'),
('Separator_docx'),
(d.repset)
) t (ContentSetName)
通过使用CROSS APPLY,您仍然可以访问查询中的所有字段,而不是CROSS JOIN。