连接两个表但仅在多个列匹配时才提取值
Joining two tables but pulling in values only if multiple columns match
我有下面的查询,它根据当周处理的量分配站量。站点大小基于全年查看 4 个数据点。这是因为车站的音量在全年波动,例如,它们可能在一个季度的音量很小,但在另一个季度的音量很大。目标是为最适合全年的站点分配一个大小。
下面的 sql 汇总了评估 station_size 将应用的大小所需的四个数据点。一年中可能会出现多种交易量组合,这就是为什么将它们存储在单独的 table 中的原因。
我有一个单独的 table 称为“station_sizes”,它根据从四个时间点获取的流量提供站大小的逻辑。
例如,如果一个站在第 14 周处理小体积,在第 27 周处理小体积,在第 40 周处理标准体积,在第 47 周处理小体积,那么它们将是一个 ds 小站大小。 station_size table.
中有 1000 多个场景
我希望最终输出是查询输出的摘录 + station_size。站点大小将从 station_size table.
中提取
我需要知道如何让下面的查询引用站大小 table 以便它可以识别每个年份组是 wk_16= xyz.. 和 wk_26 = xyz.. and wk_39 = xyz.. and wk_43 =xyz 然后它将从站大小 table.
中提取大小逻辑
对于查询中的 ar 部分。我希望逻辑是,如果一个电台至少对 AR 具有“是”,则它需要是一个标准的电台大小。我不确定我做的是否正确。比那个大不小我就可以了
SELECT station,
del_date,
sum(volume)/7 as volume_ref,DATE_PART("week",del_date) as week_num,DATE_PART("year",del_date) as year,
CASE
when sum(volume)/7 between 0 and 20000 then 'ds x-small'
when sum(volume)/7 between 20000 and 36000 and max(ar) <> 'yes' then 'ds small'
when sum(volume)/7 between 36000 and 42000 then 'ds standard'
when sum(volume)/7 between 42000 and 72000 then 'ds large'
when sum(volume)/7 > 72000 then 'ds x-large'
when max(ar) = 'YES' then 'ds standard'
else 'ds small'
end as station_ref
FROM prophecy_na.na_topology_lrp
Right JOIN wbr_global.raw_station_extended_attribute
ON prophecy_na.na_topology_lrp.station = wbr_global.raw_station_extended_attribute.ds
where week_num in(16,26,39,43)
GROUP BY station, del_date;
[![在此处输入图片描述][1]][1]
[![在此处输入图片描述][2]][2]
我已经为 station_size 添加了一个条件。请检查您是否正在寻找它。
SELECT station,
del_date,
sum(volume)/7 as volume_ref,DATE_PART("week",del_date) as week_num,DATE_PART("year",del_date) as year,
CASE
when sum(volume)/7 between 0 and 20000 then 'ds x-small'
when sum(volume)/7 between 20000 and 36000 and max(ar) <> 'yes' then 'ds small'
when sum(volume)/7 between 36000 and 42000 then 'ds standard'
when sum(volume)/7 between 42000 and 72000 then 'ds large'
when sum(volume)/7 > 72000 then 'ds x-large'
when max(ar) = 'YES' then 'ds standard'
else 'ds small'
end as station_ref,
case when a.wk_14='ds small' and a.wk_27='ds small' and wk_40='ds standard' and wk_47='ds small' then 'ds small' end station_size
FROM prophecy_na.na_topology_lrp l
Right JOIN wbr_global.raw_station_extended_attribute a
ON prophecy_na.na_topology_lrp.station = wbr_global.raw_station_extended_attribute.ds
where week_num in(16,26,39,43)
GROUP BY station, del_date;
您似乎缺少一些说明,我稍后会详细说明。您可能需要考虑为您的查询创建一个额外的查找 table,例如 StationSize。
StationSizeID
Description
MinVolume
ButLessThan
然后您可以在不调整查询的情况下调整您的范围(以防其他人使用相同的阈值。您可以使用
这样的值
StationSizeID Description MinVolume ButLessThan
1 ds x-small 0 20,000
2 ds small 20,000 36,000
3 ds standard 36,000 42,000
4 ds large 42,000 72,000
5 ds x-large 72,000 9,999,999
我们稍后会加入。
您还对“max(ar)”进行了最后一次应用的测试,但也在您的 ds-small 测试条件下。我们不知道这个“ar”字段从何而来,也不知道它可能值的上下文。如果基于字符串并且您有一个值为“是”的记录,那么它将取代任何“否”值,即使“否”是某个给定来源的最新值。此外,通过考虑 case/when 的交易量范围,并且任何给定周的交易量总和为 18,000,但它对 max(ar) 有一个“是”,它永远不会进入您的案例测试推送它达到“ds标准”水平。同样,如果您有一个 75k 的卷和一个 max(ar) = "yes",它也将保持 ds x-large 而不是 ds 标准。这是意图吗?是否应该预先测试“是”条件,所以如果“是”,它总是被认为是标准站?如果是这样,该条件需要改变。
现在,我的下一个级别需要澄清。您提到一年中有 4 周,但用户不了解所检查的给定周的内容/原因。如果给定的电台根据音量 activity 增长/下降,您是否正在寻找音量 activity?如果是这样,您是否试图提供一些权重因子来升级/降级站点大小?根据您的示例:最初是小的,标准的,小的,小的总体上被认为是小的。如果场景是:小,小,标准,标准怎么办?是否会随着规模的扩大而升级为标准尺寸的车站?
因此,除了人们向您提出疑问、了解您拥有的东西、您正在寻找的东西之外,只有适当的上下文才能真正为您提供您可能正在寻找的答案。我建议您编辑现有的 post,在该问题的详细信息中提供说明,而不是直接评论并保存。然后,您可以给我回评论以查看您 post 所做的更改,我可以继续进行更多操作。
修改/反馈
好的,我想我是从你的评论中关注你的。您的第二个 table 具有 1000 种组合,是比较给定周数量的基础,而您的 table 明确引用 14、27、40 和 47 这 4 周。但是查询您根据您在 16、26、39、43 内查找日期周的查询,正在执行的实际上是每个电台在不同周的 activity 的音量。因此,headers 栏实际上并不代表第 14 周、第 27 周、第 40 周、第 47 周的任何内容。它们只代表您要查询的 4 周,例如您可以查询第 2、11、24、31 周?在这种情况下,无论第一周的状态如何,都与您的站点大小 table 中“wk14”的匹配描述相关,同样对于 11 => Wk27,第 24 周 => Wk40,最后是第 31 周 => Wk47 .此站点大小 table 有 1000 种组合,以考虑在相应的每周位置中可能存在的值的所有(或大多数)排列。
但是,您没有像我在第一个显示低/高范围的示例中模拟的站大小 table,对吗?没有也没关系,只是试着看看比赛场地上的所有棋子。
我离你想要完成的事情越来越近了吗?
可能的解决方案
请注意...我不熟悉“dbeaver”数据库,但我打赌它是最常见的 sql 兼容。如果有任何问题,请告诉我,我们可以调整。
好的,我将尝试分段进行,这样您就可以看到所有部分是如何结合在一起的。让我们开始每周分组查询
select
lrp.station,
DATE_PART("week", lrp.del_date) as WkNum,
sum( lrp.volume ) / 7 as WkAvg,
-- since the date part of the group by will always be the same
-- correlating to the 1st, 2nd, 3rd or 4th week, I now have this
-- extra column and just apply MAX(), otherwise SQL will NAG if
-- it is not part of group by and its not an aggregate function
max( case when DATE_PART("week", lrp.del_date) = 16 then 1
when DATE_PART("week", lrp.del_date) = 26 then 2
when DATE_PART("week", lrp.del_date) = 39 then 3
else 4 end ) as WeekOrder
from
prophecy_na.na_topology_lrp lrp
where
-- EXAMPLE, if you KNOW you only care about within a current year
-- this will help optimize by pre-qualifying the single year you
-- expect the week activity within
lrp.del_Date >= '2021-01-01'
AND lrp.del_date < '2022-01-01'
-- and NOW, only filter out for the 4 specific weeks you are about
AND DATE_PART("week", lrp.del_date) in (16,26,39,43)
group by
lrp.station,
DATE_PART("week", lrp.del_date)
现在,如果您想要滚动一年时间的周,例如从 2020 年 10 月到 2021 年 8 月,只需将开始/结束日期过滤掉,这样您的周“43”就代表2020 年和第 3 周将是 2021 年 1 月,而您想要的周数的心理地图可能会被认为是
(第 43 周 = 2020 年 10 月,第 2 周 = 2021 年 1 月,第 15 周 = 2021 年 4 月,依此类推),因此您的周可能看起来像 (43, 2, 15, 21)。那么,您是否看到我认为您正在尝试覆盖的连续几周?
如果是这样,上述查询可能会得出如下结果。
Station WkNum WkAvg WeekOrder
x 16 34142 1
x 26 36879 2
x 39 35387 3
x 43 37114 4
y 16 43872 1
y 26 41983 2
y 39 42218 3
y 43 39461 4
z 16 ...
所以现在你有一个记录的主列表,每个站和你想要的每个查询的正确对应周,如果你做了年份 roll-over,只需更改正确周顺序的案例条件第 1-4 周的上下文。
现在,我们可以 运行 根据您的情况,将范围拉到描述符。所以我将使用上面的整个查询作为“预查询”(pq 别名)并构建更多。但是现在,我将应用 cross-tab 将可能的 4 周拉入一行,这将更直接地匹配您的 Station_Size table.
select
pq.Station,
max( case when pq.WeekOrder = 1
then VolRanges.StationSize else '' end ) as Wk1Size,
max( case when pq.WeekOrder = 2
then VolRanges.StationSize else '' end ) as Wk2Size,
max( case when pq.WeekOrder = 3
then VolRanges.StationSize else '' end ) as Wk3Size,
max( case when pq.WeekOrder = 4
then VolRanges.StationSize else '' end ) as Wk4Size,
-- just to tack on the "AR" status to extended properties
max( case when xa.station_id is null
then 'no' else 'yes' end ) as ARStatus
from
( entire first pre query above ) pq
JOIN
( select 'ds x-small' stationSize,
0 AtLeast,
20000 ButLessThan
UNION select 'ds small', 20000, 36000
UNION select 'ds standard', 36000, 42000
UNION select 'ds large', 42000, 72000
UNION select 'ds x-large', 72000, 9999999 ) VolRanges
on pq.WkAvg / 7 >= VolRanges.AtLeast
AND pq.WkAvg / 7 < VolRanges.ButLessThan
-- and also now getting the "ar" value from the extended properties
-- doing a LEFT-JOIN in case no such extended properties so the given
-- station is not left out of the query. So here, I am EXPLICITLY
-- pulling ONLY IF the ar = 'yes'
LEFT JOIN wbr_global.raw_station_extended_attribute xa
on pq.station_id = xa.ds
AND xa.ar = 'yes'
group by
pq.Station
所以现在,我们每个站点和一周仍然有一行,但是现在 tacked-on per-week 基础描述
站 Wk1Size Wk2Size Wk3Size Wk4Size ARStatus
x ds 小 ds 标准 ds 小 ds 小 否
y ds large ds standard ds large ds large 否
z ...
DII1(来自属性的样本,假设聚合)是
现在,我们需要将每个单独的周与其相应的 4 部分周 table 相结合,所以这是下一个嵌套,我将把它们与站点大小 table 联系起来4 个部分匹配。
select
perStation.*,
ss.station_size
from
( entire SECOND query above ) perStation
JOIN Station_Size ss
on perStation.Wk1Size = ss.wk_14
AND perStation.Wk2Size = ss.wk_27
AND perStation.Wk3Size = ss.wk_40
AND perStation.Wk4Size = ss.wk_47
因此,除了最终的“AR”考虑因素之外,以上内容将得到所有考虑。因此,让我们看看“AR”= 是的考虑因素。您有 5 种站大小类型。如果有任何东西高于标准,那就赢了。因此,您想要升级某人的唯一时间是 4wk 组合最终站大小 =“ds 小”并且 AR =“是”。所以让我们补充一点,其他的,保持原样。即:如果电台的 ar 状态为“否”,并且它们的大小为“ds small”,那是它保持小的唯一时间。
我只想更改以上内容以添加额外的 FinalStationSize 考虑因素。
select
perStation.*,
ss.station_size,
case when perStation.ARStatus = 'yes'
AND ss.Station_Size = 'ds small'
then 'ds standard'
else ss.Station_Size end FinalStationSize
您可能会遇到 SQL 可能与 dbeaver 数据库不兼容的错误,这就是为什么我试图向您展示构建事物的各个步骤,然后嵌套在一起以到达每个部分。如果有任何错误,请告诉我,我们可以尝试解决。
select
perStation.*,
ss.station_size,
case when perStation.ARStatus = 'yes'
AND ss.Station_Size = 'ds small'
then 'ds standard'
else ss.Station_Size end FinalStationSize
from
( select
pq.Station,
max( case when pq.WeekOrder = 1
then VolRanges.StationSize else '' end ) as Wk1Size,
max( case when pq.WeekOrder = 2
then VolRanges.StationSize else '' end ) as Wk2Size,
max( case when pq.WeekOrder = 3
then VolRanges.StationSize else '' end ) as Wk3Size,
max( case when pq.WeekOrder = 4
then VolRanges.StationSize else '' end ) as Wk4Size,
-- just to tack on the "AR" status to extended properties
max( case when xa.station_id is null
then 'no' else 'yes' end ) as ARStatus
from
( select
lrp.station,
DATE_PART("week", lrp.del_date) as WkNum,
sum( lrp.volume ) / 7 as WkAvg,
-- since the date part of the group by will always be the same
-- correlating to the 1st, 2nd, 3rd or 4th week, I now have this
-- extra column and just apply MAX(), otherwise SQL will NAG if
-- it is not part of group by and its not an aggregate function
max( case when DATE_PART("week", lrp.del_date) = 16 then 1
when DATE_PART("week", lrp.del_date) = 26 then 2
when DATE_PART("week", lrp.del_date) = 39 then 3
else 4 end ) as WeekOrder
from
prophecy_na.na_topology_lrp lrp
where
-- EXAMPLE, if you KNOW you only care about within a current year
-- this will help optimize by pre-qualifying the single year you
-- expect the week activity within
lrp.del_Date >= '2021-01-01'
AND lrp.del_date < '2022-01-01'
-- and NOW, only filter out for the 4 specific weeks you are about
AND DATE_PART("week", lrp.del_date) in (16,26,39,43)
group by
lrp.station,
DATE_PART("week", lrp.del_date) ) pq
JOIN
( select 'ds x-small' stationSize,
0 AtLeast,
20000 ButLessThan
UNION select 'ds small', 20000, 36000
UNION select 'ds standard', 36000, 42000
UNION select 'ds large', 42000, 72000
UNION select 'ds x-large', 72000, 9999999 ) VolRanges
on pq.WkAvg / 7 >= VolRanges.AtLeast
AND pq.WkAvg / 7 < VolRanges.ButLessThan
-- and also now getting the "ar" value from the extended properties
-- doing a LEFT-JOIN in case no such extended properties so the given
-- station is not left out of the query. So here, I am EXPLICITLY
-- pulling ONLY IF the ar = 'yes'
LEFT JOIN wbr_global.raw_station_extended_attribute xa
on pq.station_id = xa.ds
AND xa.ar = 'yes'
group by
pq.Station ) perStation
JOIN Station_Size ss
on perStation.Wk1Size = ss.wk_14
AND perStation.Wk2Size = ss.wk_27
AND perStation.Wk3Size = ss.wk_40
AND perStation.Wk4Size = ss.wk_47
我有下面的查询,它根据当周处理的量分配站量。站点大小基于全年查看 4 个数据点。这是因为车站的音量在全年波动,例如,它们可能在一个季度的音量很小,但在另一个季度的音量很大。目标是为最适合全年的站点分配一个大小。 下面的 sql 汇总了评估 station_size 将应用的大小所需的四个数据点。一年中可能会出现多种交易量组合,这就是为什么将它们存储在单独的 table 中的原因。 我有一个单独的 table 称为“station_sizes”,它根据从四个时间点获取的流量提供站大小的逻辑。
例如,如果一个站在第 14 周处理小体积,在第 27 周处理小体积,在第 40 周处理标准体积,在第 47 周处理小体积,那么它们将是一个 ds 小站大小。 station_size table.
中有 1000 多个场景我希望最终输出是查询输出的摘录 + station_size。站点大小将从 station_size table.
中提取我需要知道如何让下面的查询引用站大小 table 以便它可以识别每个年份组是 wk_16= xyz.. 和 wk_26 = xyz.. and wk_39 = xyz.. and wk_43 =xyz 然后它将从站大小 table.
中提取大小逻辑对于查询中的 ar 部分。我希望逻辑是,如果一个电台至少对 AR 具有“是”,则它需要是一个标准的电台大小。我不确定我做的是否正确。比那个大不小我就可以了
SELECT station,
del_date,
sum(volume)/7 as volume_ref,DATE_PART("week",del_date) as week_num,DATE_PART("year",del_date) as year,
CASE
when sum(volume)/7 between 0 and 20000 then 'ds x-small'
when sum(volume)/7 between 20000 and 36000 and max(ar) <> 'yes' then 'ds small'
when sum(volume)/7 between 36000 and 42000 then 'ds standard'
when sum(volume)/7 between 42000 and 72000 then 'ds large'
when sum(volume)/7 > 72000 then 'ds x-large'
when max(ar) = 'YES' then 'ds standard'
else 'ds small'
end as station_ref
FROM prophecy_na.na_topology_lrp
Right JOIN wbr_global.raw_station_extended_attribute
ON prophecy_na.na_topology_lrp.station = wbr_global.raw_station_extended_attribute.ds
where week_num in(16,26,39,43)
GROUP BY station, del_date;
[![在此处输入图片描述][1]][1] [![在此处输入图片描述][2]][2]
我已经为 station_size 添加了一个条件。请检查您是否正在寻找它。
SELECT station,
del_date,
sum(volume)/7 as volume_ref,DATE_PART("week",del_date) as week_num,DATE_PART("year",del_date) as year,
CASE
when sum(volume)/7 between 0 and 20000 then 'ds x-small'
when sum(volume)/7 between 20000 and 36000 and max(ar) <> 'yes' then 'ds small'
when sum(volume)/7 between 36000 and 42000 then 'ds standard'
when sum(volume)/7 between 42000 and 72000 then 'ds large'
when sum(volume)/7 > 72000 then 'ds x-large'
when max(ar) = 'YES' then 'ds standard'
else 'ds small'
end as station_ref,
case when a.wk_14='ds small' and a.wk_27='ds small' and wk_40='ds standard' and wk_47='ds small' then 'ds small' end station_size
FROM prophecy_na.na_topology_lrp l
Right JOIN wbr_global.raw_station_extended_attribute a
ON prophecy_na.na_topology_lrp.station = wbr_global.raw_station_extended_attribute.ds
where week_num in(16,26,39,43)
GROUP BY station, del_date;
您似乎缺少一些说明,我稍后会详细说明。您可能需要考虑为您的查询创建一个额外的查找 table,例如 StationSize。
StationSizeID
Description
MinVolume
ButLessThan
然后您可以在不调整查询的情况下调整您的范围(以防其他人使用相同的阈值。您可以使用
这样的值StationSizeID Description MinVolume ButLessThan
1 ds x-small 0 20,000
2 ds small 20,000 36,000
3 ds standard 36,000 42,000
4 ds large 42,000 72,000
5 ds x-large 72,000 9,999,999
我们稍后会加入。
您还对“max(ar)”进行了最后一次应用的测试,但也在您的 ds-small 测试条件下。我们不知道这个“ar”字段从何而来,也不知道它可能值的上下文。如果基于字符串并且您有一个值为“是”的记录,那么它将取代任何“否”值,即使“否”是某个给定来源的最新值。此外,通过考虑 case/when 的交易量范围,并且任何给定周的交易量总和为 18,000,但它对 max(ar) 有一个“是”,它永远不会进入您的案例测试推送它达到“ds标准”水平。同样,如果您有一个 75k 的卷和一个 max(ar) = "yes",它也将保持 ds x-large 而不是 ds 标准。这是意图吗?是否应该预先测试“是”条件,所以如果“是”,它总是被认为是标准站?如果是这样,该条件需要改变。
现在,我的下一个级别需要澄清。您提到一年中有 4 周,但用户不了解所检查的给定周的内容/原因。如果给定的电台根据音量 activity 增长/下降,您是否正在寻找音量 activity?如果是这样,您是否试图提供一些权重因子来升级/降级站点大小?根据您的示例:最初是小的,标准的,小的,小的总体上被认为是小的。如果场景是:小,小,标准,标准怎么办?是否会随着规模的扩大而升级为标准尺寸的车站?
因此,除了人们向您提出疑问、了解您拥有的东西、您正在寻找的东西之外,只有适当的上下文才能真正为您提供您可能正在寻找的答案。我建议您编辑现有的 post,在该问题的详细信息中提供说明,而不是直接评论并保存。然后,您可以给我回评论以查看您 post 所做的更改,我可以继续进行更多操作。
修改/反馈
好的,我想我是从你的评论中关注你的。您的第二个 table 具有 1000 种组合,是比较给定周数量的基础,而您的 table 明确引用 14、27、40 和 47 这 4 周。但是查询您根据您在 16、26、39、43 内查找日期周的查询,正在执行的实际上是每个电台在不同周的 activity 的音量。因此,headers 栏实际上并不代表第 14 周、第 27 周、第 40 周、第 47 周的任何内容。它们只代表您要查询的 4 周,例如您可以查询第 2、11、24、31 周?在这种情况下,无论第一周的状态如何,都与您的站点大小 table 中“wk14”的匹配描述相关,同样对于 11 => Wk27,第 24 周 => Wk40,最后是第 31 周 => Wk47 .此站点大小 table 有 1000 种组合,以考虑在相应的每周位置中可能存在的值的所有(或大多数)排列。
但是,您没有像我在第一个显示低/高范围的示例中模拟的站大小 table,对吗?没有也没关系,只是试着看看比赛场地上的所有棋子。
我离你想要完成的事情越来越近了吗?
可能的解决方案 请注意...我不熟悉“dbeaver”数据库,但我打赌它是最常见的 sql 兼容。如果有任何问题,请告诉我,我们可以调整。
好的,我将尝试分段进行,这样您就可以看到所有部分是如何结合在一起的。让我们开始每周分组查询
select
lrp.station,
DATE_PART("week", lrp.del_date) as WkNum,
sum( lrp.volume ) / 7 as WkAvg,
-- since the date part of the group by will always be the same
-- correlating to the 1st, 2nd, 3rd or 4th week, I now have this
-- extra column and just apply MAX(), otherwise SQL will NAG if
-- it is not part of group by and its not an aggregate function
max( case when DATE_PART("week", lrp.del_date) = 16 then 1
when DATE_PART("week", lrp.del_date) = 26 then 2
when DATE_PART("week", lrp.del_date) = 39 then 3
else 4 end ) as WeekOrder
from
prophecy_na.na_topology_lrp lrp
where
-- EXAMPLE, if you KNOW you only care about within a current year
-- this will help optimize by pre-qualifying the single year you
-- expect the week activity within
lrp.del_Date >= '2021-01-01'
AND lrp.del_date < '2022-01-01'
-- and NOW, only filter out for the 4 specific weeks you are about
AND DATE_PART("week", lrp.del_date) in (16,26,39,43)
group by
lrp.station,
DATE_PART("week", lrp.del_date)
现在,如果您想要滚动一年时间的周,例如从 2020 年 10 月到 2021 年 8 月,只需将开始/结束日期过滤掉,这样您的周“43”就代表2020 年和第 3 周将是 2021 年 1 月,而您想要的周数的心理地图可能会被认为是 (第 43 周 = 2020 年 10 月,第 2 周 = 2021 年 1 月,第 15 周 = 2021 年 4 月,依此类推),因此您的周可能看起来像 (43, 2, 15, 21)。那么,您是否看到我认为您正在尝试覆盖的连续几周?
如果是这样,上述查询可能会得出如下结果。
Station WkNum WkAvg WeekOrder
x 16 34142 1
x 26 36879 2
x 39 35387 3
x 43 37114 4
y 16 43872 1
y 26 41983 2
y 39 42218 3
y 43 39461 4
z 16 ...
所以现在你有一个记录的主列表,每个站和你想要的每个查询的正确对应周,如果你做了年份 roll-over,只需更改正确周顺序的案例条件第 1-4 周的上下文。
现在,我们可以 运行 根据您的情况,将范围拉到描述符。所以我将使用上面的整个查询作为“预查询”(pq 别名)并构建更多。但是现在,我将应用 cross-tab 将可能的 4 周拉入一行,这将更直接地匹配您的 Station_Size table.
select
pq.Station,
max( case when pq.WeekOrder = 1
then VolRanges.StationSize else '' end ) as Wk1Size,
max( case when pq.WeekOrder = 2
then VolRanges.StationSize else '' end ) as Wk2Size,
max( case when pq.WeekOrder = 3
then VolRanges.StationSize else '' end ) as Wk3Size,
max( case when pq.WeekOrder = 4
then VolRanges.StationSize else '' end ) as Wk4Size,
-- just to tack on the "AR" status to extended properties
max( case when xa.station_id is null
then 'no' else 'yes' end ) as ARStatus
from
( entire first pre query above ) pq
JOIN
( select 'ds x-small' stationSize,
0 AtLeast,
20000 ButLessThan
UNION select 'ds small', 20000, 36000
UNION select 'ds standard', 36000, 42000
UNION select 'ds large', 42000, 72000
UNION select 'ds x-large', 72000, 9999999 ) VolRanges
on pq.WkAvg / 7 >= VolRanges.AtLeast
AND pq.WkAvg / 7 < VolRanges.ButLessThan
-- and also now getting the "ar" value from the extended properties
-- doing a LEFT-JOIN in case no such extended properties so the given
-- station is not left out of the query. So here, I am EXPLICITLY
-- pulling ONLY IF the ar = 'yes'
LEFT JOIN wbr_global.raw_station_extended_attribute xa
on pq.station_id = xa.ds
AND xa.ar = 'yes'
group by
pq.Station
所以现在,我们每个站点和一周仍然有一行,但是现在 tacked-on per-week 基础描述
站 Wk1Size Wk2Size Wk3Size Wk4Size ARStatus x ds 小 ds 标准 ds 小 ds 小 否 y ds large ds standard ds large ds large 否 z ... DII1(来自属性的样本,假设聚合)是
现在,我们需要将每个单独的周与其相应的 4 部分周 table 相结合,所以这是下一个嵌套,我将把它们与站点大小 table 联系起来4 个部分匹配。
select
perStation.*,
ss.station_size
from
( entire SECOND query above ) perStation
JOIN Station_Size ss
on perStation.Wk1Size = ss.wk_14
AND perStation.Wk2Size = ss.wk_27
AND perStation.Wk3Size = ss.wk_40
AND perStation.Wk4Size = ss.wk_47
因此,除了最终的“AR”考虑因素之外,以上内容将得到所有考虑。因此,让我们看看“AR”= 是的考虑因素。您有 5 种站大小类型。如果有任何东西高于标准,那就赢了。因此,您想要升级某人的唯一时间是 4wk 组合最终站大小 =“ds 小”并且 AR =“是”。所以让我们补充一点,其他的,保持原样。即:如果电台的 ar 状态为“否”,并且它们的大小为“ds small”,那是它保持小的唯一时间。
我只想更改以上内容以添加额外的 FinalStationSize 考虑因素。
select
perStation.*,
ss.station_size,
case when perStation.ARStatus = 'yes'
AND ss.Station_Size = 'ds small'
then 'ds standard'
else ss.Station_Size end FinalStationSize
您可能会遇到 SQL 可能与 dbeaver 数据库不兼容的错误,这就是为什么我试图向您展示构建事物的各个步骤,然后嵌套在一起以到达每个部分。如果有任何错误,请告诉我,我们可以尝试解决。
select
perStation.*,
ss.station_size,
case when perStation.ARStatus = 'yes'
AND ss.Station_Size = 'ds small'
then 'ds standard'
else ss.Station_Size end FinalStationSize
from
( select
pq.Station,
max( case when pq.WeekOrder = 1
then VolRanges.StationSize else '' end ) as Wk1Size,
max( case when pq.WeekOrder = 2
then VolRanges.StationSize else '' end ) as Wk2Size,
max( case when pq.WeekOrder = 3
then VolRanges.StationSize else '' end ) as Wk3Size,
max( case when pq.WeekOrder = 4
then VolRanges.StationSize else '' end ) as Wk4Size,
-- just to tack on the "AR" status to extended properties
max( case when xa.station_id is null
then 'no' else 'yes' end ) as ARStatus
from
( select
lrp.station,
DATE_PART("week", lrp.del_date) as WkNum,
sum( lrp.volume ) / 7 as WkAvg,
-- since the date part of the group by will always be the same
-- correlating to the 1st, 2nd, 3rd or 4th week, I now have this
-- extra column and just apply MAX(), otherwise SQL will NAG if
-- it is not part of group by and its not an aggregate function
max( case when DATE_PART("week", lrp.del_date) = 16 then 1
when DATE_PART("week", lrp.del_date) = 26 then 2
when DATE_PART("week", lrp.del_date) = 39 then 3
else 4 end ) as WeekOrder
from
prophecy_na.na_topology_lrp lrp
where
-- EXAMPLE, if you KNOW you only care about within a current year
-- this will help optimize by pre-qualifying the single year you
-- expect the week activity within
lrp.del_Date >= '2021-01-01'
AND lrp.del_date < '2022-01-01'
-- and NOW, only filter out for the 4 specific weeks you are about
AND DATE_PART("week", lrp.del_date) in (16,26,39,43)
group by
lrp.station,
DATE_PART("week", lrp.del_date) ) pq
JOIN
( select 'ds x-small' stationSize,
0 AtLeast,
20000 ButLessThan
UNION select 'ds small', 20000, 36000
UNION select 'ds standard', 36000, 42000
UNION select 'ds large', 42000, 72000
UNION select 'ds x-large', 72000, 9999999 ) VolRanges
on pq.WkAvg / 7 >= VolRanges.AtLeast
AND pq.WkAvg / 7 < VolRanges.ButLessThan
-- and also now getting the "ar" value from the extended properties
-- doing a LEFT-JOIN in case no such extended properties so the given
-- station is not left out of the query. So here, I am EXPLICITLY
-- pulling ONLY IF the ar = 'yes'
LEFT JOIN wbr_global.raw_station_extended_attribute xa
on pq.station_id = xa.ds
AND xa.ar = 'yes'
group by
pq.Station ) perStation
JOIN Station_Size ss
on perStation.Wk1Size = ss.wk_14
AND perStation.Wk2Size = ss.wk_27
AND perStation.Wk3Size = ss.wk_40
AND perStation.Wk4Size = ss.wk_47