我希望有人能帮助我。如果我做一个标准的内连接或右连接,我得到的结果很少。也很高兴知道为什么我正在尝试的东西不起作用。
我有一个主要的成员表。MemberID 对所有表都是通用的
Select * from apf_members
MemberID--|--AppsTitle--|--AppsFirstName--|--AppsLastName-- 2015 娜奥米·斯佩克特夫人 2016 玛丽莎·沃森先生 2025 伊利亚·巴克先生 2031 博士赫斯赛艇 第2044章
我还想附加 3 个额外的动态构建的列。
--CurrentMember--|--UnrenewedMember--|--LapsedMember--
下面的查询当前将只返回值 ~IF~ 用户有记录。我需要它返回 NULL 或空字符串。
@theDate 是一个变量,现在将其分配给 Getdate。
Declare @theDate date
SET @theDate = GetDate()
Select Description as 'CurrentMember' from apf_finances
where @theDate between StartDate and EndDate
and Status = 'Financial Status'
Select Description as 'UnrenewedMember'
from apf_finances
where @theDate between DATEADD(year, -1, StartDate) and DATEADD(year, -1, EndDate)
and Status = 'Financial Status'
Select Description as 'LapsedMember'
from apf_finances
where @theDate between DATEADD(year, -2, StartDate) and DATEADD(year, -2, EndDate)
and Status = 'Financial Status'
放在一起,最终的结果应该是这样的。
MemberID--|--AppsTitle--|--AppsFirstName--|--AppsLastName--| --CurrentMember--|--UnrenewedMember--|--LapsedMember-- 2015 Naomi Spectre 夫人 f nf nf 2016 玛丽莎·沃森先生 uf NULL nf 2025 伊利亚·巴克先生 NULL NULL NULL 2031 Heth Rowing 博士合作 exp f 第2044章
* 2013 年 10 月 22 日更新 *
这是一个应该重新创建表的 SQL 脚本
CREATE TABLE [dbo].[apf_Members](
[MemberID] [int] IDENTITY(1,1) NOT NULL,
[AppsTitle] [nvarchar](50) NULL,
[AppsFirstName] [nvarchar](50) NULL,
[AppsMiddleName] [nvarchar](50) NULL,
[AppsLastName] [nvarchar](50) NULL,
)
GO
CREATE TABLE [dbo].[apf_Finances](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MemberID] [int] NOT NULL,
[Description] [nvarchar](50) NULL,
[StartDate] [date] NULL,
[EndDate] [date] NULL,
[Status] [nvarchar](50) NULL
)
GO
SET IDENTITY_INSERT dbo.[apf_Members] ON
GO
SET IDENTITY_INSERT dbo.[apf_Finances] ON
GO
INSERT INTO [dbo].[apf_Members]
([MemberID],[AppsTitle],[AppsFirstName],[AppsLastName])
VALUES (2015,'Mrs','Naomi', 'Specter' ),
(2016 , 'Mr' ,'Marisa','Watson' ),
(2025 , 'Mr' ,'Elia','Barker'),
(2031 , 'Dr','Heth','Rowing'),
(2044 , 'Ms','Kathryn','McKenzie');
INSERT INTO [apf_Finances]
([ID], [MemberID], [Description], [StartDate], [EndDate], [Status] )
VALUES
(12381, 2016, 'f' ,'2013-10-15','2014-10-14','Financial Status'),
(12382, 2016, '' ,'2013-10-15','2014-10-14','Donation'),
(12361, 2025, 'f' ,'2013-10-12','2014-10-11','Financial Status'),
(12362, 2025, '' ,'2013-10-12','2014-10-11','Donation'),
(12357, 2031, 'f' ,'2013-10-11','2014-10-10','Financial Status'),
(12358, 2031, '' ,'2013-10-11','2014-10-10','Donation'),
(12379, 2044, 'f' ,'2012-10-21','2013-10-20','Financial Status'),
(12380, 2044, '' ,'2012-10-21','2013-10-20','Donation'),
(12377, 2016, 'f' ,'2012-10-17','2013-10-16','Financial Status'),
(12378, 2016, '' ,'2012-10-17','2013-10-16','Donation'),
(12373, 2025, 'f' ,'2012-10-16','2013-10-15','Financial Status'),
(12374, 2031, '' ,'2011-10-16','2013-10-15','Donation'),
(12375, 2031, 'f' ,'2011-10-16','2013-10-15','Financial Status'),
(12376, 2044, '' ,'2011-10-16','2013-10-15','Donation'),
(12371, 2044, 'f' ,'2011-10-15','2013-10-14','Financial Status');