计算Oracle表中特定值之间的模式的出现次数

时间:2017-09-25 07:04:16

标签: mysql oracle count

例如,在下表的帮助下,

    Dim WSNew As Worksheet
    Set WSNew = Worksheets.Add

    Dim rngVisible As Range
    Set rngVisible = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)

    If rngVisible.Rows.Count > 1 Or rngVisible.Areas.Count > 1 Then
        rngVisible.Copy
            With WSNew.Range("A1")
                .PasteSpecial Paste:=8
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
                .Select
            End With
    Else
        MsgBox ("No such filtered criteria")
    End If

我想计算每个Rn ID Pattern 1 C20 2 C21 3 C25 4 C25 5 C25 6 C25 7 C25 8 C21 9 C23 10 C20 11 C21 12 C25 13 C25 14 C25 15 C22 16 C21 17 C20 之间的C25模式。

结果应该是,

- > C20 b / n次C25 = 5的第1次和第2次计数 见下面的参考文献

C20

- > Rn ID Pattern 1 C20 2 C21 3 C25 4 C25 5 C25 6 C25 7 C25 8 C21 9 C23 10 C20 b / n次数C25 = 3

的第2次和第3次出现次数
C20

提前致谢。

1 个答案:

答案 0 :(得分:1)

使用IGNORE NULLS分析函数和COUNT选项查找范围开头的上一行,然后使用CREATE TABLE table_name ( rn, id ) AS SELECT 1, 'C20' FROM DUAL UNION ALL SELECT 2, 'C21' FROM DUAL UNION ALL SELECT 3, 'C25' FROM DUAL UNION ALL SELECT 4, 'C25' FROM DUAL UNION ALL SELECT 5, 'C25' FROM DUAL UNION ALL SELECT 6, 'C25' FROM DUAL UNION ALL SELECT 7, 'C25' FROM DUAL UNION ALL SELECT 8, 'C21' FROM DUAL UNION ALL SELECT 9, 'C23' FROM DUAL UNION ALL SELECT 10, 'C20' FROM DUAL UNION ALL SELECT 11, 'C21' FROM DUAL UNION ALL SELECT 12, 'C25' FROM DUAL UNION ALL SELECT 13, 'C25' FROM DUAL UNION ALL SELECT 14, 'C25' FROM DUAL UNION ALL SELECT 15, 'C22' FROM DUAL UNION ALL SELECT 16, 'C21' FROM DUAL UNION ALL SELECT 17, 'C20' FROM DUAL; 分析函数和范围窗口来计算:

SQL Fiddle

Oracle 11g R2架构设置

SELECT *
FROM   (
  SELECT t.*,
         COUNT( CASE id WHEN 'C25' THEN 1 END )
           OVER ( ORDER BY rn
                  ROWS BETWEEN (rn - prev_rn) PRECEDING AND CURRENT ROW )
           AS num_c25
  FROM   (
    SELECT t.*,
           LAG( CASE id WHEN 'C20' THEN rn END, 1, 1 )
             IGNORE NULLS OVER ( ORDER BY rn ) AS prev_rn
    FROM   table_name t
  ) t
)
WHERE  id = 'C20'

查询1

| RN |  ID | PREV_RN | NUM_C25 |
|----|-----|---------|---------|
|  1 | C20 |       1 |       0 |
| 10 | C20 |       1 |       5 |
| 17 | C20 |      10 |       3 |

<强> Results

SELECT *
FROM   (
  SELECT t.*,
         COUNT( CASE id WHEN 'C25' THEN 1 END ) OVER ( ORDER BY rn ROWS BETWEEN LEVEL - 1 PRECEDING AND CURRENT ROW ) AS num_c25,
         CONNECT_BY_ROOT rn AS start_rn,
         CONNECT_BY_ISLEAF AS isleaf
  FROM   table_name t
  START WITH id = 'C20'
  CONNECT BY PRIOR rn + 1 = rn
  AND   id <> 'C20'
)
WHERE isleaf = 1

另一种方法是使用分层查询:

查询2

| RN |  ID | NUM_C25 | START_RN | ISLEAF |
|----|-----|---------|----------|--------|
|  9 | C23 |       5 |        1 |      1 |
| 16 | C21 |       3 |       10 |      1 |
| 17 | C20 |       0 |       17 |      1 |

<强> Results

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "services" does not exist
LINE 1: SELECT "services".* FROM "services"
                                 ^
: SELECT "services".* FROM "services"
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `block in exec_no_cache'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_adapter.rb:590:in `block in log'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.6/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `exec_no_cache'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:585:in `execute_and_clear'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:103:in `exec_query'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:377:in `select_prepared'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:39:in `select_all'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/query_cache.rb:95:in `select_all'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/querying.rb:39:in `find_by_sql'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/relation.rb:706:in `exec_queries'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/relation.rb:583:in `load'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/relation.rb:260:in `records'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/relation/delegation.rb:38:in `each'
/Users/Naekh/code/yoando/statuschecker/statuschecker/lib/tasks/fb_ping.rake:5:in `block (2 levels) in <top (required)>'
/Users/Naekh/code/yoando/statuschecker/statuschecker/lib/tasks/rake_them_all.rake:4:in `block (2 levels) in <top (required)>'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
/Users/Naekh/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `eval'
/Users/Naekh/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `<main>'