0

我希望有人能帮助我。如果我做一个标准的内连接或右连接,我得到的结果很少。也很高兴知道为什么我正在尝试的东西不起作用。

我有一个主要的成员表。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');
4

1 回答 1

1

这是一个罗斯文的例子。

您可以使用派生表“过滤”。然后在派生表上左连接。

您可以编写自己的,您将拥有 3 个派生表。

derivedCurrentMemberderivedUnrenewedMemberderivedLapsedMember

将您的过滤/逻辑放在每个派生表中。然后左加入每个。

这是一个通用的罗斯文示例。

我的“过滤逻辑”是折扣是否存在。(innerOd1.Discount <=0 和 innerOd1.Discount > 0)。您可能不需要分组依据。

Use Northwind
GO


Select ords.OrderID 
 , ISNULL ( derived1.MyCount , 0)  as NoDiscountCount
 , ISNULL ( derived2.MyCount , 0)  as HasDiscountCount
from dbo.Orders ords

left join
( select innerOd1.OrderID , count (*) as MyCount from dbo.[Order Details] innerOd1 where innerOd1.Discount <=0 group by innerOd1.OrderID) derived1
on ords.OrderID = derived1.OrderID

left join
( select innerOd2.OrderID , count (*) as MyCount from dbo.[Order Details] innerOd2 where innerOd2.Discount > 0 group by innerOd2.OrderID) derived2
on ords.OrderID = derived2.OrderID

order by ords.OrderID 

::: 追加 :::

这是第一个。您可以填写其余部分。

declare @theDate datetime
select @theDate = getdate()


select membs.* , currentMemberDerived.[CurrentMember]  from
[dbo].[apf_Members] membs
left join 
(
Select MemberID , [Description] as 'CurrentMember' from apf_finances 
where @theDate between StartDate and EndDate
and Status = 'Financial Status'
) currentMemberDerived
on membs.MemberID = currentMemberDerived.MemberID
于 2013-10-21T14:20:57.060 回答