SQL 查询..使用 DISTINCT 和 SUM 对 AVG 和 MEDIAN 有一点帮助
我有一个查询来获取不同用户的手机使用总持续时间...
但我需要能够计算出他们的使用情况的不同平均值..问题是某些用户共享手机,而我只能获取手机信息,所以通话持续时间会重复,这会使数据出现偏差。
所以我需要一个平均值和一个不同的值(在 pin.Number 字段上)...如果可能的话,做一个中位数也很有用......?
这是当前的查询...
SELECT TOP 40 SUM(Duration) AS TotalDuration, c.Caller, oin.Name, oin.Email, pin.Number, oin.PRN
FROM Calls as c
INNER JOIN Phones as pin On c.caller = pin.id
INNER JOIN officers as oin On pin.id = oin.fk_phones
WHERE Duration <> 0 AND Placed BETWEEN '01/07/2011 00:00:00' AND '20/08/2011 23:59:59'
GROUP BY c.Caller, oin.Name, pin.Number, oin.Email, oin.PRN
ORDER BY TotalDuration DESC
非常感谢您的任何指示
这是我所追求的当前数据的示例(但我在下面添加了我所追求的平均值),因为您可以看到一些用户共享同一部手机,但是他们之间共享秒数,因此不希望其影响平均值(我不希望重复 11113 秒),因此每个电话号码需要有一个不同的..
I have a query to get the total duration of phone usage for various users...
But I need to be able to work out distinct averages for their usage.. the problem being certain users share phones and I can only grab phone info, so the call duration is repeated and this would skew the data..
So I need an average and a distinct (on the pin.Number field)... it would also be useful to do a Median if that is possible..??
This is the current query...
SELECT TOP 40 SUM(Duration) AS TotalDuration, c.Caller, oin.Name, oin.Email, pin.Number, oin.PRN
FROM Calls as c
INNER JOIN Phones as pin On c.caller = pin.id
INNER JOIN officers as oin On pin.id = oin.fk_phones
WHERE Duration <> 0 AND Placed BETWEEN '01/07/2011 00:00:00' AND '20/08/2011 23:59:59'
GROUP BY c.Caller, oin.Name, pin.Number, oin.Email, oin.PRN
ORDER BY TotalDuration DESC
Many thanks for any pointers
Here's an example of the current data I am after (but I have added the averages below which is what I am after), as you can see some users share the same phone but the number of seconds is shared between them so don't want that to influence the average (I don't want 11113 seconds repeated), so there needs to be a distinct on each phone number..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是实现以下想法的解决方案:
获取 每部手机总计 (
SUM
(Duration)
)。排名总持续时间值的结果集 (
ROW_NUMBER
OVER (ORDER BY SUM(Duration))
)。再添加一列作为总行数 (
COUNT(*)
OVER ()
)。从结果集中获取平均值 (
AVG
(TotalDuration)
)。获取排名为两个值之间的平均值作为中位数
1)
N div 2 + 1
,2)
N div 2 + N mod 2
,其中
N
是项目数,div
是整数除法运算符,mod
是模运算符。我的测试表:
查询:
输出:
注意:在 Transact-SQL 中,
/
表示如果两个操作数都是整数,则进行整数除法。 T-SQL 中的模运算符为 <代码>%。Here's a solution that implements the following idea:
Get totals per phone (
SUM
(Duration)
).Rank the resulting set by the total duration values (
ROW_NUMBER
OVER (ORDER BY SUM(Duration))
).Include one more column for the total number of rows (
COUNT(*)
OVER ()
).From the resulting set, get the average (
AVG
(TotalDuration)
).Get the median as the average between two values whose rankings are
1)
N div 2 + 1
,2)
N div 2 + N mod 2
,where
N
is the number of items,div
is the integer division operator, andmod
is the modulo operator.My testing table:
The query:
The output:
Note: In Transact-SQL,
/
stands for integer division if both operands are integer. The modulo operator in T-SQL is%
.我希望你可以使用这个,我用临时表
Sql 来获得中位数和平均值
在这里尝试: https://data.stackexchange.com/stackoverflow/q/108612/
Sql 要获取总持续时间,
请在此处尝试:https://data.stackexchange.com/stackoverflow/q/108611/
I hope you can use this, I did it with temporary tables
Sql to get median and average
Try here: https://data.stackexchange.com/stackoverflow/q/108612/
Sql To get the Total durations
Try here: https://data.stackexchange.com/stackoverflow/q/108611/