Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在单个stmt中写入以获得预期结果_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql 如何在单个stmt中写入以获得预期结果

Sql 如何在单个stmt中写入以获得预期结果,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,此查询: select audit_id, Count(TRAINING_RESULT_ID ) safe from TBL_T_AUDIT_DETAILS where TRAINING_RESULT_ID =1 group by audit_id order by audit_id select audit_id, Count(TRAINING_RESULT_ID ) unsafe from TBL_T_AUDIT_DETAILS where TRAINING_RESULT_ID =2 g

此查询:

select audit_id, Count(TRAINING_RESULT_ID ) safe
from TBL_T_AUDIT_DETAILS
where TRAINING_RESULT_ID =1 
group by audit_id
order by audit_id
select audit_id, Count(TRAINING_RESULT_ID ) unsafe
from TBL_T_AUDIT_DETAILS
where TRAINING_RESULT_ID =2
group by audit_id
order by audit_id
select audit_id, Count(TRAINING_RESULT_ID ) na
from TBL_T_AUDIT_DETAILS
where TRAINING_RESULT_ID =3
group by audit_id 
order by audit_id
给我这些结果:

audit_id  safe
117       5
118       3
123       5
130       7
131       39
136       38
audit_id    unsafe
117           2
118           3
123           2
131           2
136           3
141           3
148           3
151           4
157           4
audit_id  na
118      1
147      124
148      8
157      116
218      79
226      47
此查询:

select audit_id, Count(TRAINING_RESULT_ID ) safe
from TBL_T_AUDIT_DETAILS
where TRAINING_RESULT_ID =1 
group by audit_id
order by audit_id
select audit_id, Count(TRAINING_RESULT_ID ) unsafe
from TBL_T_AUDIT_DETAILS
where TRAINING_RESULT_ID =2
group by audit_id
order by audit_id
select audit_id, Count(TRAINING_RESULT_ID ) na
from TBL_T_AUDIT_DETAILS
where TRAINING_RESULT_ID =3
group by audit_id 
order by audit_id
给我这些结果:

audit_id  safe
117       5
118       3
123       5
130       7
131       39
136       38
audit_id    unsafe
117           2
118           3
123           2
131           2
136           3
141           3
148           3
151           4
157           4
audit_id  na
118      1
147      124
148      8
157      116
218      79
226      47
最后,此查询:

select audit_id, Count(TRAINING_RESULT_ID ) safe
from TBL_T_AUDIT_DETAILS
where TRAINING_RESULT_ID =1 
group by audit_id
order by audit_id
select audit_id, Count(TRAINING_RESULT_ID ) unsafe
from TBL_T_AUDIT_DETAILS
where TRAINING_RESULT_ID =2
group by audit_id
order by audit_id
select audit_id, Count(TRAINING_RESULT_ID ) na
from TBL_T_AUDIT_DETAILS
where TRAINING_RESULT_ID =3
group by audit_id 
order by audit_id
给我这些结果:

audit_id  safe
117       5
118       3
123       5
130       7
131       39
136       38
audit_id    unsafe
117           2
118           3
123           2
131           2
136           3
141           3
148           3
151           4
157           4
audit_id  na
118      1
147      124
148      8
157      116
218      79
226      47
如何获得以下结果

audit_id  na  unsafe  safe
117       0    0       2
118       1    3       3
147       124  0       0
148       8    0       0 
157       116  0       0
218       79   0       0
226       47   0       0
141       0    3       0
148       0    3       0
123       0    0       2
131       0    0       2
136       0    0       3
还包括带有COUNT()函数的选项

还包括带有COUNT()函数的选项


谢谢你能帮我计算%的安全系数吗?这必须是一个围绕在我答案(或@AlexanderFedornko的答案)中的外部查询,这同样有效。您只能计算总计的百分比,不能计算单个行的百分比。此外,由于
na
状态,您必须防止被零除。类似于
SELECT safe/(safe+unsafe)FROM(谢谢你能帮我计算safe%的值吗?它必须是一个外部查询,围绕在我答案中的一个(或者@AlexanderFedornko的答案中,这同样有效)。您只能计算总计的百分比,不能计算单个行的百分比。此外,您还必须防止由于
na
状态而被零除。类似于
从中选择安全/(安全+不安全)(