SQL查询,用于计算一列中值的总发生次数和另一列中的相对发生次数(SQL query to count total ocurrences of values in one column and relative occurrence in another column)
这是我的第一篇文章,因此对格式/内容的任何一般性更正也是受欢迎的。 我对SQL比较陌生。
假设我有一个数据库,它从分类评估中收集测试结果。 我知道每个测试的预期结果是什么。 我还有一个列,指示测试是否成功,即返回的预期值与预期值匹配。 它看起来像这样:
Expected_Result Result Success A A True A B False B B True A A True B A False
我知道我可以使用
SELECT Expected_Result, COUNT(Expected_Result) FROM Evaluation_Results GROUP BY Expected_Result
返回每个预期类型的总出现次数。 我知道如何使用SELECT COUNT(*) FROM Evaluation_Results WHERE Success = 'True' AND Expected_Result = 'A'
特定预期结果的错误检测次数我在努力的地方就是把两者结合起来。 我希望查询返回所有不同预期结果的列表,每个结果的总数,成功结果的数量以及总数的百分比,如下所示:
Expected_Result Total Num_Successful Success_Rate A 3 2 66.67 B 2 1 50.00
This is my first post, so any general corrections to format/content are also welcome. I'm relatively new to SQL.
Say I have a database which collects test results from an classification evaluation. I know what the expected outcome is for each test. I also have a column indicating whether the test was successful, ie the expected value returned matched the expected value. It looks something like this:
Expected_Result Result Success A A True A B False B B True A A True B A False
I know I can return the total occurrences of each expected type with
SELECT Expected_Result, COUNT(Expected_Result) FROM Evaluation_Results GROUP BY Expected_Result
. I know how to count the number of false detections for a specific expected outcome withSELECT COUNT(*) FROM Evaluation_Results WHERE Success = 'True' AND Expected_Result = 'A'
Where I'm struggling is combining the two. I would like the query to return a list of all distinct expected results, the total of each, the count of successful results, and the percentage of the total, like so:
Expected_Result Total Num_Successful Success_Rate A 3 2 66.67 B 2 1 50.00
原文:https://stackoverflow.com/questions/38199178
最满意答案
规则说,类型和内部递归引用之间必须有一个构造函数。 所以
Node
和{ value : ... }
记录类型是构造函数。如果您打开了
-rectypes
类型,则可以定义所有这些类型。 类型检查没有问题(与您所说的相反)。$ rlwrap ocaml -rectypes OCaml version 4.03.0 # type t = int * t;; type t = int * t # type t = int * (unit -> t);; type t = int * (unit -> t) # type t = int * (unit -> t) list;; type t = int * (unit -> t) list
The rule says that there must be a constructor between the type and the internal recursive reference. So
Node
and the{ value : ... }
record types are constructors.You can define all these types if you turn on
-rectypes
. There is no problem in type checking (contrary to what you say).$ rlwrap ocaml -rectypes OCaml version 4.03.0 # type t = int * t;; type t = int * t # type t = int * (unit -> t);; type t = int * (unit -> t) # type t = int * (unit -> t) list;; type t = int * (unit -> t) list
相关问答
更多-
camlp4o提供与vanilla OCaml语法相同的语法,但稍有不兼容性: parser是camlp4o一个特殊关键字。 这就是为什么parser在使用camlp4o语法时被拒绝的原因。 您可以通过重命名parser或使用js_of_ocaml而不是CamlP4的PPX语法扩展来解决此问题。 请查看https://ocsigen.org/js_of_ocaml/api/Ppx_js了解js_of_ocaml的这个新的语法助手的js_of_ocaml 。 camlp4o provides the same ...
-
OCaml中的歧视联盟就像一个C联盟和一个枚举。 所以你的一个number类型的例子可以像这样在C中表示: enum number_tag { ZERO, INTEGER, REAL }; union number_value { int i; /* Only access this if tag is INTEGER */ float f; /* Only access this if tag is REAL */ }; struct number { enum num ...
-
在类似ML的语言中,递归类型的定义是递归不通过变体类型的定义。 这是一个实用的定义,因为它倾向于导致更有用的类型检查。 递归类型没有任何棘手的问题。 您可以使用-rectypes标志在OCaml中启用对递归类型的支持。 $ ocaml -rectypes OCaml version 4.02.1 # type tree = int * int * tree list;; type tree = int * int * tree list # let x: tree = (3, 3, [(4 ...
-
因此,在类型或类定义中使用的constraint关键字允许将适用类型的“范围”缩小为类型参数,可以这么说。文档清楚地表明约束方程两侧的类型表达式将统一为“细化”约束所涉及的类型,因为它们是类型表达式,所以可以使用所有常用的类型级别操作符。 例子: # type 'a t = int * 'a constraint 'a * int = float * int;; type 'a t = int * 'a constraint 'a = float # type ('a,'b) t = 'c r const ...
-
由于OP具有C ++语言的经验,我认为以下解释可能有用。 表单的类型声明: type 'a t 接近C ++ template
class t; 例如, 'a list是通用列表, 'a是元素的类型。 为简明起见,我们使用单个'而不是template 构造。 在OCaml的说法中,我们使用术语“参数多态”,而不是“泛型编程”。 而不是单词模板,我们说一个类型构造函数。 后者有一个有趣的结果。 与在C ++中一样,模板实例化创建类型的新实例,在OCaml中 ... -
规则说,类型和内部递归引用之间必须有一个构造函数。 所以Node和{ value : ... }记录类型是构造函数。 如果您打开了-rectypes类型,则可以定义所有这些类型。 类型检查没有问题(与您所说的相反)。 $ rlwrap ocaml -rectypes OCaml version 4.03.0 # type t = int * t;; type t = int * t # type t = int * (unit -> t);; type t = int * (unit -> ...
-
OCaml中的参考透明度(Referential transparency in OCaml)[2021-09-02]
Ocaml不具有透明性,完全如您所解释的那样。 也许Matuszek想要强调Ocaml的功能性方面,但在我看来他是误导或错误的。 例如, 表达式(但不是语句)例如说OCaml is a purely functional language , OCaml claims to be stateless 。 Section Omissions说 循环也被省略了,但它们在纯粹的功能语言中并不是非常有用,无论如何。 这很有趣,因为如果没有用,循环就不会被添加到Ocaml中。 Ocaml isn't referent ... -
其新的“可扩展变体类型”。 有关详细信息,请参阅http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec251 。 您可以使用声明一个空的可扩展变体类型 type t = .. 那么稍后您可以添加新的构造函数 type t += Foo | Bar of int 您可以将构造函数添加到多个程序的可扩展变体中。 相反,您无法在模式匹配中享受非详尽性检查,这可用于普通变体。 type event = t = ..声明已存在的可扩展类型t的别名event ...
-
OCaml联合类型(OCaml union types)[2023-05-30]
您可以在fun使用模式匹配: # (fun (Int a) -> a > 0) (Int 1);; Warning 8: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: Fraction _ - : bool = true 如果传入Fraction显然会引发匹配错误。 如果你想处理这种情况: # (function Int a -> a > 0 | Fraction {n ... -
围绕OCaml类型定义的'<'和'>'是什么意思?(What's the meaning of '<' and '>' surrounding an OCaml type definition?)[2021-10-06]
它代表一种对象类型。 对于此示例,它是具有名为run和block方法的对象类型,而没有其他方法。 It represents an object type. For this example it's the type of objects with methods named run and block, and no other methods.