【发布时间】:2022-08-18 22:43:33
【问题描述】:
我有多个 MS SQL 资产源,我想将它们组合成一个资产表,该表将存储一些基本信息,以及资产的来源。每个表都有不同数量的列,但我们只对少数列感兴趣。 每个数据源大约有 2-3k 行。
伪代码
select name, os, user, source from tblLSAssets and insert into TblALL
if tblAZ.name exists in tblAll update that row with tblAll.source +=\"AZ\" (or use separete col)
if tblAZ.name NOT exists in tblAll add a new row to tblAll with tblAZ.name,tblAZ.os etc. update source col.
对每个资产源重复。
如果这更容易,我很高兴为每个数据源提供多个列。
我在枚举每个表的代码中试过这个,但这很慢。并想知道他们是否有一些 SQL 魔法可以使这更快一点。
tblLSAssets:
| name | OS | user | colx |
|---|---|---|---|
| PC1 | Win | user1 | bla |
| PC2 | Lin | user2 | bla |
| PC3 | Win | user3 | bla |
| PC4 | Mac | user4 | bla |
tblAZ
| name | OS | user | colx | coly |
|---|---|---|---|---|
| PC1 | Win | user1 | bla | bla |
| PC20 | OS | user20 | bla | bla |
| PC30 | Xt | user30 | bla | bla |
全部
| name | OS | user | source |
|---|---|---|---|
| PC1 | Win | user1 | LS+AZ |
| PC20 | OS | user2 | AZ |
| PC30 | Xt | user3 | AZ |
| PC4 | Mac | user4 | LS |
-
听起来你想为每个表写一个
MERGE语句来填充你的tblAll。这也可以使用带有STRING_AGG()的UNION 将它们组合在一起来解决,您可以将其扔到一个视图中,而不是将所有数据存储两次。
标签: sql