我将这3个表合并到INNER JOIN的报表查询中(这是一个简化示例):
USERS
ID NAME CODE1 CODE2
1 James c1_2 c2_3
2 Bob c1_3 c2_1
C1
CODE1 VALUE
c1_1 interested
c1_2 maybe
c1_3 not interested
C2
CODE1 VALUE
c2_1 prepared
c2_2 self-study
c2_3 novice
查询结果如下所示:
NAME INTEREST PREPARED
James maybe novice
Bob not interested prepared
我想摆脱查找表并仅使用USERS表创建此查询结果,可能还有一些聪明的REPLACE()语句。可以吗?
谢谢!
答案 0 :(得分:1)
当然但我没有看到这一点。
SELECT u.Name, c1.Value, c2.Value
FROM dbo.Users u
INNER JOIN (
SELECT 'c1_1' as Code1, 'interested' as Value
UNION ALL SELECT 'c1_2', 'maybe'
UNION ALL SELECT 'c1_3', 'not interested'
) c1 ON c1.Code1 = u.Code1
INNER JOIN (
SELECT 'c2_1' as Code1, 'prepared' as Value
UNION ALL SELECT 'c2_2', 'self-study'
UNION ALL SELECT 'c2_3', 'novice'
) c2 ON c2.Code1 = u.Code2
PS。我知道这(很可能)不是你的意思,但我的猜测是,它总是会胜过任何REPLACE解决方案,而且更灵活。