【问题标题】:Add column of row count in the table在表中添加行数列
【发布时间】:2020-10-08 02:12:06
【问题描述】:

在我的 sql 查询之后,我收到了一个这样的表

  LOT_NO         x

A-TJ26-03-1-030  2
A-TJ26-03-1-020  1
A-TJ26-03-1-040  3

我想在此表中添加第三列,其中包含总行数的值,如下所示:

  LOT_NO         x  Count

A-TJ26-03-1-030  2    3
A-TJ26-03-1-020  1    3
A-TJ26-03-1-040  3    3 

我能够获取此表的计数值,但由于我想使用 java 中的逻辑比较 x-column 和 count-column,所以我想在此表中创建一个具有行数的新列。 我怎样才能做到这一点。任何建议表示赞赏。

【问题讨论】:

  • 你用的是Oracle还是Mysql的哪个数据库
  • oracle 数据库

标签: oracle


【解决方案1】:

使用count(*) over() 获取总数。这是demo

select
    LOT_NO,
    x,
    count(*) over () as count
from yourTable

【讨论】:

    【解决方案2】:

    请使用下面的alter语句语法来添加新列,

    alter table table_name add column_name data_type;
    

    例如:

    alter table table_name add count number(38,0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多