【问题标题】:How to auto increment by group in DB2如何在 DB2 中按组自动递增
【发布时间】:2015-06-26 15:47:50
【问题描述】:

我有一个不同类别的数据集 即

customer_id category
a blue
b blue
c green
d green
e green
f pink
g pink

我想插入一个 DB2 表,该表会根据我插入的顺序为每个类别自动增加一个键,即

customer_id category auto_incr
a blue 1
b blue 2
c green 1
d green 2
e green 3
f pink 1
g pink 2

那么明天当我有这个新数据集时:

customer_id category auto_incr
h blue
i blue
j pink

我的最终数据集如下所示:

customer_id category auto_incr
a blue 1
b blue 2
c green 1
d green 2
e green 3
f pink 1
g pink 2
h blue 3
i blue 4
j pink 3

我需要使用 DB2 sql 执行此操作,并且将多次插入此表

【问题讨论】:

  • 它可以很好地与触发器配合使用。试试看,真的很简单。告诉我们您遇到的问题
  • 我以前从未使用过触发器 - 你有很好的例子吗?

标签: sql db2 auto-increment


【解决方案1】:

您实际上不需要将这些值存储在表中,因为您可以即时导出它们:

SELECT 
 customer_id 
 ,category 
 ,ROW_NUMBER() OVER (PARTITION BY category ORDER BY customer_id) AS auto_incr
FROM
 yourtable
ORDER BY 1

【讨论】:

  • 当我每天都需要插入表格时,这如何工作?而且我没有存储号码?
猜你喜欢
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多