首页 \ 问答 \ 用MySQL计算百分比(Calculate percentage with MySQL)

用MySQL计算百分比(Calculate percentage with MySQL)

我有以下表格结构

语言:

id  |  language
1   |  English
2   |  Dutch

翻译:

id | translation_id | translation | language_id
1  |                | Username    | 1
2  |                | Password    | 1
3  | 2              | Wachtwoord  | 2

我尝试了以下查询来获取每种语言的概述:

SELECT
  `languages`.`id`,
  `language`
  concat( round( ( COUNT(`translations`.`languages_id` = 1) / COUNT(`translations`.`languages_id` = 2)) * 100, 0), '%') AS `percentage`
FROM `languages`
JOIN `translations`
ON  `languages`.`id` = `translations`.`languages_id`

但出于某种原因,我只获得100%的百分比。 虽然我希望荷兰人获得50%的奖金。

我现在得到的结果就是

[id] => 1 [language] => English [percentage] => 100% 

虽然我希望得到:

[id] => 1 [language] => English [percentage] => 100%
[id] => 2 [language] => Dutch [percentage] => 50%

我知道我的英语没有翻译链接。

在这种情况下,英语是默认语言,因此100%的其他语言都是英语的一定比例。 这就是为什么我希望荷兰人达到50%。

如果不对每种语言进行相同的查询,也不可能获得每种语言的百分比吗? 就像只选择id和语言一样,你会得到2, Dutch1, English行返回。


I have the following table structures

Languages:

id  |  language
1   |  English
2   |  Dutch

Translations:

id | translation_id | translation | language_id
1  |                | Username    | 1
2  |                | Password    | 1
3  | 2              | Wachtwoord  | 2

I tried the following query to get an overview for each language:

SELECT
  `languages`.`id`,
  `language`
  concat( round( ( COUNT(`translations`.`languages_id` = 1) / COUNT(`translations`.`languages_id` = 2)) * 100, 0), '%') AS `percentage`
FROM `languages`
JOIN `translations`
ON  `languages`.`id` = `translations`.`languages_id`

But for some reason I'm only getting 100% for percentage. While I would expect to get 50% for Dutch.

The result I get right now is just

[id] => 1 [language] => English [percentage] => 100% 

While I would expect to get:

[id] => 1 [language] => English [percentage] => 100%
[id] => 2 [language] => Dutch [percentage] => 50%

I know I have no translations linked for English.

In this case English is the default language so that would be 100% every other language is a certain percentage of English. That's why I expect Dutch to be 50%.

Also wouldn't it be possible to get the percentage for every language without doing the same query for every language? Just like when only selecting the id and language you would get both 2, Dutch and 1, English rows returned.


原文:https://stackoverflow.com/questions/41572569
更新时间:2022-06-02 21:06

最满意答案

  1. 目前无法通过API实现这一目标。 唯一的解决方案是您自己管理此映射。 你可以使用一个数组。 此外,还有针对此功能的要求,因此您可以通过https://code.google.com/p/analytics-issues/issues/detail?id=305对其进行投票并跟踪状态更改

  2. 要识别价值,你所拥有的是接近正确的:

    • 查询ga:dimension3,然后根据维度的范围,对于指标,您可以使用ga:网页浏览(如果自定义维度的范围限定为点击并假设您在设置维度值后发送综合浏览量),则ga:visits(session)范围自定义维度)或ga:访问者(用户范围自定义维度)。
    • 使用start-index(如果需要)分析所有结果。
    • 获得所有结果后,您需要遍历每一行并确定为该维度设置的所有唯一值。 您可以使用简单的for循环和Object来存储字符串值。 例如,对于每一行,检查维度值是否为Object的成员,如果不是,则添加它。

  1. This is currently not possible through the API. The only solution would be for you to manage this mapping yourself. You could use an array for this. Also, there is a feature request for this so you can upvote it and follow any status changes at https://code.google.com/p/analytics-issues/issues/detail?id=305

  2. To identify values, what you have is close to correct:

    • Query for ga:dimension3 and then depending on the scope of the dimension, for a metric you can use ga:pageviews (if custom dimension is scoped to hits and assuming you are sending pageviews after setting the dimension value), ga:visits (session scoped custom dimension), or ga:visitors (user scoped custom dimension).
    • Paginate through all results using start-index (if required).
    • Once you have all results and you'll need to go through each row and identify all the unique values set for that dimension. You could use a simple for loop and an Object to store the string values. E.g. For each row, check if dimension value is a member of Object, if not then add it.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用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)