如何从中创建视图 select,并将其与现有 table
How do I create a view, select from it, and join it with an existing table
所以这是生成我想要的数据的代码 table 1.
with data as
(
select
CP_REF,
count(*) * 1.0 /
nullif(count(case when QUANTITY > 0 then 1 end), 0) as ADI,
stdevp(QUANTITY) / nullif(avg(QUANTITY), 0) as COV
from
DF_ALL_DEMAND_BY_ROW_V
where
parent is not null
group by
CP_REF
)
select
CP_REF, ADI, COV
case
when ADI < 1.32 and COV * COV < 0.49 then 'Smooth'
when ADI >= 1.32 and COV * COV < 0.49 then 'Intermittent'
when ADI < 1.32 and COV * COV >= 0.49 then 'Erratic'
when ADI >= 1.32 and COV * COV <= 0.49 then 'Lumpy'
else 'No Quantity Measured'
end
from data;
这提供了一个非常优雅的解决方案...
这就是我要找的,但是,现在我需要获取此数据并将其与原始 table DF_ALL_DEMAND_BY_ROW_V
合并。我希望它包含 DF_ALL_DEMAND_BY_ROW_V
中存在的所有内容,但将上面的数据加入 CP_REF
,我认为这是一个 LEFT JOIN
,但我无法弄清楚。
不确定这是否是您的意思,但是为什么不创建第二个 CTE 并加入其中,例如
with data as (
select
...
), Q2 as (
select
CP_REF, ADI,
case
when ADI < 1.32 and COV * COV < 0.49 then 'Smooth'
when ADI >= 1.32 and COV * COV < 0.49 then 'Intermittent'
when ADI < 1.32 and COV * COV >= 0.49 then 'Erratic'
when ADI >= 1.32 and COV * COV <= 0.49 then 'Lumpy'
else 'Smooth'
end as StatusOrWhatever
from data
)
select stuff
from DF_ALL_DEMAND_BY_ROW_V v
left join Q2 on D2.CF_REF = v.CP_REF
所以这是生成我想要的数据的代码 table 1.
with data as
(
select
CP_REF,
count(*) * 1.0 /
nullif(count(case when QUANTITY > 0 then 1 end), 0) as ADI,
stdevp(QUANTITY) / nullif(avg(QUANTITY), 0) as COV
from
DF_ALL_DEMAND_BY_ROW_V
where
parent is not null
group by
CP_REF
)
select
CP_REF, ADI, COV
case
when ADI < 1.32 and COV * COV < 0.49 then 'Smooth'
when ADI >= 1.32 and COV * COV < 0.49 then 'Intermittent'
when ADI < 1.32 and COV * COV >= 0.49 then 'Erratic'
when ADI >= 1.32 and COV * COV <= 0.49 then 'Lumpy'
else 'No Quantity Measured'
end
from data;
这提供了一个非常优雅的解决方案...
这就是我要找的,但是,现在我需要获取此数据并将其与原始 table DF_ALL_DEMAND_BY_ROW_V
合并。我希望它包含 DF_ALL_DEMAND_BY_ROW_V
中存在的所有内容,但将上面的数据加入 CP_REF
,我认为这是一个 LEFT JOIN
,但我无法弄清楚。
不确定这是否是您的意思,但是为什么不创建第二个 CTE 并加入其中,例如
with data as (
select
...
), Q2 as (
select
CP_REF, ADI,
case
when ADI < 1.32 and COV * COV < 0.49 then 'Smooth'
when ADI >= 1.32 and COV * COV < 0.49 then 'Intermittent'
when ADI < 1.32 and COV * COV >= 0.49 then 'Erratic'
when ADI >= 1.32 and COV * COV <= 0.49 then 'Lumpy'
else 'Smooth'
end as StatusOrWhatever
from data
)
select stuff
from DF_ALL_DEMAND_BY_ROW_V v
left join Q2 on D2.CF_REF = v.CP_REF