问题
I have a fairly simple join for three tables that is not returning any results. If I modify slightly I get sometime that appears jumbled (same data values in two fields). There is a Documents table from which I need the filename, a Variable table which has the variable definitions and names, and lastly I have a VariableValue table that the values for the variables are stored in. I am trying to get two values for each filename. Subject and Author. Here is the SQL call.
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
If I remove the (VV2.VariableName = N'Subject') I get the following result:
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
If I remove the (VV1.VariableName = N'Author') I get the following result:
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
What I would like is:
Filename Author Subject
ECO-28720.docx billpark LOW PWR BM DUMP
Not sure what subtle thing I am missing?
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
AND
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
回答1:
VariableValue.ValueText AS Author,
VariableValue.ValueText AS Subject
You're calling the exact same alias.columnname and assigning it to both Author and Subject.
回答2:
We need to add projectID, revisionNo and ConfigurationID to the results as the PK for VariableValue is a composite key. If we don't include them, then the results could get mixed up for different projects/revsions or configurations.
So unless you have a rule that says only return the latest revision for a document... we may get multiple records back.
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
回答3:
With a terrific assist from xQbert this code does it.
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
来源:https://stackoverflow.com/questions/46301060/sql-join-not-returning-any-results