从主表中选择全部,加入其他人

时间:2013-10-21 13:00:31

标签: sql sql-server tsql join dynamic

我希望有人可以帮助我。如果我做标准的内部加入或右加入,我得到的结果很少。也很高兴也知道我为什么不会工作。

我有一个主要的成员表。 MemberID对所有表格都是通用的

Select * from apf_members
MemberID--|--AppsTitle--|--AppsFirstName--|--AppsLastName--

2015       Mrs             Naomi            Specter 
2016       Mr              Marisa           Watson 
2025       Mr              Elia             Barker 
2031       Dr              Heth             Rowing 
2044       Ms              Kathryn          McKenzie

我还想附加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       Mrs             Naomi            Specter          f                  nf                  nf
2016       Mr              Marisa           Watson           uf                 NULL                nf
2025       Mr              Elia             Barker          NULL                NULL                NULL
2031       Dr              Heth             Rowing          co                  exp                 f
2044       Ms              Kathryn          McKenzie        NULL                f                   NULL

* 更新22/10/2013 *

这是一个应该重新创建表的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');

1 个答案:

答案 0 :(得分:1)

这是一个Northwind示例。

您可以使用派生表“过滤”。 然后在派生表上保持联接。

你可以写你的,你将有3个派生表。

derivedCurrentMember derivedUnrenewedMember derivedLapsedMember

将过滤/逻辑放在每个派生表中。然后在每一个上加入。

这是一个通用的Northwind示例。

我的“过滤逻辑”是否存在折扣。 (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 

::: APPEND :::

这是第一个。 你可以填写剩下的部分。

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