我有两个表,一个是客户团队,另一个是CHeader,当我在客户团队中输入记录时,我应该在CHeader中收集数据(带有种类列数据作为' Ini Test')天。如果7天过去并且没有为该客户收集Cheader数据,那么我应该能够在查询输出中获得这些结果。注意:仅适用于' Ini测试'是这种情况下的有效数据。以下是查询。
CREATE TABLE [dbo].[ClientTeam](
[ClientTeamID] [int] IDENTITY(1,1) NOT NULL,
[TeamID] [int] NOT NULL,
[ClientID] [int] NOT NULL,
[Startdate] [smalldatetime] NOT NULL,
CONSTRAINT [PK15] PRIMARY KEY NONCLUSTERED
(
[ClientTeamID] 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
insert into [ClientTeam] ([TeamID],[ClientID],[Startdate]) values(123,1, GETDATE())
insert into [ClientTeam] ([TeamID],[ClientID],[Startdate]) values(12,23, DATEdiff(day,2,GETDATE()))
insert into [ClientTeam] ([TeamID],[ClientID],[Startdate]) values(12,25, DATEdiff(day,3,GETDATE()))
CREATE TABLE [dbo].[CHeader](
[CHID] [int] IDENTITY(1,1) NOT NULL,
[ClientID] [int] NOT NULL,
[TeamID] [int] NOT NULL,
[CreateDate] [datetime] NOT NULL,
[Kind] [varchar](80) NOT NULL,
CONSTRAINT [PK22] PRIMARY KEY NONCLUSTERED
(
[CHID] 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
insert into [CHeader] ([ClientID],[TeamID],[CreateDate],[Kind]) values(1,123,DATEdiff(HOUR,4,GETDATE()),'Ini Test')
insert into [CHeader] ([ClientID],[TeamID],[CreateDate],[Kind]) values(5,123,DATEdiff(HOUR,4,GETDATE()),'Gen Test')
insert into [CHeader] ([ClientID],[TeamID],[CreateDate],[Kind]) values(6,126,DATEdiff(HOUR,4,GETDATE()),'XYZ')
insert into [CHeader] ([ClientID],[TeamID],[CreateDate],[Kind]) values(23,126,DATEdiff(HOUR,5,GETDATE()),'XYZ')
答案 0 :(得分:0)
根据我的理解,您希望从ClientTeam获取数据,并且只根据提到的标准将其加入CHeader。
我想从ClientTeam中选择并选择左连接到CHeader。你可以加入 C.ClientID = CH.ClientID AND C.StartDate> DATEADD(DAY,-7,CH.CreateDate)和CH.Kind =' Ini Test'