此刻,我正在尝试创建一个视图,将所有“默认”信息集中在一个地方。但是,棘手的部分是我想根据它们是否处于活动状态,将表1中的某些行动态连接起来。
CREATE VIEW [dbo].[BaseDefaults] AS
SELECT
dt.Rate,
dt2.Growth,
dt3.Value,
dt4.Period,
dt5.Type,
dt5.TypeValue,
// dynamically select those added from the dynamic join below
FROM (
SELECT defaultValue AS Rate, ROW_NUMBER() OVER(ORDER BY defaultValue) AS RowNumber
FROM defaultValues
WHERE defaultValueName = 'Rate'
) as dt
LEFT OUTER JOIN (
SELECT defaultValue as Growth, ROW_NUMBER() OVER(ORDER BY defaultValue) AS RowNumber
FROM defaultValues
WHERE defaultValueName = 'Growth'
) as dt2
ON dt.RowNumber = dt2.RowNumber
LEFT OUTER JOIN (
SELECT defaultValue as Value, ROW_NUMBER() OVER(ORDER BY defaultValue) AS RowNumber
FROM defaultValues
WHERE defaultValueName = 'Value'
) as dt3
ON dt2.RowNumber = dt3.RowNumber
LEFT OUTER JOIN (
SELECT Period, ROW_NUMBER() OVER(ORDER BY Period) AS RowNumber
FROM Period
WHERE isDefault = 1
) as dt4
ON dt3.RowNumber = dt4.RowNumber
LEFT OUTER JOIN (
SELECT Type, defaultValue as TypeValue, ROW_NUMBER() OVER(ORDER BY Type) AS RowNumber
FROM Types
WHERE isDefault = 1
) as dt5
ON dt4.RowNumber = dt5.RowNumber
// here I want to dynamically add all values as a column that are returned from
// SELECT defaultValueName, defaultValue
// FROM AnotherTable
// WHERE IsActive = 1
这可能吗?
我希望有这样的东西:
Rate Growth Value Period Type TypeValue DynamicV1 DynamicV2
0.1 0.2 10 5 Percent 0.2 0.2 0.3
仅在最终的保留连接中从选择查询返回的情况下才选择并添加DynamicV1和DynamicV2