首页 \ 问答 \ SQL查询,用于计算一列中值的总发生次数和另一列中的相对发生次数(SQL query to count total ocurrences of values in one column and relative occurrence in another column)

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 withSELECT 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 with SELECT 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
更新时间:2024-03-02 08:03

最满意答案

规则说,类型和内部递归引用之间必须有一个构造函数。 所以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不具有透明性,完全如您所解释的那样。 也许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 ...
  • 您可以在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 ...
  • 它代表一种对象类型。 对于此示例,它是具有名为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.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)