AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 244112
Accepted
Christopher Karsten
Christopher Karsten
Asked: 2019-07-31 01:26:32 +0800 CST2019-07-31 01:26:32 +0800 CST 2019-07-31 01:26:32 +0800 CST

为范围 sql 之间的值创建列

  • 772

我想要实现的是基于范围之间的 num_extents 创建一个新列,并且只打印下面的表名。

这就是我所拥有的 [table1]:

在此处输入图像描述

这就是我想要的[table2]:

在此处输入图像描述

我使用了一个 case 语句,它允许使用以下内容获取范围之间的值:

select sum(case when num_of_extents between 0 and 9 then 1 else 0 end) as one,
       sum(case when num_of_extents between 10 and 19 then 1 else 0 end) as two,
       sum(case when num_of_extents between 20 and 29 then 1 else 0 end) as three,
       sum(case when num_of_extents between 30 and 39 then 1 else 0 end) as four,
       sum(case when num_of_extents between 40 and 49 then 1 else 0 end) as five,
       sum(case when num_of_extents >= 50 then 1 else 0 end) as six
from (
select
a.tabname,
count(*) num_of_extents,
sum( pe_size ) total_size_pg,
round(sum( pe_size*4),2) total_size_kb,
round(sum( pe_size*4/1024 ),2) total_size_mb,
round(sum( pe_size *4/1024/1024),2) total_size_gb
from systabnames a, sysptnext b, sysdatabases c
where a.partnum = b.pe_partnum
and a.dbsname = c.name
and a.dbsname = 'system'
-- a.tabname not like ' %'
group by 1
order by 3 desc, 4 desc
) a

上面的输出:

one    2
two    1
three  1
four   5
five   0
six    2

请忽略使用的列的命名,但我不想在范围之间求和,而是想打印出表的名称,而不是像 [table2] 中那样。如何更改我的 sql case 语句以在 informix 中执行此操作?

informix
  • 1 1 个回答
  • 1703 Views

1 个回答

  • Voted
  1. Best Answer
    Christopher Karsten
    2019-08-01T06:45:29+08:002019-08-01T06:45:29+08:00

    我想出了我的问题的答案。以下工作正常

    select
    case when num_of_extents between 0 and 9 then a.tabname end as one,
    case when num_of_extents between 10 and 19 then a.tabname end two,
    case when num_of_extents between 20 and 29 then a.tabname end three,
    case when num_of_extents between 30 and 39 then a.tabname end four,
    case when num_of_extents between 40 and 49 then a.tabname end five,
    case when num_of_extents >= 50 then a.tabname end six
    from (
    select
    a.tabname,
    count(*) num_of_extents,
    sum( pe_size ) total_size_pg,
    round(sum( pe_size*4),2) total_size_kb,
    round(sum( pe_size*4/1024 ),2) total_size_mb,
    round(sum( pe_size *4/1024/1024),2) total_size_gb
    from systabnames a, sysptnext b, sysdatabases c
    where a.partnum = b.pe_partnum
    and a.dbsname = c.name
    and a.dbsname = 'system'
    -- a.tabname not like ' %'
    group by 1
    order by 3 desc, 4 desc
    ) a group by 1,2,3,4,5,6
    

    dbaccess 中的输出如下所示:

    one
    two    bc_storage_idx2
    three
    four
    five
    six
    
    one
    two
    three
    four   tock_mvmt
    five
    six
    
    one
    two
    three  rd_stats
    four
    five
    six
    
    one
    two    tock_mvmt_idx1
    three
    four
    five
    six
    ...
    

    请注意,我没有使用与上面相同的表格数据。以上只是如何在 dbaccess 中呈现信息的示例。由于列格式的数量,它将无法根据需要显示,但可以在提取时输出到电子表格时进行过滤。

    如果我在 AIX 中弄乱我的环境变量,下面的格式会起作用:

           select
           (case when num_of_extents between 0 and 9 then a.tabname end)[2,20] as one,
           case when num_of_extents between 10 and 19 then a.tabname[2,20] end two,
           case when num_of_extents between 20 and 29 then a.tabname[2,20] end three,
           case when num_of_extents between 30 and 39 then a.tabname[2,20] end four,
           case when num_of_extents between 40 and 49 then a.tabname[2,20] end five,
           case when num_of_extents >= 50 then a.tabname[2,20] end six
    from (
    select
    a.tabname,
    count(*) num_of_extents,
    sum( pe_size ) total_size_pg,
    round(sum( pe_size*4),2) total_size_kb,
    round(sum( pe_size*4/1024 ),2) total_size_mb,
    round(sum( pe_size *4/1024/1024),2) total_size_gb
    from systabnames a, sysptnext b, sysdatabases c
    where a.partnum = b.pe_partnum
    and a.dbsname = c.name
    and a.dbsname = 'system'
    -- a.tabname not like ' %'
    group by 1
    order by 3 desc, 4 desc
    ) a group by 1,2,3,4,5,6
    

    我不知道如何使该部分在 stackexchange 上以粗体显示,但请参阅 [2,20],它将用于格式。

    • 1

相关问题

  • 给定以下规则,我应该如何最好地设计表和关系?

  • 尽管已关闭,但与 Informix 的连接仍处于活动状态?

  • 聚簇索引是否比预先排序加载文件和创建非聚簇索引提供更多好处?

  • Informix 的重叠

  • 如何删除名称不明确的程序?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve