用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, Dutch
和1, 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
and1, English
rows returned.
原文:https://stackoverflow.com/questions/41572569
最满意答案
目前无法通过API实现这一目标。 唯一的解决方案是您自己管理此映射。 你可以使用一个数组。 此外,还有针对此功能的要求,因此您可以通过https://code.google.com/p/analytics-issues/issues/detail?id=305对其进行投票并跟踪状态更改
要识别价值,你所拥有的是接近正确的:
- 查询ga:dimension3,然后根据维度的范围,对于指标,您可以使用ga:网页浏览(如果自定义维度的范围限定为点击并假设您在设置维度值后发送综合浏览量),则ga:visits(session)范围自定义维度)或ga:访问者(用户范围自定义维度)。
- 使用start-index(如果需要)分析所有结果。
- 获得所有结果后,您需要遍历每一行并确定为该维度设置的所有唯一值。 您可以使用简单的for循环和Object来存储字符串值。 例如,对于每一行,检查维度值是否为Object的成员,如果不是,则添加它。
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
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.
相关问答
更多-
未满24-48小时的数据可能尚未完成处理,因此我建议不要查看过去24小时内查看的数据,它不会是正确的。 其他明智的,你可以看到这一点。 假设你的意思是自定义维度是,你可以在同一请求中提取自定义维度和页面路径 。 您可能希望查看Dimensions&metrics资源管理器以及Google Analytics分析查询资源管理器,这两者都可以帮助您了解可以从API中提取哪些数据。 Data under 24-48 hours old has probably not completed processing s ...
-
差异可能是由于抽样造成的 。 您的API报告是否已被抽样? 如果您使用的是Google表格,它会在报告标题中告诉您。 如果差异是一个数量级,那么它可能是别的东西。 此外,您可能应该考虑使用高级细分而不是视图过滤来隔离视图中的特定登录页面。 但这取决于您的特定用例。 The discrepancy might be due to sampling. Is your API report sampled? If you're using Google Sheets, it will tell you so in ...
-
因此,实时API看起来不支持自定义维度 实时API支持的维度和指标列表。 https://developers.google.com/analytics/devguides/reporting/realtime/dimsmets/ So it looks like the real time API doesn't support custom dimensions List of supported dimensions and metrics below for the real time api. h ...
-
该名称仅用于报告界面。 在API中,您可以通过关键字“维度”跟随其各自的数字索引来处理自定义维度。 因此,在您的示例中,您将通过查询“dimension1”来检索“zcode”的值。 The name is used in the reporting interface only. In the API you address custom dimensions by the keyword "dimension" followed by their respective numeric index. So ...
-
目前无法通过API实现这一目标。 唯一的解决方案是您自己管理此映射。 你可以使用一个数组。 此外,还有针对此功能的要求,因此您可以通过https://code.google.com/p/analytics-issues/issues/detail?id=305对其进行投票并跟踪状态更改 要识别价值,你所拥有的是接近正确的: 查询ga:dimension3,然后根据维度的范围,对于指标,您可以使用ga:网页浏览(如果自定义维度的范围限定为点击并假设您在设置维度值后发送综合浏览量),则ga:visits(ses ...
-
Google Analytics报告Api - PHP:添加用户帐户(Google Analytics Reporting Api - PHP: Add user account)[2023-11-24]
首先,确保通过设置访问类型获取刷新令牌以使其脱机。 然后创建一个单独的页面进行报告。 对于所有报告,单独的页面应使用帐户ID和刷新令牌,如果它在令牌上给您一个错误,您可以自己刷新它,或将它们发送回登录页面。 您只需要申请一次凭证,即可访问所有帐户。 php指南只使用第一个帐户,但是你可以迭代所有这些帐户并获得更多 $client = new Google_Client(); $client->setAuthConfig(__DIR__ . '/creds/client_secrets.json'); $cl ... -
如何从Google Analytics报告API获取自定义维度?(How do you get custom dimensions from Google Analytics reporting API?)[2023-07-12]
只需要“ dimension1 ”或类似名称。 您可以在管理部分/ 自定义防御中的代码示例中获取它。 Google has added support for this. http://analytics.blogspot.com/2013/07/40-new-data-points-in-google-analytics.html -
您应该尝试使用dateOfSession<>段表示法 : segment=users::sequence::^ga:sessionCount==1;dateOfSession<>2014-05-20_2014-05-30;->>ga:sessionDurationBucket>600 上述细分将为您提供2014年5月20日至2014年5月30日期间第一次会话并在网站上花费超过600秒的用户。 2016年4月更新 截至2016年4月,随着Analytics Reporting API V4的发布,现在可以通 ...
-
发现得好。 我认为该教程中存在错误。 $client->setAuthConfig($KEY_FILE_LOCATION); $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); $analytics = new Google_Service_AnalyticsReporting($client); 尝试在代码中添加上面的中间行。 您需要设置一个工作范围。 让我知道它是否有效,我将通过电子邮件发送开发团队并更 ...
-
ga:productPrice通过Analytics Core Reporting API(v3)(ga:productPrice via the Analytics Core Reporting API (v3))[2022-04-15]
Google Analytics中没有名为ga:productPrice维度。 但是GA中还有其他指标,例如ga:transactionRevenue , ga:revenuePerItem , ga:itemRevenue以及其他与交易和收入相关的指标。 以下是电子商务相关维度和指标的列表 。 希望能帮助到你 ! There is no dimension called ga:productPrice in Google Analytics. But there are other metrics in ...