我正在从外部源接收一些数据,其中实际值被ids替换。
| Serial# | Sex | Married | Income |
------------------------------------
| 1 | 1 | 2 | 1 |
| 2 | 1 | 1 | 3 |
| 3 | 1 | 2 | 2 |
现在,我有一个维度表,其中包含每个id的值:
| Tag | Value | Id |
--------------------------------------
| Sex | M | 1 |
| Sex | F | 2 |
| Married | Y | 1 |
| Married | N | 1 |
| Income | Less than 100 | 1 |
| Income | Between 100 and 1K | 2 |
| Income | More than 1K | 3 |
现在,我希望用第一个表中的值替换第一个表中三个列Sex、Married和Income的所有in。
同样,如果一个未知的Id进来了,它不在维度表中,我们希望用‘未知’来更新。
这只是一个例子。我的数据包含大约100个这样的列。实现这一目标的最便宜和最快的方法是什么?我不想写成百上千条更新语句。
发布于 2018-01-09 07:48:54
我不认为需要更新任何东西,您可以连接这两个表:
select i."Serial#",
sd."Value" as sex,
md."Value" as married,
id."Value" as income
from the_intput_table i
join dimension sd on sd.id = i."Sex" and sd."Tag" = 'Sex'
join dimension md on md.id = i."Married" and md."Tag" = 'Married'
join dimension id on id.id = i."Income" and id."Tag" = 'Income'
发布于 2018-01-09 07:54:57
如果您想用列ID的值更新现有的列ID,那么可以使用下面的查询来实现:
UPDATE
table1
SET
table1.Sex = Table_B.col1,
table1.Married = Table_B.col1,
table1.Income = Table_B.col1,
FROM table1
INNET JOIN Dimension as d1 ON t1.Sex = d1.Id AND d1.Tag = 'Sex'
INNET JOIN Dimension as d2 ON t1.Married = d2.Id AND d2.Tag = 'Married'
INNET JOIN Dimension as d3 ON t1.Income = d3.Id AND d3.Tag = 'Income'
发布于 2018-01-21 06:05:28
对上面的代码做少许修改,
select i.Serial#,
case when sd.Value is not null then sd.Value else 'UNKNOWN' end as sex,
case when md.Value is not null then md.Value else 'UNKNOWN' end as married,
case when id.Value is not null then id.Value else 'UNKNOWN' end as income
from the_intput_table i
left outer join dimension sd on sd.id = i.Sex and sd.Tag = 'Sex'
left outer join dimension md on md.id = i.Married and md.Tag = 'Married'
left outer join dimension id on id.id = i.Income and id.Tag = 'Income'
https://stackoverflow.com/questions/48163461
复制