我有两个表,我正在努力编写一个生成我需要的结果的查询。
表格1
CREATE TABLE [Table 1](
[ID] [int] NOT NULL,
[Active_Status] [char](1) NOT NULL,
[Status Change Date] [date] NOT NULL
)
INSERT INTO [Table 1] VALUES (1,'Y','2000-01-15')
INSERT INTO [Table 1] VALUES (1,'N','2003-01-20')
INSERT INTO [Table 1] VALUES (2,'N','2002-01-25')
INSERT INTO [Table 1] VALUES (2,'Y','2003-01-15')
INSERT INTO [Table 1] VALUES (2,'N','2010-01-20')
INSERT INTO [Table 1] VALUES (3,'Y','2005-01-25')
INSERT INTO [Table 1] VALUES (3,'Y','2007-01-20')
INSERT INTO [Table 1] VALUES (3,'N','2011-01-15')
表 2
CREATE TABLE [Table 2](
[ID] [int] NOT NULL,
[Decision] [varchar](4) NOT NULL,
[Decision Change Date] [date] NOT NULL
)
INSERT INTO [Table 2] VALUES (1,'BUY' ,'2000-05-15')
INSERT INTO [Table 2] VALUES (1,'SELL','2010-05-20')
INSERT INTO [Table 2] VALUES (1,'SELL','2012-05-25')
INSERT INTO [Table 2] VALUES (2,'HOLD','2004-05-15')
INSERT INTO [Table 2] VALUES (2,'BUY' ,'2011-05-10')
INSERT INTO [Table 2] VALUES (3,'SELL','2008-05-15')
INSERT INTO [Table 2] VALUES (3,'BUY' ,'2011-05-25')
我想要的输出
首先,我需要按ID
and对结果表进行排序Decision Change Date
。随后我需要查找Active_Status
相应的Decision Change Date
.
同样,我需要显示上一时期的Active_Status
and Decision
。