对于SQL Server 2017,我创建了一个名为Challenger的表
drop table if exists challenger
create table challenger (O_Ring_Failure char(1),
Launch_temperature float,
Leak_check_pressure char(10))
select * from challenger
insert into challenger values ('N',66,'Low')
insert into challenger values ('N',69,'Low')
insert into challenger values ('N',68,'Low')
insert into challenger values ('N',67,'Low')
insert into challenger values ('N',72,'Low')
insert into challenger values ('N',73,'Low')
insert into challenger values ('N',70,'Low')
insert into challenger values ('N',78,'High')
insert into challenger values ('N',67,'High')
insert into challenger values ('N',67,'High')
insert into challenger values ('N',75,'High')
insert into challenger values ('N',70,'High')
insert into challenger values ('N',81,'High')
insert into challenger values ('N',76,'High')
insert into challenger values ('N',79,'High')
insert into challenger values ('N',75,'High')
insert into challenger values ('N',76,'High')
insert into challenger values ('Y',70,'Low')
insert into challenger values ('Y',57,'High')
insert into challenger values ('Y',63,'High')
insert into challenger values ('Y',70,'High')
insert into challenger values ('Y',53,'High')
insert into challenger values ('Y',58,'High')
我想通过O_Ring_Failure从 Launch_temperature 列中提取,以创建另外两列Y_temperature和N_temperature(即,那些O_Ring_Failure ='Y'和'N'的温度)
我写的是以下内容:
alter table challenger add
Y_temperature float,
N_temperature float;
go
with cte1
as
( select Launch_temperature as y_temp from challenger where O_Ring_Failure='Y'),
cte2 as(select Launch_temperature as n_temp from challenger where O_Ring_Failure='N')
insert into challenger
select y_temp, n_temp from cte1, cte2;
go
select * from challenger
我想将此表和两列传递给我创建的用于计算Z分数的存储过程,因此我不需要这些NULL开头和Y_temperature或N_temperature的重复项。有什么办法摆脱它们?
存储过程如下:
create procedure usp_bivariate
@tbl varchar(200),
@target_colname varchar(100),
@predictor_colname varchar(100)
as
begin
declare @sql varchar(max)
set @sql='with cte1(mean1,mean2, var1, var2, count1, count2) as ( select avg('+@target_colname+') as mean1, var('+@target_colname
+') as var1,count('+@target_colname+') as count1, avg('+@predictor_colname+') as mean2, var('+@predictor_colname
+') as var2,count('+@predictor_colname+') as count2 from '+@tbl+') select (mean1-mean2)/sqrt(var1/count1+var2/count2) from cte1'
exec(@sql)
end
答案 0 :(得分:0)
您要每个温度个数吗?
select launch_temperature,
sum(case when O_Ring_Failure = 'N' then 1 else 0 end) as num_notfailures,
sum(case when O_Ring_Failure = 'Y' then 1 else 0 end) as num_failures
from challenger
group by launch_temperature;
我想不出任何其他有用的方式来满足您的分析目标。