如何插入根据另一列 (SQL) 中的值设置唯一 ID 的列?

How to insert a column which sets unique id based on values in another column (SQL)?

我将创建 table,我将在其中为不同的公司插入多个值。基本上我有下面 table 中的所有值,但我想添加一个链接到 IndicatorName 的列 IndicatorID,以便每个指标都有一个唯一的 ID。这显然不是 PrimaryKey.

我将插入多选数据:

CREATE TABLE abc                
INSERT INTO abc
SELECT company_id, 'roe', roevalue, metricdate
FROM TABLE1 
INSERT INTO abc
SELECT company_id, 'd/e', devalue, metricdate
FROM TABLE1

所以,我不知道如何添加我上面提到的 IndicatorID。

编辑: 这是我填充新 table:

的方式
INSERT INTO table(IndicatorID, Indicator, Company, Value, Date)

SELECT [the ID that I need], 'NI_3y' as 'Indicator', t.Company, avg(t.ni) over (partition by t.Company order by t.reportdate rows between 2 preceding and current row) as 'ni_3y',
t.reportdate
FROM table t
LEFT JOIN IndicatorIDs i
ON i.Indicator = roe3 -- the part that is not working if I have separate indicatorID table

我将为相同的公司插入不同的指标。我想要 indicatorID.

您的“指标”本身就是一个合适的实体。使用所有指标创建 table:

create table indicators (
    indicator_id int identity(1, 1) primary key,
    indicator varchar(255)
);

那么,在这个table中只使用id。您可以在参考table.

中查找值

然后你的插入有点复杂:

INSERT INTO indicators (indicator)
    SELECT DISTINCT roevalue
    FROM table1 t1
    WHERE NOT EXISTS (SELECT 1 FROM indicators i2 WHERE i2.indicator = t1.roevalue);

然后:

INSERT INTO ABC (indicatorId, companyid, value, date)
    SELECT i.indicatorId, t1.company, v.value, t1.metricdate
    FROM table1 t1 CROSS APPLY
         (VALUES ('roe', t1.roevalue), ('d/e', t1.devalue)
         ) v(indicator, value) JOIN
         indicators i
         ON i.indicator = v.indicator;

此过程称为规范化,这是在数据库中存储数据的典型方式。

DDL 和 INSERT 语句创建一个指标 table,对指标具有唯一约束。因为 ind_id 旨在成为 abc table 中的外键,所以它使用 IDENTITY 属性.

创建为不可分解的代理整数主键
drop table if exists test_indicators;
go
create table test_indicators (
  ind_id        int identity(1, 1) primary key not null,
  indicator     varchar(20) unique not null);
go

insert into test_indicators(indicator) values
('NI'),
('ROE'),
('D/E');

abc table 依赖指标 table 中的 ind_id 列作为外键引用。填充 abc table company_id 与 ind_id 相关联。

drop table if exists test_abc
go
create table test_abc(
  a_id  int     identity(1, 1) primary key not null,
  ind_id        int not null references test_indicators(ind_id),
  company_id    int not null,
  val           varchar(20) null);
go

insert into test_abc(ind_id, company_id) 
select ind_id, 102 from test_indicators where indicator='NI'
union all
select ind_id, 103 from test_indicators where indicator='ROE'
union all
select ind_id, 104 from test_indicators where indicator='D/E'
union all
select ind_id, 103 from test_indicators where indicator='NI'
union all
select ind_id, 105 from test_indicators where indicator='ROE'
union all
select ind_id, 102 from test_indicators where indicator='NI';

查询得到结果

select i.ind_id, a.company_id, i.indicator, a.val
from test_abc a
     join test_indicators i on a.ind_id=i.ind_id;

输出

ind_id  company_id  indicator   val
1       102         NI          NULL
2       103         ROE         NULL
3       104         D/E         NULL
1       103         NI          NULL
2       105         ROE         NULL
1       102         NI          NULL

我终于找到了我的问题的解决方案,在我看来这很简单,尽管这需要时间并询问不同的人。 首先,我创建了 indicators table,我在其中为我拥有的所有指标分配了 primary key

CREATE TABLE indicators (
    indicator_id int identity(1, 1) primary key,
    indicator varchar(255)
);

然后我不使用任何 JOINsCROSS APPLY 即可轻松填充。我不知道这是否是最佳选择,但它似乎是最简单的选择:

INSERT INTO table(IndicatorID, Indicator, Company, Value, Date)    
SELECT 
    (SELECT indicator_id from indicators i where i.indicator = 'NI_3y) as IndicatorID, 
    'NI_3y' as 'Indicator', 
     Company, 
     avg(ni) over (partition by Company order by reportdate rows between 2 preceding and current row) as ni_3y,
    reportdate
FROM TABLE1