簡體   English   中英

如何插入基於另一列(SQL)中的值設置唯一ID的列?

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

我將創建表,我將為不同的公司插入多個值。 基本上,我擁有下表中的所有值,但我想添加一個與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。

編輯:這是我填充新表的方式:

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

我將為同一家公司插入不同的指標。 我想要指標 ID。

您的“指標”本身就是一個適當的實體。 創建一個包含所有指標的表:

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

然后,使用此表中的id 您可以在參考表中查找該值。

然后你的插入有點復雜:

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 語句創建指標表,對指標具有唯一約束。 因為 ind_id 旨在成為 abc 表中的外鍵,所以它使用 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 表依賴於指標表中的 ind_id 列作為外鍵引用。 填充 abc 表 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表,在其中為我擁有的所有指標分配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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM