【问题标题】:Fetch first 10 rows with IN Operation使用 IN 操作获取前 10 行
【发布时间】:2022-12-18 19:24:30
【问题描述】:

我想按照以下条件获取前 10 行以及 tabname = 'TABLE1' 和 'TABLE2' 的位置(如果这些表甚至不在前 10 行中)

根据以下条件的前 10 行工作正常:

db2 "select substr(a.tabname,1,30) as TABNAME,
> a.rows_read as RowsRead,
> (a.rows_read / (b.commit_sql_stmts + b.rollback_sql_stmts + 1)) as TBRRTX,
> (b.commit_sql_stmts + b.rollback_sql_stmts) as TXCNT
> from sysibmadm.snaptab a, sysibmadm.snapdb b
> where a.dbpartitionnum = b.dbpartitionnum
> and b.db_name = 'LIVE'
> order by a.rows_read desc fetch first 10 rows only"

TABNAME                        ROWSREAD             TBRRTX               TXCNT
------------------------------ -------------------- -------------------- --------------------
XOUTMSGLOG                              43845129056                   41           1049571334
SCHSTATUS                               35336410261                   33           1049571334
ADDRESS                                 26817245226                   25           1049571334
CATGRPDESC                              25628156703                   24           1049571334
ORDERITEMS                              23945555619                   22           1049571334
ORDERS                                  10656700035                   10           1049571334
XPAYINSTDATA                            10555959906                   10           1049571334
OFFER                                   10426958061                    9           1049571334
SCHBRDCST                               10286981444                    9           1049571334
ATTRVALDESC                              8327058697                    7           1049571334

  10 record(s) selected.

现在,要求是有“TABLE1”和“TABLE2”(如果这些表甚至不在前 10 行)那么如何在上面的语句中添加这个条件?

所以它看起来像下面这样:

TABNAME                        ROWSREAD             TBRRTX               TXCNT
------------------------------ -------------------- -------------------- --------------------
XOUTMSGLOG                              43845129056                   41           1049571334
SCHSTATUS                               35336410261                   33           1049571334
ADDRESS                                 26817245226                   25           1049571334
CATGRPDESC                              25628156703                   24           1049571334
ORDERITEMS                              23945555619                   22           1049571334
ORDERS                                  10656700035                   10           1049571334
XPAYINSTDATA                            10555959906                   10           1049571334
OFFER                                   10426958061                    9           1049571334
SCHBRDCST                               10286981444                    9           1049571334
ATTRVALDESC                              8327058697                    7           1049571334
TABLE1                                        81444                    1           10495713341
TABLE2                                           97                    1           1049571334
 
 12 record(s) selected.

【问题讨论】:

  • 为这两个额外的表添加第二个查询并使用 UNION ALL 将两个查询放在一起

标签: sql plsql db2-luw


【解决方案1】:

适用于任何 SELECT 语句的通用解决方案。

SELECT *
FROM
(
  -- Whatever SELECT statement with the desired expression 
  -- in the ORDER BY clause of the ROW_NUMBER function
  SELECT 
    TABNAME, COLCOUNT
  , ROW_NUMBER () OVER (ORDER BY COLCOUNT DESC) AS RN_
  FROM SYSCAT.TABLES
  WHERE TABSCHEMA = 'SYSIBM'
)
WHERE RN_ <= 5 OR TABNAME IN ('SQLSCHEMAS', 'SQLTABLES')
ORDER BY RN_
TABNAME COLCOUNT RN_
SYSROUTINES 84 1
SYSTABLES 83 2
SYSINDEXES 71 3
SYSPLAN 69 4
ROUTINES 56 5
SQLSCHEMAS 11 73
SQLTABLES 11 74

在你的情况下:

SELECT *
FROM
(
  SELECT substr(a.tabname,1,30) as TABNAME,
  a.rows_read as RowsRead,

  --(a.rows_read / (b.commit_sql_stmts + b.rollback_sql_stmts + 1)) as TBRRTX,
  --(b.commit_sql_stmts + b.rollback_sql_stmts) as TXCNT
  A.ROWS_READ / NULLIF (B.TOTAL_APP_COMMITS + B.TOTAL_APP_ROLLBACKS, 0) as TBRRTX,
  B.TOTAL_APP_COMMITS + B.TOTAL_APP_ROLLBACKS as TXCNT,

  ROW_NUMBER () OVER (order by a.rows_read desc) AS RN_
  FROM 
  /*
  sysibmadm.snaptab a, sysibmadm.snapdb b
  where a.dbpartitionnum = b.dbpartitionnum
  --and b.db_name = 'LIVE'
  --order by a.rows_read desc fetch first 10 rows ONLY
  */
  -- It's better to use new MON functions instead of old SNAP ones
  TABLE (MON_GET_TABLE (NULL, NULL, -2)) a
  JOIN TABLE (MON_GET_DATABASE (-2)) b ON b.MEMBER = a.MEMBER
)
WHERE RN_ <= 10 OR TABNAME IN ('TABLE1', 'TABLE2')
ORDER BY RN_

顺便提一句:
sysibmadm.snapdb 仅返回当前连接的数据库的信息,因为它基于 SELECT ... FROM TABLE (SYSPROC.SNAP_GET_DB(' '))(空格作为参数值表示当前数据库)。因此,如果您使用了错误的数据库名称,b.db_name = 'LIVE' 谓词只能使您的查询不返回任何行。

【讨论】:

    猜你喜欢
    • 2017-04-02
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 2015-09-08
    • 2011-03-05
    相关资源
    最近更新 更多