0
我有兩個表一個是客戶團隊,另一個是CHeader,當我在客戶團隊中輸入記錄時,我應該在CHeader中收集數據(並在7天內提供專欄數據作爲'Ini Test')。如果7天過去了,Cheader數據不是爲那個客戶收集的,那麼我應該可以在查詢輸出中獲得這些結果。注意:只有'Ini Test'類纔是這種情況下的有效數據。以下是查詢。Sql Server從一個表中選擇行,如果在日期範圍內沒有在另一個表中收集日期
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')
是的,但您的答案是不檢查clientTeam +7的開始日期是> currentdatetime。我應該能夠在鎖定開始日期的7天內得到客戶誰在臨牀標題中沒有數據的「Ini Test」 –
那麼你可以自己做,你的解釋不是很清楚。考慮在CreateDate上使用DATEADD函數並將其與GETDATE()進行比較 – Ryan