0

给定以下 2 个表:

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 列?

4

4 回答 4

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]%';
于 2013-06-26T17:14:12.740 回答
1

When you are grouping by Proj_ID you can only add aggregate function on ACCT_ID to return, first of all I can't understand why you are using group by, can't you just use simple select as below

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]%';
于 2013-06-26T17:16:26.447 回答
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]%';
于 2013-06-26T17:15:28.770 回答
0

让它也返回 ACCT_ID。您在这里只返回 PROJ_ID、PROJ_NAME :

选择 PROJ_ID、PROJ_NAME。在此处包含 ACCT_ID 并查看它是否有效。

于 2013-06-26T17:10:51.733 回答