创建多个自定义聚合函数

时间:2016-06-04 01:02:41

标签: sql tsql group-by aggregate-functions

我将此表命名为Account。

该表的简化视图如下:

acct_num | ssn       | branch | open_date    |close_date  | has_product1 |  has_product2      
----------------------------------------------------------------------------------------
0123456  | 123456789 | 01     | 01/01/2000   | NULL       | 1            |  0
0123457  | 123456789 | 02     | 03/05/2004   | NULL       | 0            |  1
1234405  | 322145678 | 04     | 04/16/2016   | 05/01/2016 | 1            |  1
...

注意ssn 123456789有2个帐号。

我需要创建一个新的数据集,按acct_num对表进行分组,并显示基于每个组中的行计算的新列。

但这些计算本质上是多种多样的。

我需要的表格(在此简化示例中)如下:

ssn       |  home_branch    | date_of_first_membership   |   eligibility_indicator
-----------------------------------------------------------------------------------

显然ssn很容易,但其余的都超出了我的目标。

  • home branchbranch来自最早open_date和非空close_date的行的值。

  • open_date只是该群组中的最低open_date值。

  • 如果至少有1个开户eligibility_status且至少有1个(可能不同)开户has_product1

    ,则
  • has_product2为1

因此,我期望从上面的例子中得到的结果集是:

ssn       | home_branch     | date_of_first_membership   | eligibility_indicator
-----------------------------------------------------------------------------------
123456789 | 01              | 01/01/2000                 | 1
322145678 | 04              | 04/16/2016                 | 0

修改

这些评论指出了一个矛盾。为了解决这个矛盾,我现在想要过滤掉所有没有开户的ssn。

因此,新的预期结果集是:

ssn       | home_branch     | date_of_first_membership   | eligibility_indicator
-----------------------------------------------------------------------------------
123456789 | 01              | 01/01/2000                 | 1

2 个答案:

答案 0 :(得分:1)

您可以使用条件聚合执行此操作。第一个计算需要一些技巧 - 获得没有关闭日期的行的最小日期:

select ssn,
       max(case when open_date = min_open_date then branch end) as home_branch,
       min(open_date) as date_of_first_membership,
       (case when max(has_product1) > 0 and max(has_product2) > 0
             then 1 else 0
        end) as eligibility_indicator
from (select a.*,
             min(case when close_date is null then open_date end)  over (partition by ssn ) as min_opendate
      from account a
     ) a
group by ssn;

答案 1 :(得分:0)

在2008 sql server中测试

create table account (
    acct_num varchar(15), 
    ssn int, 
    branch varchar(10), 
    open_date Date, 
    close_date Date, 
    has_product1 int, 
    has_product2 int, 
)
insert into account
values (0123456,123456789,01,'01/01/2000',null, 1,0),
(0123457,123456789,02,'03/05/2004',null, 0,1),
(1234405,322145678 ,04,'04/16/2016','05/01/2016', 1,1)


select *, (select branch from account where open_date = x.date_of_first_membership and ssn = x.ssn) home_branch from (
select ssn, MIN(open_date) date_of_first_membership,
case when close_date is not null then 0 ELSE
case when MAX(has_product1) > 0 and MAX(has_product2) >0 then 1 ELSE 0 end end eligibility_indicator
  from account
  where close_date is null
   group by ssn, close_date
  ) x