使用复合键表加入和/或子查询

时间:2017-09-28 18:05:10

标签: sql sql-server subquery sql-server-2014 composite-primary-key

几天前我问了一个类似的问题。我有三张桌子。每个表的DDL都在此线程中找到(SQL JOIN not returning any results)。我的需求略有改变(这是我震惊的表情),我在获得我需要的东西方面没有取得多大成功;关闭,但不完全。

我想要做的是提交用户名(或名称的一部分,即LIKE)并返回Description(Variable.Value表)和文档文件名(Documents表)。最新修订版。我这样做是为了获得最新的转速并取得巨大成功。我已经尝试了无数种方法来插入作者名称(variablevalue表)以获得我所追求的内容。我尝试了一个没有运气的子查询。我认为复合键正在抓住我,因为我还没有完成复合键的大量工作。 TIA。

Select Distinct vv.ValueCache 
from VariableValue vv, Documents d
where d.filename like 'ECO-%' and d.deleted = 0 and  d.DocumentID = vv.DocumentID and
vv.VariableID =  ( Select VariableID
                   from Variable
                   where VariableName like 'Description') and 
vv.RevisionNo = ( Select Max( Vv1.RevisionNo )
                  From VariableValue vv1 
                  where vv1.VariableID = vv.VariableID and
                        vv1.DocumentID = d.DocumentID and
                        vv1.ValueText is not null ) and
vv.ValueCache != ''
order by vv.ValueCache ASC

这是Documents表缺少的DDL。

USE [PDM_NT_Vault]
GO

/****** Object:  Table [dbo].[Documents]    Script Date: 9/29/2017 9:51:40 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Documents](
    [DocumentID] [int] IDENTITY(1,1) NOT NULL,
    [Filename] [nvarchar](255) NULL,
    [LockProject] [int] NULL CONSTRAINT [DF__TemporaryU__Mode__3B75D760]  DEFAULT ((2)),
    [UserID] [int] NULL CONSTRAINT [DF__Temporary__UserI__3C69FB99]  DEFAULT ((0)),
    [LockDomain] [nvarchar](255) NULL,
    [LockPath] [nvarchar](255) NULL,
    [Busy] [bit] NOT NULL CONSTRAINT [DF__TemporaryU__Busy__3E52440B]  DEFAULT ((0)),
    [Flushed] [int] NULL CONSTRAINT [DF_Documents_Flushed]  DEFAULT ((1)),
    [DefValStored] [int] NULL CONSTRAINT [DF_Documents_DefValStored]  DEFAULT ((0)),
    [RevGenCounter] [int] NULL,
    [LatestRevisionNo] [int] NULL,
    [CurrentStatusID] [int] NULL,
    [WorkingVersionModified] [int] NULL,
    [ExtensionID] [int] NULL CONSTRAINT [DF_Documents_ExtensionID]  DEFAULT ((1)),
    [LockDate] [datetime] NULL,
    [UserDocRefsModified] [bit] NULL,
    [Deleted] [bit] NOT NULL CONSTRAINT [DF_Documents_Deleted]  DEFAULT ((0)),
    [Shared] [int] NULL CONSTRAINT [DF_Documents_Shared]  DEFAULT ((0)),
    [LockViewID] [uniqueidentifier] NULL,
    [Link] [bit] NULL CONSTRAINT [DF_Documents_Link]  DEFAULT ((0)),
    [DocTypeID] [int] NULL,
    [ObjectTypeID] [int] NULL CONSTRAINT [DF_Documents_ObjectTypeID]  DEFAULT ((1)),
    [Flags] [int] NOT NULL CONSTRAINT [DF_Documents_Flags]  DEFAULT ((0)),
 CONSTRAINT [aaaaaDocuments1_PK] PRIMARY KEY CLUSTERED 
(
    [DocumentID] 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]

GO

ALTER TABLE [dbo].[Documents]  WITH NOCHECK ADD  CONSTRAINT [Documents_FK00] FOREIGN KEY([UserID])
REFERENCES [dbo].[Users] ([UserID])
GO

ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [Documents_FK00]
GO

ALTER TABLE [dbo].[Documents]  WITH NOCHECK ADD  CONSTRAINT [FK_Documents_DocType] FOREIGN KEY([DocTypeID])
REFERENCES [dbo].[DocType] ([DocTypeID])
GO

ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_DocType]
GO

ALTER TABLE [dbo].[Documents]  WITH NOCHECK ADD  CONSTRAINT [FK_Documents_FileExtension] FOREIGN KEY([ExtensionID])
REFERENCES [dbo].[FileExtension] ([ExtensionID])
GO

ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_FileExtension]
GO

ALTER TABLE [dbo].[Documents]  WITH NOCHECK ADD  CONSTRAINT [FK_Documents_ObjectType] FOREIGN KEY([ObjectTypeID])
REFERENCES [dbo].[ObjectType] ([ObjectTypeID])
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_ObjectType]
GO

ALTER TABLE [dbo].[Documents]  WITH CHECK ADD  CONSTRAINT [FK_Documents_Projects1] FOREIGN KEY([LockProject])
REFERENCES [dbo].[Projects] ([ProjectID])
GO

ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_Projects1]
GO

0 个答案:

没有答案