开发者

Update ID using two or more Tables

I Have a table called UID and the data looks like:

U_ID   Urgent  Impact  Severe  Priority
1      1       1       NULL    1
2      1       2       NULL    1
3      1       3       NULL    1
4      3       4       NULL    3
5      2       2       NULL    2
6      NULL    NULL    1       NULL
7      NULL    NULL    NULL    1
8      NULL    NULL    NULL    2
9      NULL    NULL    NULL    3
10     NUL开发者_高级运维L    NULL    NULL    4
11     1       NULL    NULL    NULL
12     1       1       NULL    NULL
13     1       2       NULL    NULL
14     1       2       1       NULL
15     1       3       1       NULL

and also I have another table called STID and the data looks like:

Priority    Impact  Urgent   ID
    3            4  3        1
    3            4  3        1
    2            2  2        1
    2            2  2        1
    2            2  2        1

and Also I have another table called PRID and the data looks like:

Priority   ID
1          2
2          2
2          2
3          2
4          2

I Have a Table called FID and it contains U_ID and ID columns So I am trying to Insert those columns using the U_ID from UID Table and ID from STID and PRID Table.How can I do this?

My Final FID Table should like this:

U_ID   ID
4      1
4      1
5      1
5      1
5      1
7      2
8      2
8      2
9      2
10     2


select U_ID, S.ID
from UID as U
  inner join STID as S
    on S.Pr = U.PR and
       S.Im = U.IM and
       S.Ur = U.UR
union all
select U_ID, P.ID
from UID as U
  inner join PRID as P
    on U.PR = P.PR and
       U.IM is null and
       U.UR is null


select u.U_ID, s.ID
into #temp
from UID as u
  inner join STID as s on (u.Priority=s.Priority and u.Impact=s.Impact)
and s.Priority is not null

insert into #temp
select u.U_ID, p.ID
from UID as u
  inner join PRID as p on (u.Priority=p.Priority)
and u.U_ID not in (select U_ID from #temp)

Select * from #temp
order by U_ID

Drop Table #Temp
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜