我想在底部概括我的查询,以便获得所有项目和其他项目信息,其中我的子组具有一定的值,但我无法使其工作,因为我无法获得[ProjectFk]
SELECT
[HeadCount].[ID],
[LinkProjectAreaFk],
[Month1],
[Month2],
[Month3],
[Month4],
[Month5],
[Month6],
[Month7],
[Month8],
[Month9],
[Month10],
[Month11],
[Month12],
[HcYear],
[ApproveDirFk],
[ApproveDirDate],
[FreezeFk],
[freezeDate],
[CDSID],[FreezeTypeFk],
[ApproveDirTypeFk],
[SourcesFk],
[Project].[Title],
[Project].[Title]
FROM
[HeadCount],
[Project]
WHERE
[HeadCount].[LinkProjectAreaFk]
IN
(SELECT
[ID]
FROM
[LinkProject-Area]
WHERE
[SubgroupFk]=1)
and
[Project].[ID]=[LinkProject-Area].[ProjectFk]
以下查询有效,但仅适用于一个项目,我如何从子查询中获取projectFk,因为当我查看它时,他们说这是不可能的,并且使用左外连接我不能让它工作在所有
SELECT
[HeadCount].[ID],
[LinkProjectAreaFk],
[Month1],
[Month2],
[Month3],
[Month4],
[Month5],
[Month6],
[Month7],
[Month8],
[Month9],
[Month10],
[Month11],
[Month12],
[HcYear],
[ApproveDirFk],
[ApproveDirDate],
[FreezeFk],
[freezeDate],
[CDSID],
[FreezeTypeFk],
[ApproveDirTypeFk],
[SourcesFk],
[Project].[Title],
[Project].[Title]
FROM
[HeadCount],
[Project]
WHERE
[HeadCount].[LinkProjectAreaFk] IN
(SELECT
[ID]
FROM
[LinkProject-Area]
WHERE
[ProjectFk]=90
and
[SubgroupFk]=1)
and
[Project].[ID]=90
希望你能帮助我或指出我正确的方向
亲切的问候
嗨,我一直在尝试你的方式,但它仍然没有按照它应有的方式: 我无法绑定多部分标识符“MyCTE.ProjectFK”。和 无法绑定多部分标识符“MyCTE.ID”。当我查看这个错误与使用我的联接错误任何想法有关时?
;WITH MyCTE (ID, ProjectFK)
AS
(
SELECT
ID,
ProjectFk
FROM
[LinkProject-Area]
WHERE
SubgroupFk = 1
)
SELECT
*
FROM
[LinkProject-Area]
INNER JOIN
HeadCount ON [LinkProjectAreaFk] = MyCTE.ID
INNER JOIN
Project ON Project.ID = MyCTE.ProjectFK
答案 0 :(得分:0)
我认为你会想要像这样组织这个查询:
SELECT ...
FROM
(
SELECT ID, ProjectFk
FROM [LinkProject-Area]
WHERE SubgroupFk = 1
) LinkProjectAreas
INNER JOIN HeadCount ON LinkProjectAreaFK = LinkProjectAreas.ID
INNER JOIN Project ON Project.ID = LinkProjectAreas.ProjectFK
您还可以使用公用表表达式(CTE)来组织它:
;WITH LinkProjectAreas (ID, ProjectFK) AS
(
SELECT ID, ProjectFk
FROM [LinkProject-Area]
WHERE SubgroupFk = 1
)
SELECT ...
FROM LinkProjectAreas
INNER JOIN HeadCount ON LinkProjectAreaFK = LinkProjectAreas.ID
INNER JOIN Project ON Project.ID = LinkProjectAreas.ProjectFK