我有以下5个表 -
CREATE TABLE [dbo].[MSP_EpmProject](
[ProjectUID] [uniqueidentifier] NOT NULL,
[ProjectName] [nvarchar](255) NOT NULL,
[ProjectAuthorName] [nvarchar](255) NULL,
CONSTRAINT [PK_MSP_EpmProject] PRIMARY KEY CLUSTERED
([ProjectUID] ASC))
CREATE TABLE [dbo].[Project_CI_Mapping](
[ProjectName] [nvarchar](255) NOT NULL,
[CI] [nvarchar](100) NOT NULL)
CREATE TABLE [dbo].[ca_owned_resource](
[resource_name] [nvarchar](100) NOT NULL,
[resource_description] [nvarchar](255) NULL,
[resource_family] [int] NULL,
[resource_class] [int] NOT NULL,
[resource_status] [int] NULL,
[resource_tag] [nvarchar](64) NULL)
CREATE TABLE [dbo].[DimTeamProject](
[ProjectNodeSK] [int] IDENTITY(1,1) NOT NULL,
[ProjectNodeGUID] [uniqueidentifier] NOT NULL,
[ProjectNodeName] [nvarchar](256) NULL,
PRIMARY KEY CLUSTERED
([ProjectNodeSK] ASC))
CREATE TABLE [dbo].[DimIteration](
[IterationSK] [int] IDENTITY(1,1) NOT NULL,
[IterationName] [nvarchar](256) NULL,
[IterationGUID] [nvarchar](256) NOT NULL,
PRIMARY KEY CLUSTERED
([IterationSK] ASC))
我有一个简单的查询,试图从所有表中获取列,但它返回给我重复的值。尝试INNER JOIN会返回重复值&尝试LEFT OUTER JOIN时,它为“DimIteration.IterationName”提供了NULL值。
查询是 -
select m.ProjectName,m.ProjectAuthorName "Project Manager", p.CI,c.resource_tag "Alt CI ID", i.IterationName
from MSP_EpmProject m, Project_CI_Mapping p, ca_owned_resource c, DimTeamProject t, DimIteration i
where i.ProjectGUID = UPPER(CAST(t.ProjectNodeGUID AS NVARCHAR(256)))
and p.CI = c.resource_name
and m.ProjectName = p.ProjectName
order by m.ProjectName,m.ProjectAuthorName, p.CI,c.resource_tag, i.IterationName
可能的映射是 -
MSP_EpmProject.ProjectName = Project_CI_Mapping.ProjectName
Project_CI_Mapping.CI = ca_owned_resource.resource_name
ca_owned_resource.resource_tag = DimTeamProject.ProjectNodeName
DimIteration.ProjectGUID = UPPER(CAST(DimTeamProject.ProjectNodeGUID AS NVARCHAR(256)))
同样适用的解决方案是什么?
感谢。
答案 0 :(得分:1)
如果不详细研究这个问题,摆脱重复的一种方法是在ORDER BY
之前插入一个GROUP BY子句,如下所示:
select m.ProjectName,m.ProjectAuthorName "Project Manager", p.CI,c.resource_tag "Alt CI ID", i.IterationName
from MSP_EpmProject m, Project_CI_Mapping p, ca_owned_resource c, DimTeamProject t, DimIteration i
where i.ProjectGUID = UPPER(CAST(t.ProjectNodeGUID AS NVARCHAR(256)))
and p.CI = c.resource_name
and m.ProjectName = p.ProjectName
GROUP BY m.ProjectName,m.ProjectAuthorName, p.CI,c.resource_tag, i.IterationName
order by m.ProjectName,m.ProjectAuthorName, p.CI,c.resource_tag, i.IterationName
另一种方法是在DISTINCT
之后和您希望返回的第一列之前插入SELECT
。
e.g。 SELECT DISTINCT m.ProjectName...
答案 1 :(得分:1)
您的查询中有CROSS JOIN
。如果你使用较新的ANSI-92语法重写它(无论如何我建议你做reasons explained here),你可以看到交叉连接的位置:
select m.ProjectName,
m.ProjectAuthorName "Project Manager",
p.CI,c.resource_tag "Alt CI ID",
i.IterationName
from MSP_EpmProject m
INNER JOIN Project_CI_Mapping p
ON m.ProjectName = p.ProjectName
INNER JOIN ca_owned_resource c
ON p.CI = c.resource_name
CROSS JOIN DimTeamProject t
INNER JOIN DimIteration i
ON i.ProjectGUID = UPPER(CAST(t.ProjectNodeGUID AS NVARCHAR(256)))
order by m.ProjectName,m.ProjectAuthorName, p.CI,c.resource_tag, i.IterationName;
基本上没有任何事情可以事先将DimTeamProject
与任何表格联系起来。基于你有这个
ca_owned_resource.resource_tag = DimTeamProject.ProjectNodeName
作为一种可能的关系,它根本不在您的查询中,我建议您的查询需要:
select m.ProjectName,m.ProjectAuthorName "Project Manager", p.CI,c.resource_tag "Alt CI ID", i.IterationName
from MSP_EpmProject m, Project_CI_Mapping p, ca_owned_resource c, DimTeamProject t, DimIteration i
where i.ProjectGUID = UPPER(CAST(t.ProjectNodeGUID AS NVARCHAR(256)))
and p.CI = c.resource_name
and m.ProjectName = p.ProjectName
and c.resource_tag = t.ProjectNodeName -- NEW Clause
order by m.ProjectName,m.ProjectAuthorName, p.CI,c.resource_tag, i.IterationName
但是,正如我已经说过的,我建议使用ANSI 92显式连接,这样您的查询就会变成:
SELECT m.ProjectName,
m.ProjectAuthorName "Project Manager",
p.CI,c.resource_tag "Alt CI ID",
i.IterationName
FROM MSP_EpmProject m
INNER JOIN Project_CI_Mapping p
ON m.ProjectName = p.ProjectName
INNER JOIN ca_owned_resource c
ON p.CI = c.resource_name
INNER JOIN DimTeamProject t
ON t.ProjectNodeName = c.resource_tag
INNER JOIN DimIteration i
ON i.ProjectGUID = UPPER(CAST(t.ProjectNodeGUID AS NVARCHAR(256)))
ORDER BY m.ProjectName,m.ProjectAuthorName, p.CI,c.resource_tag, i.IterationName;