gpt4 book ai didi

sql - 使用先前在同一 SQL 语句中定义的列短名称

转载 作者:行者123 更新时间:2023-12-03 04:21:05 24 4
gpt4 key购买 nike

我正在从包含预期结果和实际结果值的表格中进行一些统计。

这个语句非常有效:

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

区别只是,我想通过使用之前定义的列名称来缩短表达式 - 已测试确定

可能吗?

附注:如果它有任何重要性的话,表格是:

CREATE TABLE Results(
id INTEGER PRIMARY KEY AUTOINCREMENT,
expected_result TEXT,
image_id TEXT,
model_ref TEXT,
predicted_result TEXT,
test_date TEXT)

最佳答案

在大多数数据库中,别名不能在定义的查询的同一“级别”(或范围)上使用(因为在解析查询时可能无法解析别名) 。您可以做的是将查询包装在外部查询中,该查询可以访问别名,如下所示:

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

关于sql - 使用先前在同一 SQL 语句中定义的列短名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28764070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com