具有日期计算功能的SQL Server PARTITION BY

时间:2018-07-24 16:19:34

标签: sql-server partition-by

am试图运行一些需要PARTITION BY但无法解决所需复杂性的SQL。简化了以下数据,但想法是使用表的前三列查找第4列(我已手动添加了值),同时还显示了表中的其他列。

对于表中的每个客户,只要该客户记录的查询日期小于,则需要计算当前记录的查询日期之前的天数前28天,否则显示NULL。还需要考虑可通过“查询”列中的值较小的关系打破关系-将较小的值计为第一。

Customer Enquiry EnquiryDate   DaysSinceLastEnquiryForCustomer
522181   232853  19/01/2014    NULL
522181   234750  30/01/2014    11
522181   235141  03/02/2014    4
522181   235210  03/02/2014    4
522181   262015  23/09/2014    NULL
522181   263942  09/10/2014    16
522181   265583  22/10/2014    13
522181   311345  01/10/2015    NULL
522181   321938  31/12/2015    NULL
522181   322404  31/12/2015    0
522181   328057  27/01/2016    23
522181   329164  02/02/2016    6
522181   329426  03/02/2016    1
522181   336409  17/03/2016    14
522181   336581  18/03/2016    1
522181   337003  22/03/2016    4
522181   343338  15/05/2016    NULL
522181   344185  23/05/2016    8
522181   352323  06/08/2016    14

预先感谢

1 个答案:

答案 0 :(得分:0)

不确定我了解该逻辑如何产生这些结果。但是,这里有一个示例可供使用:

在SSMS(或VS)中请注意,如果按住Shift + Alt和向上/向下箭头,则会出现“垂直选择”,您可以在其中垂直输入多个列中的相同值。因此,可以轻松地将上面的固定宽度表转换为INSERT .. VALUES查询。

use tempdb
go

drop table if exists C
create table C(Customer int, Enquiry int, EnquiryDate date)
insert into C(Customer,Enquiry,EnquiryDate)
values

--Customer Enquiry EnquiryDate   DaysSinceLastEnquiryForCustomer
(522181, 232853,parse('19/01/2014' as date using 'en-GB')),--  NULL
(522181, 234750,parse('30/01/2014' as date using 'en-GB')),--  11
(522181, 235141,parse('03/02/2014' as date using 'en-GB')),--  5
(522181, 235210,parse('03/02/2014' as date using 'en-GB')),--  5
(522181, 262015,parse('23/09/2014' as date using 'en-GB')),--  NULL
(522181, 263942,parse('09/10/2014' as date using 'en-GB')),--  NULL
(522181, 265583,parse('22/10/2014' as date using 'en-GB')),--  13
(522181, 311345,parse('01/10/2015' as date using 'en-GB')),--  10
(522181, 321938,parse('31/12/2015' as date using 'en-GB')),--  NULL
(522181, 322404,parse('31/12/2015' as date using 'en-GB')),--  0
(522181, 328057,parse('27/01/2016' as date using 'en-GB')),--  23
(522181, 329164,parse('02/02/2016' as date using 'en-GB')),--  6
(522181, 329426,parse('03/02/2016' as date using 'en-GB')),--  1
(522181, 336409,parse('17/03/2016' as date using 'en-GB')),--  14
(522181, 336581,parse('18/03/2016' as date using 'en-GB')),--  1
(522181, 337003,parse('22/03/2016' as date using 'en-GB')),--  4
(522181, 343338,parse('15/05/2016' as date using 'en-GB')),--  NULL
(522181, 344185,parse('23/05/2016' as date using 'en-GB')),--  8
(522181, 352323,parse('06/08/2016' as date using 'en-GB'))--  14


select *, prev.daysSince
from C c1
outer apply 
(
  select top 1 *, datediff(day, c2.EnquiryDate, c1.EnquiryDate) daysSince
  from C c2
  where c2.Customer = c1.Customer
    and c2.Enquiry != c1.Enquiry
    and c2.EnquiryDate < c1.EnquiryDate
    and datediff(day, c2.EnquiryDate, c1.EnquiryDate) < 28
  order by c2.EnquiryDate desc
) prev
order by c1.Customer,c1.Enquiry