我从表中获得了一些具有预期和实际结果值的统计数据。
这句话完美无缺:
select model_ref AS [Model],
count(id) AS Tested,
count(case when expected_result = predicted_result then predicted_result end) AS OK,
count(case when NOT(expected_result = predicted_result) then expected_result end) AS Wrong,
( (count(case when expected_result = predicted_result then predicted_result end)*1.0) / count(id) * 100 ) AS [Accuracy (%)]
from Results
但这不是:
select model_ref AS [Model],
count(id) AS Tested,
count(case when expected_result = predicted_result then predicted_result end) AS OK,
count(case when NOT(expected_result = predicted_result) then expected_result end) AS Wrong,
( OK *1.0) / Tested * 100 ) AS [Accuracy (%)]
from Results
区别仅在于,我想通过使用先前定义的列名来缩短表达式 - 已测试和确定
有可能吗?
P.S .: 如果它具有任何重要性,则表格为:
CREATE TABLE Results(
id INTEGER PRIMARY KEY AUTOINCREMENT,
expected_result TEXT,
image_id TEXT,
model_ref TEXT,
predicted_result TEXT,
test_date TEXT)
答案 0 :(得分:2)
在大多数数据库中,别名不能在查询的同一“级别”(或范围)上使用(因为在解析查询时可能无法解析别名) 。你可以做的是将查询包装在一个外部查询中,该查询可以访问这样的别名:
select
Model,
Tested,
OK,
Wrong,
(( OK *1.0) / Tested * 100 ) AS [Accuracy (%)]
from (
select model_ref AS [Model],
count(id) AS Tested,
count(case when expected_result = predicted_result then predicted_result end) AS OK,
count(case when NOT(expected_result = predicted_result) then expected_result end) AS Wrong
from Results
group by model_ref
) a