SQL JOIN没有返回任何结果

时间:2017-09-19 12:55:14

标签: sql-server inner-join

对于没有返回任何结果的三个表,我有一个相当简单的连接。如果我稍微修改,我会得到一些混乱(两个字段中相同的数据值)。有一个Documents表,我需要文件名,一个变量表,它有变量的定义和名称,最后我有一个VariableValue表,存储变量的值。我试图为每个文件名获取两个值。主题和作者。这是SQL调用。

    SELECT DISTINCT Documents.Filename, VariableValue.ValueText AS Author,
    VariableValue.ValueText AS Subject 
    FROM Documents INNER JOIN
    VariableValue ON Documents.DocumentID = VariableValue.DocumentID INNER JOIN
    Variable AS VV1 ON VariableValue.VariableID = VV1.VariableID INNER JOIN
    Variable AS VV2 ON VariableValue.VariableID = VV2.VariableID

    WHERE (Documents.Filename LIKE N'ECO-%') AND (Documents.Deleted = 0) AND 
    (VV1.VariableName = N'Author') AND (VariableValue.ValueText <> '-') AND 
    (VV2.VariableName = N'Subject')
    ORDER BY Author asc

如果我删除了(VV2.VariableName = N&#39; Subject&#39;),我会得到以下结果:

Filename         Author     Subject
ECO-27533.docx  billpark    billpark
ECO-27630.docx  billpark    billpark
ECO-27683.docx  billpark    billpark
ECO-27790.docx  billpark    billpark
ECO-27812.docx  billpark    billpark
ECO-27975.docx  billpark    billpark

如果我删除(VV1.VariableName = N&#39;作者&#39;),我会得到以下结果:

Filename        Author  Subject
ECO-28720.docx  24006   LOW PWR BM DUMP 24006 LOW PWR BM DUMP
ECO-28595.docx  24052   PIN THREAD CORRECTION   24052 PIN THREAD CORRECTION
ECO-28517.docx  24087   24087
ECO-28791.docx  25584   REV A TO B  25584 REV A TO B
ECO-28714.docx  25873   PRESSURE RELEAF ASSY    25873 PRESSURE RELEAF ASSY

我想要的是:

Filename        Author       Subject
ECO-28720.docx  billpark     LOW PWR BM DUMP

不确定我遗失了哪些微妙的东西?

TIA。

DDL

CREATE TABLE [dbo].[Variable](
    [VariableID] [int] IDENTITY(1,1) NOT NULL,
    [VariableName] [nvarchar](255) NOT NULL,
    [VariableType] [int] NOT NULL,
    [IsDeleted] [bit] NOT NULL,
    [FlagUnique] [bit] NOT NULL,
    [FlagMandatory] [bit] NOT NULL,
    [FlagFreeUpdateAllVersion] [bit] NOT NULL CONSTRAINT [DF__Variable__FlagFr__05EEBAAE]  DEFAULT ((0)),
    [FlagFreeUpdateLatestVersion] [bit] NOT NULL CONSTRAINT [DF__Variable__FlagFr__06E2DEE7]  DEFAULT ((0)),
 CONSTRAINT [PK_Variable] PRIMARY KEY CLUSTERED 
(
    [VariableID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[VariableValue](
    [VariableID] [int] NOT NULL,
    [DocumentID] [int] NOT NULL,
    [ProjectID] [int] NOT NULL,
    [RevisionNo] [int] NOT NULL,
    [ConfigurationID] [int] NOT NULL,
    [ValueText] [nvarchar](max) NOT NULL CONSTRAINT [DF_VariableValue_ValueText]  DEFAULT (N''),
    [ValueInt] [int] NULL,
    [ValueFloat] [float] NULL,
    [ValueDate] [datetime] NULL,
    [ValueCache] [nvarchar](64) NOT NULL CONSTRAINT [DF_VariableValue_ValueCache]  DEFAULT (''),
    [IsLongText] [bit] NOT NULL CONSTRAINT [DF_VariableValue_IsLongText]  DEFAULT ((0)),
 CONSTRAINT [PK_VariableValue] PRIMARY KEY CLUSTERED 
(
    [VariableID] ASC,
    [DocumentID] ASC,
    [ProjectID] ASC,
    [RevisionNo] ASC,
    [ConfigurationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[VariableValue]  WITH NOCHECK ADD  CONSTRAINT [FK_VariableValue_DocumentConfiguration] FOREIGN KEY([ConfigurationID])
REFERENCES [dbo].[DocumentConfiguration] ([ConfigurationID])
GO

ALTER TABLE [dbo].[VariableValue] CHECK CONSTRAINT [FK_VariableValue_DocumentConfiguration]
GO

ALTER TABLE [dbo].[VariableValue]  WITH NOCHECK ADD  CONSTRAINT [FK_VariableValue_Documents] FOREIGN KEY([DocumentID])
REFERENCES [dbo].[Documents] ([DocumentID])
GO

ALTER TABLE [dbo].[VariableValue] CHECK CONSTRAINT [FK_VariableValue_Documents]
GO

ALTER TABLE [dbo].[VariableValue]  WITH CHECK ADD  CONSTRAINT [FK_VariableValue_Projects] FOREIGN KEY([ProjectID])
REFERENCES [dbo].[Projects] ([ProjectID])
GO

ALTER TABLE [dbo].[VariableValue] CHECK CONSTRAINT [FK_VariableValue_Projects]
GO

ALTER TABLE [dbo].[VariableValue]  WITH NOCHECK ADD  CONSTRAINT [FK_VariableValue_Variable] FOREIGN KEY([VariableID])
REFERENCES [dbo].[Variable] ([VariableID])
GO

ALTER TABLE [dbo].[VariableValue] CHECK CONSTRAINT [FK_VariableValue_Variable]
GO

3 个答案:

答案 0 :(得分:0)

get-process | format-table @{name='VM(MB)';width=50;expression={$_.VM};alignment='right'}, name

您正在调用完全相同的alias.columnname,并将其分配给作者和主题。

答案 1 :(得分:0)

我们需要将projectID,revisionNo和ConfigurationID添加到结果中,因为VariableValue的PK是一个复合键。如果我们不包含它们,那么结果可能会因不同的项目/转换或配置而混淆。

因此,除非您的规则只返回文档的最新修订版...我们可能会收到多条记录。

SELECT Documents.Filename
     , VariableValue.ProjectID
     , VariableValue.RevisionNo
     , VariableValue.ConfigurationID
     , max(Case when VV1.VariableName = N'Author' then VariableValue.ValueText END) as Author
     , max(Case when VV1.VariableName = N'Subject' then VariableValue.ValueText END)  AS Subject 
FROM Documents 
INNER JOIN VariableValue 
   ON Documents.DocumentID = VariableValue.DocumentID 
INNER JOIN Variable AS VV1 
   ON VariableValue.VariableID = VV1.VariableID 
WHERE (Documents.Filename LIKE N'ECO-%') 
  AND (Documents.Deleted = 0) 
  AND (VariableValue.ValueText <> '-') 
  AND (VV1.VariableName in (N'Author',N'Subject') 
GROUP BY Documents.Filename
     , VariableValue.ProjectID
     , VariableValue.RevisionNo
     , VariableValue.ConfigurationID

ORDER BY Author asc

答案 2 :(得分:0)

有了xQbert的极好帮助,这段代码就可以了。

SELECT DISTINCT Documents.Filename, max(Case when VV1.VariableName = N'Author' then VariableValue.ValueText END) as Author, 
                                    max(Case when VV1.VariableName = N'Subject' then VariableValue.ValueText END) AS Description 
FROM Documents INNER JOIN
                         VariableValue ON Documents.DocumentID = VariableValue.DocumentID INNER JOIN
                         Variable AS VV1 ON VariableValue.VariableID = VV1.VariableID INNER JOIN
                         Variable AS VV2 ON VariableValue.VariableID = VV2.VariableID
WHERE (Documents.Filename LIKE N'ECO-%') AND (Documents.Deleted = 0) AND  (VV1.VariableName in (N'Author',N'Subject') AND (VariableValue.ValueText <> '-'))
Group BY Documents.Filename
ORDER BY Author asc