gpt4 book ai didi

sql - 摆脱重复值 - SQL Server

转载 作者:行者123 更新时间:2023-12-02 02:00:18 24 4
gpt4 key购买 nike

我有以下 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)))

什么是合适的解决方案?

谢谢。

最佳答案

您的查询中有一个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;

关于sql - 摆脱重复值 - SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17574004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com