我有一个现有的sql查询,我想将其应用于从“不同”查询返回的每条记录。
我猜想循环遍历每个返回的记录,将其存储为字符串,并在其他查询中使用该值。我该怎么做呢?
sudo查询:
Select ...
for each record returned as X,
Select ... etc ... where ... LIKE X
编辑: 不知道如何让它更清晰,但我知道我可能没有把它弄清楚。我会试试:
distinct将返回一个包含许多记录的列。我需要将每个值应用于第二个SQL查询。
所以喜欢..选择X和Y,但使用X
从我的第二个查询返回Y.EDIT2: 如果distinct select返回
1
2
3
4
第二个查询返回单个记录“A”,当where子句看起来像...... ='1',“B”当where子句看起来像...... ='2',“C”当where子句看起来像...... ='3',而当where子句看起来像是C ='4'
然后我希望我的最终输出看起来像
1 | A
2 | B
3 | C
4 | C
编辑3: 第一个查询:
SELECT DISTINCT [user_id] from dbo.sap_empl_subset
第二个查询:
SELECT [name_pref_mixed]
FROM dbo.sap_empl_subset AS E
WHERE E.sap_position_no IN
(SELECT P.sap_position_no
FROM dbo.sap_position AS P
WHERE (LTRIM(RTRIM(P.sap_position_desc)) LIKE '%[VICE ]PRESIDENT%')
OR (LTRIM(RTRIM(P.sap_position_desc)) LIKE 'CHIEF%'))
AND E.sap_org_code =
(SELECT
CASE
WHEN S.sap_org_code_level2 = 0 THEN S.sap_org_code
WHEN S.sap_org_code_level3 = 0 THEN S.sap_org_code_level1
ELSE S.sap_org_code_level2
END
FROM dbo.sap_org_structure AS S
WHERE S.sap_org_code =
(SELECT E1.sap_org_code
FROM dbo.sap_empl_subset AS E1
WHERE E1.[user_id] = '<each item from first query needs applied here>'))
答案 0 :(得分:2)
SELECT *
FROM (
SELECT DISTINCT value
FROM mytable
) x
JOIN othertable y
ON y.value LIKE '%' || x.value || '%'
更新
如果你第一次查询是
SELECT my_x
FROM mytable
WHERE my_y = '…'
,第二个是
SELECT other_z
FROM othertable
WHERE other_y = my_x
你只需要一个加入:
SELECT my_x, other_z
FROM mytable
JOIN othertable
ON other_y = my_x
WHERE my_y = '…'
如果您刚刚发布了查询,那么回答会更容易。
更新2:
试试这个:
SELECT es.user_id, esp.name_pref_mixed
FROM sap_empl_subset es
JOIN sap_org_structure os
ON os.sap_org_code = es.sap_org_code
JOIN sap_empl_subset esс
ON esc.sap_org_code =
CASE
WHEN os.sap_org_code_level2 = 0 THEN os.sap_org_code
WHEN os.sap_org_code_level3 = 0 THEN os.sap_org_code_level1
ELSE os.sap_org_code_level2
END
WHERE esc.sap_position_no IN
(
SELECT sap_position_no
FROM sap_position sp
WHERE (LTRIM(RTRIM(sp.sap_position_desc)) LIKE '%[VICE ]PRESIDENT%')
OR (LTRIM(RTRIM(sp.sap_position_desc)) LIKE 'CHIEF%'))
)
DISTINCT
似乎在这里多余。您的第二个查询中有条件:
WHERE S.sap_org_code =
(
SELECT E1.sap_org_code
FROM dbo.sap_empl_subset AS E1
WHERE E1.[user_id] = '<each item from first query needs applied here>')
)
如果sap_empl_subset.user_id
答案 1 :(得分:0)
无需合并两个查询。我需要的只是嵌套的select语法,如下所示,其中第一行是第一个查询,第一个嵌套select是第二个查询。没有必要加入。
SELECT Distinct U.[user_id] AS "User ID", (
SELECT [empl_last_name]
FROM dbo.sap_empl_subset AS E
WHERE E.sap_position_no IN
(SELECT P.sap_position_no
FROM dbo.sap_position AS P
WHERE (LTRIM(RTRIM(P.sap_position_desc)) LIKE '%[VICE ]PRESIDENT%')
OR (LTRIM(RTRIM(P.sap_position_desc)) LIKE '%CHIEF%')
OR (LTRIM(RTRIM(P.sap_position_desc)) LIKE '%[EXECUTIVE ]VP%')
)
AND E.sap_org_code =
(SELECT
CASE
WHEN S.sap_org_code_level2 = 0 THEN S.sap_org_code
WHEN S.sap_org_code_level3 = 0 THEN S.sap_org_code_level1
ELSE S.sap_org_code_level2
END
FROM dbo.sap_org_structure AS S
WHERE S.sap_org_code =
(SELECT E1.sap_org_code
FROM dbo.user_id AS E1
WHERE E1.[user_id] = U.[user_id]))) As "VP"
From dbo.user_id As U WHERE U.[user_id] <> ''
ORDER BY [User ID]