从标志更改为新列时有效地提取列值
Efficiently extracting a column value from when a flag changes into a new column
我有每周重述的数据。在下面的 table 中,我收到 Date、Product、Location、Rate、FlagA 和 FlagB 列。 FlagC 是一个计算列,如果 FlagB 在任何未来的记录中被标记为 1,则将 FlagA = 1 的记录标记为 1。同样,我需要创建一个名为 FlagC_Rate 的计算列,它为 FlagB = 1 的行提取 Rate 列中的值(例如,在本例中为 0.06)。关于我应该写什么 select 语句以获得 FlagC_Rate 值的任何想法?我复制了 FlagC 列的代码作为参考。
用于计算 FLAGC 列的代码:
Select (SUM(case when FlagB = 1 then 1 else null end) OVER(Partition by product, location))
* (case when FlagA = 1 then 1 else null end) as FlagC FROM Table
尝试 FLAGC_RATE
Select (SUM(case when FlagB = 1 then 1 else null end) OVER(Partition by product, location))
* (case when FlagA = 1 then 1 else null end) * (case when FlagB = 1 then Rate else null end) as FLAGC_Rate FROM Table
关于我如何尝试填充的示例数据和示例 FLAGC_RATE
1 月数据
Date Product Location Rate FlagA FlagB FlagC FlagC_Rate
Jan 2020 WidgetA X 0.05 1 0 0 NULL
2 月数据(FlagC 列得到重述和填充)
Date Product Location Rate FlagA FlagB FlagC FlagC_Rate
Jan 2020 WidgetA X 0.05 1 0 1 0.06
Feb 2020 WidgetA X 0.06 0 1 0 NULL
3 月数据(标志 C 列为空,因为每个 product/location 配对只会看到一个具有 FlagC 和 FlagC_Rate 值的记录)
Date Product Location Rate FlagA FlagB FlagC FlagC_Rate
Jan 2020 WidgetA X 0.05 1 0 1 0.06
Feb 2020 WidgetA X 0.06 0 1 0 NULL
March 2020 WidgetA X 0.06 0 0 0 NULL
尝试以下操作:
Select *
, SUM(case when FlagB = 1 then 1 else null end) OVER(Partition by product, location) * (case when FlagA = 1 then 1 else null end) as FlagC
, max(case when FlagB = 1 then Rate else null end) OVER(Partition by product, location) * (case when FlagA = 1 then 1 else null end) as FLAGC_Rate
from data_table
请参阅数据库<>fiddle here.
我有每周重述的数据。在下面的 table 中,我收到 Date、Product、Location、Rate、FlagA 和 FlagB 列。 FlagC 是一个计算列,如果 FlagB 在任何未来的记录中被标记为 1,则将 FlagA = 1 的记录标记为 1。同样,我需要创建一个名为 FlagC_Rate 的计算列,它为 FlagB = 1 的行提取 Rate 列中的值(例如,在本例中为 0.06)。关于我应该写什么 select 语句以获得 FlagC_Rate 值的任何想法?我复制了 FlagC 列的代码作为参考。
用于计算 FLAGC 列的代码:
Select (SUM(case when FlagB = 1 then 1 else null end) OVER(Partition by product, location))
* (case when FlagA = 1 then 1 else null end) as FlagC FROM Table
尝试 FLAGC_RATE
Select (SUM(case when FlagB = 1 then 1 else null end) OVER(Partition by product, location))
* (case when FlagA = 1 then 1 else null end) * (case when FlagB = 1 then Rate else null end) as FLAGC_Rate FROM Table
关于我如何尝试填充的示例数据和示例 FLAGC_RATE
1 月数据
Date Product Location Rate FlagA FlagB FlagC FlagC_Rate
Jan 2020 WidgetA X 0.05 1 0 0 NULL
2 月数据(FlagC 列得到重述和填充)
Date Product Location Rate FlagA FlagB FlagC FlagC_Rate
Jan 2020 WidgetA X 0.05 1 0 1 0.06
Feb 2020 WidgetA X 0.06 0 1 0 NULL
3 月数据(标志 C 列为空,因为每个 product/location 配对只会看到一个具有 FlagC 和 FlagC_Rate 值的记录)
Date Product Location Rate FlagA FlagB FlagC FlagC_Rate
Jan 2020 WidgetA X 0.05 1 0 1 0.06
Feb 2020 WidgetA X 0.06 0 1 0 NULL
March 2020 WidgetA X 0.06 0 0 0 NULL
尝试以下操作:
Select *
, SUM(case when FlagB = 1 then 1 else null end) OVER(Partition by product, location) * (case when FlagA = 1 then 1 else null end) as FlagC
, max(case when FlagB = 1 then Rate else null end) OVER(Partition by product, location) * (case when FlagA = 1 then 1 else null end) as FLAGC_Rate
from data_table
请参阅数据库<>fiddle here.