递归分组和 count/max 给定日期时间间隔的记录
recusively group and count/max records for a given datetime interval
我打开了另一个问题,但希望这个问题更清楚并且针对我需要帮助的内容。
示例数据:(SQL Fiddle link 包含在下面)
groupid custid cust_type cust_date data_total_1 data_total_2
CA123 ABC12345 SLE January, 01 2014 5 10
CA123 ABC12345 SLE February, 01 2014 2 5
CA123 ABC12345 SLE March, 01 2014 7 11
CA123 ABC12345 SLE April, 01 2014 7 4
FL444 BBB22222 SLE January, 01 2014 2 3
FL444 BBB22222 SLE March, 01 2014 7 21
FL444 BBB22222 SLE July, 01 2014 3 9
WA999 ZZZ99909 NSLE April, 01 2014 2 10
WA999 ZZZ99909 NSLE May, 01 2014 4 9
对于每个给定的 groupid、custid、cust_type 组合,我需要在给定的时间间隔(3 个月)内抓取评估记录。我需要计算记录数并获取每个记录的 "range" 中存在的最大 data_total_x 值。
我的预期输出与此类似:
groupid custid cust_type cust_date custid_count max_data_total_1 max_data_total_2
CA123 ABC12345 SLE January, 01 2014 4 7 11
CA123 ABC12345 SLE February, 01 2014 3 7 11
CA123 ABC12345 SLE March, 01 2014 2 7 11
CA123 ABC12345 SLE April, 01 2014 1 7 4
FL444 BBB22222 SLE January, 01 2014 2 7 21
FL444 BBB22222 SLE March, 01 2014 1 7 21
FL444 BBB22222 SLE July, 01 2014 1 3 9
WA999 ZZZ99909 NSLE April, 01 2014 2 4 10
WA999 ZZZ99909 NSLE May, 01 2014 1 4 9
SQL Fiddle 包括示例数据和我的尝试:
http://sqlfiddle.com/#!6/ba5a53/10/0
如有任何帮助,我们将不胜感激。
我认为应该这样做:
select
groupid,
custid,
cust_type,
f.custid_count,
f.max_data_total_1,
f.max_data_total_2
from records r
outer apply (
select
count(*) as custid_count,
max(data_total_1) as max_data_total_1,
max(data_total_2) as max_data_total_2
from
records r2
where
r.groupid = r2.groupid and
r.custid = r2.custid and
r.cust_type = r2.cust_type and
r2.cust_date <= dateadd(month, 3, r.cust_date) and
r2.cust_date >= r.cust_date
) f
SQL Fiddle: http://sqlfiddle.com/#!6/ba5a53/14
我打开了另一个问题,但希望这个问题更清楚并且针对我需要帮助的内容。
示例数据:(SQL Fiddle link 包含在下面)
groupid custid cust_type cust_date data_total_1 data_total_2
CA123 ABC12345 SLE January, 01 2014 5 10
CA123 ABC12345 SLE February, 01 2014 2 5
CA123 ABC12345 SLE March, 01 2014 7 11
CA123 ABC12345 SLE April, 01 2014 7 4
FL444 BBB22222 SLE January, 01 2014 2 3
FL444 BBB22222 SLE March, 01 2014 7 21
FL444 BBB22222 SLE July, 01 2014 3 9
WA999 ZZZ99909 NSLE April, 01 2014 2 10
WA999 ZZZ99909 NSLE May, 01 2014 4 9
对于每个给定的 groupid、custid、cust_type 组合,我需要在给定的时间间隔(3 个月)内抓取评估记录。我需要计算记录数并获取每个记录的 "range" 中存在的最大 data_total_x 值。
我的预期输出与此类似:
groupid custid cust_type cust_date custid_count max_data_total_1 max_data_total_2
CA123 ABC12345 SLE January, 01 2014 4 7 11
CA123 ABC12345 SLE February, 01 2014 3 7 11
CA123 ABC12345 SLE March, 01 2014 2 7 11
CA123 ABC12345 SLE April, 01 2014 1 7 4
FL444 BBB22222 SLE January, 01 2014 2 7 21
FL444 BBB22222 SLE March, 01 2014 1 7 21
FL444 BBB22222 SLE July, 01 2014 1 3 9
WA999 ZZZ99909 NSLE April, 01 2014 2 4 10
WA999 ZZZ99909 NSLE May, 01 2014 1 4 9
SQL Fiddle 包括示例数据和我的尝试: http://sqlfiddle.com/#!6/ba5a53/10/0
如有任何帮助,我们将不胜感激。
我认为应该这样做:
select
groupid,
custid,
cust_type,
f.custid_count,
f.max_data_total_1,
f.max_data_total_2
from records r
outer apply (
select
count(*) as custid_count,
max(data_total_1) as max_data_total_1,
max(data_total_2) as max_data_total_2
from
records r2
where
r.groupid = r2.groupid and
r.custid = r2.custid and
r.cust_type = r2.cust_type and
r2.cust_date <= dateadd(month, 3, r.cust_date) and
r2.cust_date >= r.cust_date
) f
SQL Fiddle: http://sqlfiddle.com/#!6/ba5a53/14