[英]SQL: Select statement to return ratio of 2 column values
我有一個電子測試數據表,其中包含給定電壓下成對組件的通過/失敗結果。 它看起來像這樣:
Component_A|Component_B|Voltage|Result
1 2 1.0 Pass
1 2 1.0 Pass
1 2 1.0 Fail
1 2 1.0 Fail
1 2 2.0 Pass
1 2 2.0 Pass
1 2 2.0 Pass
1 2 2.0 Pass
3 4 1.0 Pass
3 4 1.0 Pass
3 4 1.0 Pass
3 4 1.0 Pass
3 4 2.0 Pass
3 4 2.0 Pass
3 4 2.0 Pass
3 4 2.0 Pass
對於每個Component_A
, Component_B
, Voltage
,我想顯示對於給定的故障率Component_A/Component_B/Voltage
組合以及用於該組合的失敗比率Component_A/Component_B
。 預期輸出如下:
Component_A|Component_B|Voltage|Voltage_Fail_Ratio|Component_Fail_Ratio
1 2 1.0 2/4(0.5) 2/8(0.25)
1 2 2.0 0/4(0) 0/8(0)
3 4 1.0 4/4(0) 0/8(0)
3 4 2.0 0/4(0) 0/8(0)
有一些選項會很棒(使用聯合或子查詢……選項越多越好)。 我已經嘗試編寫一個子查詢來至少獲得電壓故障率,但它沒有報告沒有故障的組件/電壓組合,我不確定如何獲得組件故障率:
SELECT f.component_a,f.component_b,f.voltage,f.failcount,p.passcount, f.failcount/(f.failcount+p.passcount) FROM
((SELECT component_a,component_b,voltage, count(*) as failcount from `tests` where
result='FAIL'
GROUP BY component_a,component_b,voltage) f INNER JOIN
(SELECT component_a,component_b,voltage, count(*) as passcount from `tests` where
result='PASS'
GROUP BY component_a,component_b,voltage) p on p.component_a=f.component_a and p.component_b=f.component_b and
p.voltage=f.voltage);
我認為這就是您想要的 - 它在兩個單獨的子查詢中計算Voltage_Fail_Ratio
和Component_Fail_Ratio
,然后在匹配的component_a
和component_b
值上連接:
SELECT t1.component_a, t1.component_b, t1.Voltage, t1.Voltage_Fail_Ratio, Component_Fail_Ratio
FROM (
SELECT component_a, component_b, Voltage,
SUM(result = 'FAIL') / COUNT(*) AS Voltage_Fail_Ratio
FROM tests
GROUP BY component_a, component_b, Voltage
) t1
JOIN (
SELECT component_a, component_b,
SUM(result = 'FAIL') / COUNT(*) AS Component_Fail_Ratio
FROM tests
GROUP BY component_a, component_b
) t2 ON t2.component_a = t1.component_a AND t2.component_b = t1.component_b
;
輸出:
component_a component_b Voltage Voltage_Fail_Ratio Component_Fail_Ratio
1 2 1 0.5 0.25
1 2 2 0 0.25
3 4 1 0 0
3 4 2 0 0
這在某種程度上與@Nick 的類似,但使用 WITH 的視圖。 只是為了給你更多的選擇。
WITH for_volt_fail_ratio AS(
SELECT com_a, com_b, volt, SUM(result = 'FAIL') / COUNT(*) AS Voltage_Fail_Ratio
FROM tests
GROUP BY com_a, com_b, volt),
for_com_fail_ratio AS(
SELECT com_a, com_b,SUM(result = 'FAIL') / COUNT(*) AS Component_Fail_Ratio
FROM tests
GROUP BY com_a, com_b),
sh AS(
SELECT tb1.com_a,tb1.com_b,tb1.volt,tb1.Voltage_Fail_Ratio,tb2.Component_Fail_Ratio
FROM for_volt_fail_ratio tb1 INNER JOIN for_com_fail_ratio tb2 ON (tb1.com_a=tb2.com_a AND tb1.com_b=tb2.com_b)
)
SELECT * FROM sh
最簡單的方法是條件聚合:
select component_a, component_b, voltage,
sum(case when result = 'PASS' then 1 else 0 end) as num_pass,
sum(case when result = 'FAIL' then 1 else 0 end) as num_fail,
avg(case when result = 'FAIL' then 1.0 else 0 end) as fail_rate
from tests
group by component_a, component_b, voltage;
如果您確實在使用 MySQL(正如 SQL Fiddle 建議的那樣),您可以將其簡化為:
select component_a, component_b, voltage,
sum(result = 'PASS') as num_pass,
sum(result = 'FAIL') as num_fail,
avg(result = 'FAIL') as fail_rate
from tests
group by component_a, component_b, voltage
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.