如何使用SQL生成此类结果

时间:2016-11-13 16:23:59

标签: sql sql-server date transactions

我编辑了我的问题,因为人们似乎误解了我想要的东西。

我有一个表格,其中包含以下列:

  1. 公司
  2. 交易ID
  3. 交易日期
  4. 我想要的结果是:

    |      COMPANY        |  Transaction ID  |Transaction Date  |     GROUP
    |---------------------|------------------|------------------|----------
    |     Company A       |      t_0001      |    01-01-2014    |         1
    |     Company A       |      t_0002      |    02-01-2014    |         1
    |     Company A       |      t_0003      |    04-01-2014    |         1
    |     Company A       |      t_0003      |    10-01-2014    |         2
    |     Company B       |      t_0004      |    02-01-2014    |         1
    |     Company B       |      t_0005      |    02-01-2014    |         1
    |     Company C       |      t_0006      |    03-01-2014    |         1
    |     Company C       |      t_0007      |    05-01-2014    |         2
    

    交易和日期首先分组为公司。公司内的交易从最早到最晚分类。如果先前的交易在移动窗口期间少于3天前执行,则逐行检查交易。

    例如,t_0002和t_0001相隔不到3天,因此它们属于第1组.t_0003和t_0002相隔不到3天,因此即使t_0003和t_0003相隔> = 3天,它们也属于第1组。

    我认为实现这一目标的方法是首先按公司对数据进行分组,然后按日期对事务进行排序,但在此之后我陷入困境。我可以用什么方法来产生这种结果?对此有何帮助?

    P.S。我正在使用SQL Server 2014。

3 个答案:

答案 0 :(得分:0)

我已根据交易ID确定了每家公司之间的天差。因此,如果天差小于3则进入第1组,其他则为2.根据您的要求更改滞后条款并使用它。

nowPlaying

答案 1 :(得分:0)

如果您不关心组中的编号,请使用

works_by_artists()

<强> Sample Demo

如果组需要连续数字,请使用

select *, 
dense_rank() over(partition by company order by transaction_date) -
(select count(distinct transaction_date) from t 
 where t1.company=company  
 and datediff(dd,transaction_date,t1.transaction_date) between 1 and 2) grp
from t t1
order by 1,3

答案 2 :(得分:0)

create table xn (
  [Company] char(1),
  [Transaction ID] char(6),
  [Transaction Date] date,
  primary key ([Company], [Transaction ID], [Transaction Date])
);

insert into xn values
('A', 't_0001', '2014-01-01'),
('A', 't_0002', '2014-01-02'),
('A', 't_0003', '2014-01-04'),
('A', 't_0003', '2014-01-10'),
('B', 't_0004', '2014-01-02'),
('B', 't_0005', '2014-01-02'),
('C', 't_0006', '2014-01-03'),
('C', 't_0007', '2014-01-05');

每个查询都建立在之前的查询之上。有更简洁的方法来编写这样的查询,但我认为这种方式有助于您学习lag(...) over (...)等窗口函数。

这里的第一个将前一个交易日期带入&#34;当前&#34;行。

select 
    [Company], 
    [Transaction ID], 
    [Transaction Date], 
    lag ([Transaction Date]) over (partition by [Company] order by [Transaction Date]) as [Prev Transaction Date]
from xn

此查询确定&#34;当前&#34;之间的天数。交易日期和上一个交易日期。

select 
    [Company], 
    [Transaction ID], 
    [Transaction Date], 
    [Prev Transaction Date], 
    DateDiff(d, [Prev Transaction Date], [Transaction Date]) as [Days Between]
from (select 
          [Company], 
          [Transaction ID], 
          [Transaction Date], 
          lag ([Transaction Date]) over (partition by [Company] order by [Transaction Date]) as [Prev Transaction Date]
      from xn) x  

这是根据天数进行分组。

select 
    [Company],
    [Transaction ID], 
    [Transaction Date], 
    case when [Days Between] between 0 and 3 then 1
         when [Days Between] is null then 1
         when [Days Between] > 3 then 2
         else 'Ummm'
    end as [Group Num]
from (
        select 
            [Company], 
            [Transaction ID], 
            [Transaction Date], 
            [Prev Transaction Date], 
            DateDiff(d, [Prev Transaction Date], [Transaction Date]) as [Days Between]
        from (select 
                  [Company], 
                  [Transaction ID], 
                  [Transaction Date], 
                  lag ([Transaction Date]) over (partition by [Company] order by [Transaction Date]) as [Prev Transaction Date]
              from xn) x  
) y;