我正在尝试获取此查询的结果并返回相同的结果,除了查找另一个表以获取值来代替我的BK值。
SELECT
Case when S.SCENARIO_SK is null then -2
when DS.SCENARIO_SK is null then -1
else DS.SCENARIO_SK
end SCENARIO_SK,
GLP.DATE_SK,
CASE
WHEN S.Q_ACCT_STR = GLEM.Q_ACCT_STR
THEN
GLA.ACCOUNT_BK
ELSE
LEFT(S.Q_ACCT_STR,5)
END ACCOUNT,
CASE
WHEN S.Q_ACCT_STR = GLEM.Q_ACCT_STR
THEN
GLO.ORG_BK
ELSE
RIGHT(S.Q_ACCT_STR,5)
END ORG,
S.Q_ACCT_STR,
S.GL_PER_AMT,
S.GL_PER_BAL,
S.Q_YTD_AMT,
S.DM_CREATE_DATE,
S.DM_AUDIT_KEY
FROM STG_GL_SUMMARY S
LEFT OUTER JOIN GL_PER_PARMS GLP ON GLP.GLP_AUTO_KEY = S.GLP_AUTO_KEY
LEFT OUTER JOIN D_GL_SCENARIO DS ON DS.SCENARIO_SK =S.SCENARIO_SK
LEFT OUTER JOIN STG_GL_EXCEPTION_MAP GLEM ON GLEM.Q_ACCT_STR = S.Q_ACCT_STR
LEFT OUTER JOIN D_GL_ACCOUNT GLA ON GLA.ACCOUNT_BK = GLEM.ACCOUNT_BK
LEFT OUTER JOIN D_GL_ORG GLO ON GLO.ORG_BK = GLEM.ORG_BK
从上面的查询中,我想执行以下操作:
SELECT GLA.ACCOUNT_SK
FROM D_GL_ACCOUNT GLA
WHERE QUERY(ACCOUNT) = GLA.ACCOUNT.BK
SELECT GLO.ORG_SK
FROM D_GL_ORG GLO
WHERE QUERY(ACCOUNT) = GLO.ORG.BK
我的最终结果将是两个查询的所有字段。
谢谢。我对T_SQL比较陌生。
答案 0 :(得分:1)
您可以使用所谓的派生表。只需用大括号包装内部查询,指定别名并对其执行选择。例如:
select
tmp.value
from
(select
some_value as value
from
some_table
) tmp