7

在 Enrollment_Changes 表中,列出的手机型号是订户在该日期更改的手机。

如果 Enrollment_Changes 没有后续更改,则订阅者更改为的电话将列在 P_Enrollment 表中

例如,订阅者 12345678 于 2011 年 1 月 5 日注册了一个 RAZR。2011 年 11 月 1 日,他从 RAZR 更换。您可以在 2012 年 5 月 19 日的 Enrollment_Changes 上看到他在下一个事务中更改的内容。

您如何找到首次注册 iPhone 3 的订阅人数?

这是我用于创建表的代码

创建表:TBL 1

USE [Test2]
GO

/****** Object:  Table [dbo].[P_ENROLLMENT]    ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[P_ENROLLMENT](
    [Subid ] [float] NULL,
    [Enrollment_Date] [datetime] NULL,
    [Channel] [nvarchar](255) NULL,
    [Region] [nvarchar](255) NULL,
    [Active_Status] [float] NULL,
    [Drop_Date] [datetime] NULL,
    [Phone_Model] [nvarchar](255) NULL
) ON [PRIMARY]

GO

待定 2

USE [Test2]
GO

/****** Object:  Table [dbo].[ENROLLMENT_CHANGES]     ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ENROLLMENT_CHANGES](
    [Subid] [float] NULL,
    [Cdate] [datetime] NULL,
    [Phone_Model] [nvarchar](255) NULL
) ON [PRIMARY]

GO

插入 TBL1

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12345678, '2011-01-05 00:00:00', 'Retail', 'Southeast', 1, NULL, 'iPhone 4');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12346178, '2011-03-13 00:00:00', 'Indirect Dealers', 'West', 1, NULL, 'HTC Hero');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12346679, '2011-05-19 00:00:00', 'Indirect Dealers', 'Southeast', 0, '2012-03-15 00:00:00', 'Droid 2');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12347190, '2011-07-25 00:00:00', 'Retail', 'Northeast', 0, '2012-05-21 00:00:00', 'iPhone 4');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12347701, '2011-08-14 00:00:00', 'Indirect Dealers', 'West', 1, NULL, 'HTC Hero');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12348212, '2011-09-30 00:00:00', 'Retail', 'West', 1, NULL, 'Droid 2');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12348723, '2011-10-20 00:00:00', 'Retail', 'Southeast', 1, NULL, 'Southeast');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12349234, '2012-01-06 00:00:00', 'Indirect Dealers', 'West', 0, '2012-02-14 00:00:00', 'West');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12349745, '2012-01-26 00:00:00', 'Retail', 'Northeast', 0, '2012-04-15 00:00:00', 'HTC Hero');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12350256, '2012-02-11 00:00:00', 'Retail', 'Southeast', 1, NULL, 'iPhone 4');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12350767, '2012-03-02 00:00:00', 'Indirect Dealers', 'West', 1, NULL, 'Sidekick');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12351278, '2012-04-18 00:00:00', 'Retail', 'Midwest', 1, NULL, 'iPhone 3');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12351789, '2012-05-08 00:00:00', 'Indirect Dealers', 'West', 0, '2012-07-04 00:00:00', 'iPhone 3');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12352300, '2012-06-24 00:00:00', 'Retail', 'Midwest', 1, NULL, 'Droid 2');

INSERT INTO [P_ENROLLMENT]([Subid ], [Enrollment_Date], [Channel], [Region], [Active_Status], [Drop_Date], [Phone_Model]) 
    VALUES(12352811, '2012-06-25 00:00:00', 'Retail', 'Southeast', 1, NULL, 'Sidekick');

插入 TBL2

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12345678, '2011-11-01 00:00:00', 'RAZR');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12346178, '2012-01-07 00:00:00', 'HTC Hero');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12348723, '2012-01-28 00:00:00', 'RAZR');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12350256, '2012-02-21 00:00:00', 'Blackberry Bold');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12349745, '2012-05-05 00:00:00', 'HTC Hero');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12345678, '2012-05-19 00:00:00', 'Palm Pre');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12347190, '2012-05-20 00:00:00', 'HTC Hero');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12350256, '2012-05-21 00:00:00', 'Blackberry Bold');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12349234, '2012-06-04 00:00:00', 'Palm Pre');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12346178, '2012-06-05 00:00:00', 'iPhone 3');

INSERT INTO [ENROLLMENT_CHANGES]([Subid], [Cdate], [Phone_Model]) 
    VALUES(12350767, '2012-06-10 00:00:00', 'iPhone 3');
4

3 回答 3

4

为了计数

select COUNT(*) Total
from
(
    select e.*,
        rn = row_number() over (partition by e.subid order by c.cdate desc),
        first_model = coalesce(c.phone_model, e.phone_model)
    from [P_ENROLLMENT] e
    left join [ENROLLMENT_CHANGES] c on c.subid = e.subid
) x
where rn=1 and first_model = 'iPhone 3'


对于所有记录

select *
from
(
    select e.*,
        rn = row_number() over (partition by e.subid order by c.cdate desc),
        first_model = coalesce(c.phone_model, e.phone_model)
    from [P_ENROLLMENT] e
    left join [ENROLLMENT_CHANGES] c on c.subid = e.subid
) x
where rn=1 and first_model = 'iPhone 3'
order by subid
于 2012-10-01T00:43:46.930 回答
1

You want to know if the first record in the table is an iPhone 3. Something like this:

select count(*)
from (select e.*,
             row_number() over (partition by subid order by enrollment_date) as seqnum
      from p_enrollment e
     ) e
where seqnum = 1 and phone_model = 'iPhone 3'
于 2012-10-01T00:31:34.953 回答
1

也许我想得太简单了,但是以下任何一个都不能满足您的要求吗?:

SELECT      Phone_Model
            , COUNT(*) AS Initially_Enrolled
FROM        p_enrollment
GROUP BY    Phone_Model

(工作 SQLFiddle:http ://sqlfiddle.com/#!3/68258/4 )

或者

SELECT      COUNT(*) AS Initially_Enrolled
FROM        p_enrollment
WHERE       Phone_Model = 'iPhone 3'

(工作 SQLFiddle:http ://sqlfiddle.com/#!3/68258/3 )

由于您只需要初始注册,因此 ENROLLMENT_CHANGES 表无关紧要。

于 2012-10-01T01:00:48.280 回答