---
title: "oracle 统计sql"
author: "virusfu"
date: "2011-11-16T18:08:06Z"
tags: []
categories: ["数据库"]
slug: "oracle-sql"
draft: false
lastmod: "2012-06-29T15:25:55Z"
id: 1262520
url: "https://virusfu.iteye.com/blog/1262520"
---

oracle 统计月平均交易次数 ：

 

 

        select n\_tsc\_src\_usr\_id , floor(count(c\_tsc\_no)/trunc(months\_between(max(d\_tsc\_req\_time),min(d\_tsc\_req\_time))))

       from tbl\_tsc\_base

  group by n\_tsc\_src\_usr\_id

       having months\_between(max(d\_tsc\_req\_time),min(d\_tsc\_req\_time)) &gt;=1

 

 

oracle 日交易统计：

 

 

select *

  from (select rownum rn, r.*

          from (select to\_char(to\_date(substr(txn\_tm, 0, 8), 'yyyy-mm-dd'),

                               'yyyy-mm-dd') as txn\_date,

                       sum(txn\_amt) as txn\_amt\_sum,

                       count(1) as txn\_count,

                       -sum(MERC\_FEE\_AMT) txn\_fee\_sum

                  from HPSTJNL

                 where merc\_id = '800000370110002'

                   and txn\_cd = 2010020

                 group by substr(txn\_tm, 0, 8)

                 order by txn\_date desc) r)

 where rn &gt;= 1

   and rn &lt; 16

 

oracle 统计总行数：

 

 

select count(1)

  from (select count(1)

          from HPSTJNL

         where merc\_id = '800000370110002'

           and txn\_cd = 2010020

         group by substr(txn\_tm, 0, 8))

 

最近一个月的统计信息：

 and  to\_char(to\_date(txn\_tm,'yyyy-mm-dd hh24:mi:ss'), 'yyyymmdd') between to\_char(add\_months(sysdate,-1),'yyyymmdd') and to\_char(trunc(sysdate), 'yyyymmdd') 

 

上一个月的统计信息: txn\_tm字段格式：20120314102320

substr(txn\_tm,0,6) = to\_char(add\_months(sysdate,-1),'yyyymm')