检索并计算每个银行用户的单次出现次数

时间:2017-12-11 15:32:34

标签: sql sql-server sql-server-2008

我正在努力解决我面临的某个问题。道歉如果我不以错误的方式表达它,我就是Stackoverflow的新手。

我有一个报告,该报告应该是唯一的用户数,创建的帐户数,激活的帐户数以及尚未激活的帐户数。

我能够获得这些(创建的帐户数量,激活的帐户数量以及尚未激活的帐户数量),但无法实现前者。

这是我的脚本片段。

SELECT Bank,Users, Added_Accounts FROM (
   select BANK_NAME as Bank, COUNT(DISTINCT USER_ID) as 'Users',
      COUNT(DISTINCT USER_ID+BANK_NAME+ACCOUNT_NUMBER) as Added_Accounts
   from MA
   where MOBILE_PROFILE_CREATION between @datefrom and @dateto 
      and USER_ID IS NOT NULL AND USER_ID !='' and Bank_Name NOT LIKE '%XXX%' 
   group by BANK_NAME

   UNION ALL

    select 'Total' as Bank, COUNT(DISTINCT USER_ID+BANK_NAME) as 'Users', 
       COUNT(DISTINCT USER_ID+BANK_NAME+ACCOUNT_NUMBER) as Added_Accounts
    from MA
    where MOBILE_PROFILE_CREATION between @datefrom and @dateto
       and USER_ID IS NOT NULL AND USER_ID !='' and Bank_Name NOT LIKE '%XXX%'

) as WeekData_UsersByBanks

这将获得每个银行的用户数量,但我想要实现的仅仅是第一次出现的用户被检索和计数,例如如果用户在3个银行中拥有帐户,我只想要检索和计算第一个匹配项。

有没有人知道如何实现这一目标?

提前致谢。

更新

DDL
CREATE TABLE [dbo].[MA](
    [USER_ID] [nvarchar](255) NULL,
    [ACCOUNT_NUMBER] [nvarchar](255) NULL,
    [BANK_CODE] [nvarchar](255) NULL,
    [BANK_NAME] [nvarchar](255) NULL,
    [MOBILEPROFILEACTIVE] [float] NULL,
    [MOBILE_PROFILE_CREATION] [nvarchar](255) NULL,
    [USERSTATUS] [nvarchar](255) NULL
) ON [PRIMARY]

INSERT INTO MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],    
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES    
('abc@qwerty.com','009****220','63','Bank A','1','2017-08-07 00:00','N')
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES 
('abc@qwerty.com','005****017','63','Bank A','0','2017-08-07 00:00','N')
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES 
('abc@qwerty.com','007****559','63','Bank A','0','2017-08-07 00:00','N')
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES 
('abc@qwerty.com','302****187','76','Bank E','1','2017-08-07 00:00','N')
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES 
('abc@qwerty.com','207****421','33','Bank F','0','2017-08-07 00:00','N')
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES 
('def@ghi.com','005****047','63','Bank A','0','2017-09-09 00:00','N')
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES 
('def@ghi.com','237****275','50','Bank B','0','2017-09-09 00:00','N')
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES 
('def@ghi.com','208****712','57','Bank G','1','2017-09-09 00:00','N')
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES 
('poi.o@plm.com','075****022','214','Bank D','1','2017-08-03 00:00','N')
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES         
('qaz.wsx@mmm.com','601****999','70','Bank C','1','2017-03-15 00:00','N')  
insert into MA ([USER_ID],[ACCOUNT_NUMBER],[BANK_CODE],[BANK_NAME],
[MOBILEPROFILEACTIVE],[MOBILE_PROFILE_CREATION],[USERSTATUS]) VALUES 
('qwe@rty.com','204****889','33','Bank F','0','2017-04-21 00:00','N')

这是我得到的SQL Fiddle结果。

在小提琴中,用户abc@qwerty.com在3家银行(银行A,银行E和银行F)拥有账户

我希望用户出现在银行的第一次出现时,而不是用户出现在所有银行计数中。

我希望这更清楚。

2 个答案:

答案 0 :(得分:0)

我已经弄清楚如何通过银行获得第一次出现的用户。我只是使用下面的代码段。

select user_id email, min(bank_name) as bank from ma
group by user_id;

以下是结果的SQL fiddle

答案 1 :(得分:0)

请参阅sqlfiddle http://www.sqlfiddle.com/#!6/6a5d4b/37/1

SELECT BANK
    , SUM(CASE WHEN RNUM = 1 THEN 1 ELSE 0 END) AS Users
    , MIN(Added_Accounts) AS Added_ACCOUNTS
    , MIN(Activated_accounts) as Activated_accounts
FROM (
    select BANK_NAME as Bank
        , COUNT(ACCOUNT_NUMBER) OVER(PARTITION BY BANK_NAME) as Added_Accounts
        , ROW_NUMBER() OVER (PARTITION BY USER_ID ORDER BY BANK_NAME) AS RNUM
        , SUM(MOBILEPROFILEACTIVE) OVER(PARTITION BY BANK_NAME) as Activated_Accounts

    from MA
    where MOBILE_PROFILE_CREATION between @datefrom and @dateto 
    and USER_ID IS NOT NULL AND USER_ID !='' and Bank_Name NOT LIKE '%XXX%' 

) as WeekData_UsersByBanks
GROUP BY BANK

UNION ALL
select 'Total' as Bank
    , COUNT(DISTINCT USER_ID) AS Users
    , COUNT(DISTINCT USER_ID+BANK_NAME+ACCOUNT_NUMBER) as Added_Accounts
    , SUM(MOBILEPROFILEACTIVE) 
from MA
where MOBILE_PROFILE_CREATION between @datefrom and @dateto
and USER_ID IS NOT NULL AND USER_ID !='' and Bank_Name NOT LIKE '%XXX%'