鉴于以下两个表:
PROJ_CUSTOM PSR_FINAL_DATA
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
PROJ_ID PROJ_NAME PROJ_ID ACCT_ID
61000.001.ABC Accounting 61000.001.ABC 610-10-23
61000.001.ABD Marketing 61000.001.ABD 610-20-23
61000.001.ABE Applications 61000.001.ABE 610-30-23
61000.001.ABF HR 61000.001.ABF 610-40-23
61000.001.ABG Foo 61000.001.ABG 610-50-23
61000.001.ABC Accounting 61000.001.ABC 610-10-23
61000.001.ABD Marketing 61000.001.ABD 610-20-23
61000.001.ABE Applications 61000.001.ABE 610-30-23
61000.001.ABF HR 61000.001.ABF 610-40-23
61000.001.ABG Foo 61000.001.ABG 610-50-23
以下sql:
SELECT PROJ_ID, PROJ_NAME
FROM DELTEK.PROJ_CUSTOM
INNER JOIN
(
SELECT PROJ_ID AS Project
FROM DELTEK.PSR_FINAL_DATA
GROUP BY PROJ_ID
) FinalDb
ON FinalDb.Project = PROJ_ID
WHERE PROJ_ID LIKE '61000.001.[A-Z]%';
sql返回PROJ_ID和PROJ_NAME列。
我如何能够从表2(PSR_FINAL_DATA)添加ACCT_ID列?
答案 0 :(得分:2)
只要每个PROJ_ID的ACCT_ID始终相同,您可以尝试:
SELECT Custom.PROJ_ID, Custom.PROJ_NAME, FinalDb.ACCT_ID
FROM DELTEK.PROJ_CUSTOM Custom
INNER JOIN
(
SELECT DISTINCT PROJ_ID, ACCT_ID
FROM DELTEK.PSR_FINAL_DATA
) FinalDb
ON FinalDb.PROJ_ID = Custom.PROJ_ID
WHERE Custom.PROJ_ID LIKE '61000.001.[A-Z]%';
答案 1 :(得分:1)
当您通过Proj_ID进行分组时,您只能在ACCT_ID上添加聚合函数才能返回,首先我无法理解您使用group by的原因,不能只使用简单的选择如下所示
SELECT PROJ_ID, PROJ_NAME FROM DELTEK.PROJ_CUSTOM
INNER JOIN
(
SELECT PROJ_ID,ACC_ID
FROM DELTEK.PSR_FINAL_DATA
) FinalDb
ON FinalDb.Project = PROJ_ID
WHERE PROJ_ID LIKE '61000.001.[A-Z]%';
答案 2 :(得分:0)
让它也返回ACCT_ID。您只在此处返回PROJ_ID,PROJ_NAME:
SELECT PROJ_ID,PROJ_NAME。在那里包括ACCT_ID,看它是否有效。
答案 3 :(得分:0)
SELECT PROJ_ID, PROJ_NAME,ACCT_ID
FROM DELTEK.PROJ_CUSTOM
INNER JOIN
(
SELECT PROJ_ID AS Project,ACCT_ID
FROM DELTEK.PSR_FINAL_DATA
GROUP BY PROJ_ID
) FinalDb
ON FinalDb.Project = PROJ_ID
WHERE PROJ_ID LIKE '61000.001.[A-Z]%';