有没有更优雅的方法使用 ruby​​-dbi 和连接到数据库来计算一组数字的中位数

发布于 2024-11-28 06:23:10 字数 1479 浏览 1 评论 0原文

我有以下代码,它计算从数据库中提取的一组数字的中位数。问题涉及是否有更简单的方法来计算 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东风软 2024-12-05 06:23:10

Intersystems Caché 有一个 MEDIAN 函数:

MEDIAN< /a>

该页面上的示例之一:

SELECT MEDIAN(birthd.decade.MEMBERS, MEASURES.[%COUNT]) ON 0 FROM patients

Intersystems Caché has a MEDIAN function:

MEDIAN

One of the examples on that page:

SELECT MEDIAN(birthd.decade.MEMBERS, MEASURES.[%COUNT]) ON 0 FROM patients
再见回来 2024-12-05 06:23:10

数组中的更新

错误是由于 ActiveRecord 集中的 nil 元素造成的,

   <%if !statistic.HrsIn.nil? %>
   ['<%= statistic.Specimen_Number %>', <%= statistic.HrsIn %>],
   <% end %> 

仅在系统间缓存深度查看中通过 Median 函数修复,而不是常规 SQL

Update

Error in array was due to nil elements in ActiveRecord set, fixed by

   <%if !statistic.HrsIn.nil? %>
   ['<%= statistic.Specimen_Number %>', <%= statistic.HrsIn %>],
   <% end %> 

Median function only in intersystems cache deep see and not regular SQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文