sql server连接表与转换表

时间:2012-08-22 00:09:28

标签: sql-server

我有两张桌子:

ID   Name    indicator_1   indicator_2
1    text         1               1
2    text         1               1
3    text         1               1
4    text         2               1
5    text         2               1
6    text         2               2
7    text         2               2
8    text         3               2

FieldName    Value    Description
indicator_1    1         good
indicator_1    2         medium
indicator_1    3         bad
indicator_2    1         yes
indicator_2    2         no

有没有办法加入这两张桌子并到达:

ID   Name    indicator_1   indicator_2
1    text         good               yes
2    text         good               yes
3    text         good               yes
4    text         medium             yes
5    text         medium             yes
6    text         medium             no
7    text         medium             no
8    text         bad                no

这只是一个样本,实际表格中有大约70个指标......非常感谢

1 个答案:

答案 0 :(得分:4)

SELECT t1.ID, t1.Name, t2a.Description, t2b.Description
FROM Table1 t1
INNER JOIN Table2 t2a ON t2a.FieldName = 'indicator_1' AND t2a.Value = t1.indicator_1
INNER JOIN Table2 t2b ON t2b.FieldName = 'indicator_2' AND t2b.Value = t1.indicator_2

根据您的实际数据,您可能希望将“INNER”更改为“LEFT”,但INNER JOIN可能就在这里。