有没有更优雅的方法使用 ruby-dbi 和连接到数据库来计算一组数字的中位数
我有以下代码,它计算从数据库中提取的一组数字的中位数。问题涉及是否有更简单的方法来计算 DBI::ROW 数组中列的中位数?
作为中值计算中的旁注 array.sort 抛出并错误“未定义方法”
require 'dbi'
def median(array, already_sorted=false)
return nil if array.empty?
# array.sort
m_pos = array.size / 2
return array.size % 2 == 1 ? array[m_pos] : mean(array[m_pos-1..m_pos])
end
def mean(array)
array.inject(array.inject(0) { |sum, x| sum += x } / array.size.to_f)
end
begin
dbh = DBI.connect('DBI:jdbc:Cache://10.150.98.11:1972/SQLHRT', 'tpathhrt', 'tpathhrt',
'driver'=>'com.intersys.jdbc.CacheDriver')
sth = dbh.prepare("SELECT
DATEDIFF('hh',Result_Set.Date_Time_Booked_In,current_timestamp) AS HrsIn
FROM
iLabTP.Outstanding_Work_Index,
iLabTP.Result_Set Result_Set,
iLabTP.Request
WHERE
Outstanding_Work_Index.Request_Row_ID = Result_Set.Request_Row_ID and
Outstanding_Work_Index.Request_Row_ID = Request.Request_Row_ID and
Result_Set.Set_Code = 'TVITDN' order by HrsIn ASC")
sth.execute()
arr =[1]
sth.fetch do |row|
arr << row[0]
end
printf "Number of Records %d:\n", arr.size
printf "Median %s:\n", median(arr)
sth.finish
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
I have the following code which calculates the median of a group of numbers drawn from a database. The question relates to if there is an easier way for calculating the median of a column from a array of DBI::ROWs?
As an aside array.sort in the median calculation throws and error 'undefined method'
require 'dbi'
def median(array, already_sorted=false)
return nil if array.empty?
# array.sort
m_pos = array.size / 2
return array.size % 2 == 1 ? array[m_pos] : mean(array[m_pos-1..m_pos])
end
def mean(array)
array.inject(array.inject(0) { |sum, x| sum += x } / array.size.to_f)
end
begin
dbh = DBI.connect('DBI:jdbc:Cache://10.150.98.11:1972/SQLHRT', 'tpathhrt', 'tpathhrt',
'driver'=>'com.intersys.jdbc.CacheDriver')
sth = dbh.prepare("SELECT
DATEDIFF('hh',Result_Set.Date_Time_Booked_In,current_timestamp) AS HrsIn
FROM
iLabTP.Outstanding_Work_Index,
iLabTP.Result_Set Result_Set,
iLabTP.Request
WHERE
Outstanding_Work_Index.Request_Row_ID = Result_Set.Request_Row_ID and
Outstanding_Work_Index.Request_Row_ID = Request.Request_Row_ID and
Result_Set.Set_Code = 'TVITDN' order by HrsIn ASC")
sth.execute()
arr =[1]
sth.fetch do |row|
arr << row[0]
end
printf "Number of Records %d:\n", arr.size
printf "Median %s:\n", median(arr)
sth.finish
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Intersystems Caché 有一个 MEDIAN 函数:
MEDIAN< /a>
该页面上的示例之一:
Intersystems Caché has a MEDIAN function:
MEDIAN
One of the examples on that page:
数组中的更新
错误是由于 ActiveRecord 集中的 nil 元素造成的,
仅在系统间缓存深度查看中通过 Median 函数修复,而不是常规 SQL
Update
Error in array was due to nil elements in ActiveRecord set, fixed by
Median function only in intersystems cache deep see and not regular SQL