我想做一个平均值:问题是对于 1 个项目,我正在计算每个元素的 AVG(有效),但是一旦我想要类别平均值的全局平均值(something 和 foo),它就不起作用(mysql 向我抛出一个错误:请参阅下面我使用的语法)。
我需要这样做,因为我想按全球平均值对结果进行排序
SELECT AVG(AVG(category1)+AVG(category2)) /2 as moy
.....
ORDER BY moy DESC
Run Code Online (Sandbox Code Playgroud)
谢谢,
编辑:我想要每个类别的平均值编辑2:
得到表:服务器(...)得到表:answer_poll(价格、接口、服务、质量)
一个用户有 1 个服务器,他可以多次回答该服务器的民意调查
SELECT s.name , s.type , COUNT(s.GSP_nom) as nb_votes,
TRUNCATE(AVG(quality), 2) as quality, TRUNCATE(AVG(price), 2) as price,
TRUNCATE(AVG(interface), 2) as interface, TRUNCATE(AVG(services), 2) as services
FROM answer_poll AS v
INNER JOIN server AS s ON v.idServ = s.idServ
GROUP BY s.name
ORDER BY global average :d
Run Code Online (Sandbox Code Playgroud)
这个请求=每个类别的平均值,但我想要平均值的平均值:p
我创建了两个视图来帮助计算 user_diary_number,然后选择日记数 > 用户总 user_diary_number 平均值的用户。
两个视图如下:
create view user_diary_number as
(
select user_id,count( distinct diary_id ) as diary_num
from user_diary
group by user_id
);
Run Code Online (Sandbox Code Playgroud)
第二次使用havingand avg:
create view hw_diary as
(
select u.user_id, u.realname, ud.diary_num, school.school_name
from (user as u cross join user_diary_number as ud on u.user_id = ud.user_id )cross join school on u.school_id = school.school_id
having diary_num > avg(diary_num)
);
Run Code Online (Sandbox Code Playgroud)
现在的问题是什么,第二个视图只有 1 行结果。当然,我们有超过 1 个用户的日记数量 > 平均 diary_num。事实上,我总共有 251 篇日记和 103 个用户。一些用户有9、4、5本日记。但结果只有 1 个用户拥有 3 …
我有一个查询,其中包含我想要显示的正确条件和字段:
SELECT t.business_process_id,
COUNT (tsp.status) AS COUNT,
ROUND (AVG (tsp.end_date - tsp.start_date), 2) * 24 * 60 AS average,
ROUND (MAX (tsp.end_date - tsp.start_date), 2) * 24 * 60 AS MAX,
ROUND (MIN (tsp.end_date - tsp.start_date), 2) * 24 * 60 AS MIN,
ROUND (MEDIAN (tsp.end_date - tsp.start_date), 2) * 24 * 60 AS MEDIAN,
ROUND (STDDEV (tsp.end_date - tsp.start_date), 2) AS std_deviation
FROM transaction_status_period tsp, transaction t
WHERE t.trans_id = tsp.trans_id
AND tsp.status = 'R'
AND tsp.end_date IS NOT …Run Code Online (Sandbox Code Playgroud) 我有一个矩阵,看起来有点像这样:
Date Time Data
15000 04/09/2014 05:45:00 0.908
15001 04/09/2014 06:00:00 0.888
15002 04/09/2014 06:15:00 0.976
15003 04/09/2014 06:30:00 1.632
15004 04/09/2014 06:45:00 1.648
15005 04/09/2014 07:00:00 1.164
15006 04/09/2014 07:15:00 0.568
15007 04/09/2014 07:30:00 1.020
15008 04/09/2014 07:45:00 1.052
15009 04/09/2014 08:00:00 0.920
15010 04/09/2014 08:15:00 0.656
15011 04/09/2014 08:30:00 1.172
15012 04/09/2014 08:45:00 1.000
15013 04/09/2014 09:00:00 1.420
15014 04/09/2014 09:15:00 0.936
15015 04/09/2014 09:30:00 0.996
15016 04/09/2014 09:45:00 1.100
15017 04/09/2014 10:00:00 0.492
Run Code Online (Sandbox Code Playgroud)
它包含一年的数据,每天有 96 行(从 00:00 …
我想获取 Postgres 中两个日期之间的平均日期:
在 SqlServer 中通过这个简单的查询完成:
SELECT DateAdd(ms, DateDiff(ms, date_begin, date_end)/2, date_begin)
Run Code Online (Sandbox Code Playgroud)
例如:
SELECT DateAdd(ms, DateDiff(ms, getdate(), dateadd(d, 1, getdate()))/2, getdate())
Run Code Online (Sandbox Code Playgroud)
现在我想在 postgres 中进行相同的查询。我尝试类似的东西
SELECT current_timestamp + ((SELECT ((DATE_PART('day', (current_timestamp + INTERVAL '1 day')::timestamp - current_timestamp::timestamp) * 24 +
DATE_PART('hour', (current_timestamp + INTERVAL '1 day')::timestamp - current_timestamp::timestamp)) * 60 +
DATE_PART('minute', (current_timestamp + INTERVAL '1 day')::timestamp - current_timestamp::timestamp)) * 60 +
DATE_PART('second', (current_timestamp + INTERVAL '1 day')::timestamp - current_timestamp::timestamp)||'second')::interval
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
在 Postgres 中是否有更简单的方法来完成这个简单的操作?
我是 BigQuery 的新手,所以我正在努力以一种可能有用的方式对我的数据进行分组。此时,我已将 unix 时间戳转换为日期(例如 2018-08-27 04:54:56 UTC 等...)。我每 10 分钟左右阅读一次,并且只想按日期对它们进行分组,然后对我的各个专栏取每日平均值。我认为 'group by' 然后是 'avg' 可能是前进的方向,但我不确定如何实施第一部分。到目前为止,我用来创建表的代码如下:
SELECT TIMESTAMP_SECONDS(timestamp) as timestamp,humidity,co2,temperature,app_id
FROM data.staging
where timestamp is not null
;
Run Code Online (Sandbox Code Playgroud)
这是我的输出的几行:
|| Timestamp || humidity || co2 || temperature || app id||
||========================||===========||=======||==============||========||
||2018-08-31 13:35:50 UTC || 63.4 || 634.5 || 21.2 || office_||
||2018-08-31 14:37:20 UTC || 67.8 || 600.4 || 20.8 || office_||
Run Code Online (Sandbox Code Playgroud)
等等..
抱歉格式不正确,如果还有其他需要,我很乐意提供。谢谢你的帮助!
所以假设我有一个简单的对象:
public class Items {
private static int
numItems,
itemsInStock,
itemsSold,
//...
}
Run Code Online (Sandbox Code Playgroud)
在代码的其他地方我有一个 ArrayList<Items>
如果有一种简单的方法可以获得 numItems / ItemsInStock 等的平均值/中位数,而无需在对象的每个字段上执行 for/each?
当前代码如下:
for (Items item: allItems) {
if (item.field != null) {
temp += item.field;
}
}
fieldAvg = temp / allItems.size();
Run Code Online (Sandbox Code Playgroud) 我想创建一个接收任意数量参数并根据数量取平均值的函数。论据。所以我尝试了如下代码;
function takeAvg(...nums) {
return nums.reduce((total, curVal) => {
return (total + curVal) / nums.length;
});
}
console.log(takeAvg(10, 20)) // returns 15 - no problem
console.log(takeAvg(10, 20, 30)) // returns 13.333333333333334 weirdly.Run Code Online (Sandbox Code Playgroud)
我认为问题出在 nums.legth 上,但我不明白为什么?请帮忙。谢谢。
这是一个示例表 CALLRECORD:
+--------+------------+
|callid | rating |
|1 | |
|2 | 5 |
|3 | |
|4 | 1 |
|5 | |
+--------+------------+
Run Code Online (Sandbox Code Playgroud)
输出总通话数、评级通话数、平均评级和未评级通话数没有问题:
select count(*) as total from callrecord;
select count(*) as rated, avg(rating) as average_rating from callrecord where rating is not null;
select count(*) as unrated from callrecord where rating is null;
Run Code Online (Sandbox Code Playgroud)
+--------+
|total |
|5 |
+--------+
+--------+------------+
|rated |average |
|2 |3 |
+--------+------------+
+--------+
|unrated |
|3 |
+--------+
Run Code Online (Sandbox Code Playgroud)
我正在寻找如何使用单个 SQL 请求将以上所有内容输出到一行:
+--------+--------+------------+---------+ …Run Code Online (Sandbox Code Playgroud) 我尝试遵循 powershell-command,但随后打开了 1000 个窗口并且 powershell ISE 崩溃了。有没有办法在后台运行批处理文件 1000 次?有没有更聪明的方法来获得平均执行时间?
这是我试过的代码:
cd C:\scripts
Measure-Command {
for($i = 0;$i -lt 1000;$i++){
Start-Process -FilePath "C:\scripts\open.bat"
}
}
Run Code Online (Sandbox Code Playgroud) average ×10
sql ×3
mysql ×2
batch-file ×1
collections ×1
count ×1
datetime ×1
group-by ×1
having ×1
informix ×1
java ×1
javascript ×1
mapreduce ×1
oracle ×1
postgresql ×1
powershell ×1
r ×1
request ×1
sql-null ×1
subset ×1
timestamp ×1