我需要根据特定的连接条件创建一个自动增加的id。我有两个表,每个表都有标识列。主表与" trxid"上的辅助表有关系。柱。以下是样本数据
declare @t1 table (trxid int,data1 varchar(100), data2 varchar(100))
declare @m table (mid int, trxid int, mname varchar(100), mdate date)
insert into @t1
select 1,'Test1','Test1'
union all
select 2,'Test2','Test2'
insert into @m
select 1,1,'Stage Gate1',GETDATE()
union all
select 2,1,'Stage Gate2',GETDATE()
union all
select 3,1,'Stage Gate3',GETDATE()
union all
select 4,1,'Stage Gate4',GETDATE()
union all
select 5,1,'Stage Gate5',GETDATE()
union all
select 6,1,'Stage Gate6',GETDATE()
union all
select 7,2,'Stage Gate1',GETDATE()
union all
select 8,2,'Stage Gate2',GETDATE()
union all
select 9,2,'Stage Gate3',GETDATE()
union all
select 10,2,'Stage Gate4',GETDATE()
union all
select 11,2,'Stage Gate5',GETDATE()
union all
select 12,2,'Stage Gate6',GETDATE()
union all
select 13,2,'Stage Gate7',GETDATE()
结果以下声明
select t.trxid,m.mid,t.data1,t.data2,m.mname,m.mdate from @t1 t inner join @m m on(t.trxid=m.trxid)
trxid mid data1 data2 mname mdate
----------- ----------- ------- ------- --------------- ----------
1 1 Test1 Test1 Stage Gate1 2018-06-07
1 2 Test1 Test1 Stage Gate2 2018-06-07
1 3 Test1 Test1 Stage Gate3 2018-06-07
1 4 Test1 Test1 Stage Gate4 2018-06-07
1 5 Test1 Test1 Stage Gate5 2018-06-07
1 6 Test1 Test1 Stage Gate6 2018-06-07
2 7 Test2 Test2 Stage Gate1 2018-06-07
2 8 Test2 Test2 Stage Gate2 2018-06-07
2 9 Test2 Test2 Stage Gate3 2018-06-07
2 10 Test2 Test2 Stage Gate4 2018-06-07
2 11 Test2 Test2 Stage Gate5 2018-06-07
2 12 Test2 Test2 Stage Gate6 2018-06-07
2 13 Test2 Test2 Stage Gate7 2018-06-07
预期结果是
trxid id mid data1 data2 mname mdate
----------- ----------- ----------- ------- ------- --------------- ----------
1 1 1 Test1 Test1 Stage Gate1 2018-06-07
1 2 2 Test1 Test1 Stage Gate2 2018-06-07
1 3 3 Test1 Test1 Stage Gate3 2018-06-07
1 4 4 Test1 Test1 Stage Gate4 2018-06-07
1 5 5 Test1 Test1 Stage Gate5 2018-06-07
1 6 6 Test1 Test1 Stage Gate6 2018-06-07
2 1 7 Test2 Test2 Stage Gate1 2018-06-07
2 2 8 Test2 Test2 Stage Gate2 2018-06-07
2 3 9 Test2 Test2 Stage Gate3 2018-06-07
2 4 10 Test2 Test2 Stage Gate4 2018-06-07
2 5 11 Test2 Test2 Stage Gate5 2018-06-07
2 6 12 Test2 Test2 Stage Gate6 2018-06-07
2 7 13 Test2 Test2 Stage Gate7 2018-06-07
答案 0 :(得分:1)
假设您不需要保留此ID,然后根据持久数据生成新的唯一增量数,则可以使用窗口函数为每组数据生成this.toolbar = this.header.nativeElement.children[1];
:
row_number
如果您需要将此数据基于您已保存到其他地方的表中的数据,那么您的查询将需要更复杂。