【问题标题】:Comparing Data by time in Sql在Sql中按时间比较数据
【发布时间】:2018-07-24 12:26:13
【问题描述】:

我有数据

SELECT [Dt]
      ,x1
      ,[SaleCount]
      ,x2
      ,x3
      ,action
  FROM table

action 是一个动作(0 不是,1 是)。 事情的实质如下: 例如对于组

x1 + x2 + x3
1    2    2017

可能是一个动作,这里有一些选项可以让这个群体对另一个群体产生影响。 比如一个群

x1 + x2 + x3,
2 + 3 + 2017

我需要重组数据,以便有某些组合。 There is a action on the group 1 + 2 + 2017 and no action on the group 2 + 3 + 2017(no marker);

or there is a action of the group 1 + 2 + 2017, but in group 2 + 3 + 2017 was also action before the action for  group 1 + 2 + 2017(marker before)

(即 2+3+2017 的动作列排在第 1 + 2 + 2017 组的动作列之前)

or there is a action on the group 1 + 2 + 2017, but on the group 2 + 3 + 2017 there was also an action AFTER the action group 1 + 2 + 2017(marker after)

(即 2+3+2017 的动作列排在第 1 + 2 + 2017 组的动作列之后)

所以我需要分配与这些条件相对应的动作组合。 数据样本

Declare @t table 
(Dt date,
  x1 int,
  Sale int,
  x2 int,
  x3 int,
  action int,
  typegroup varchar);
insert into @t values
('23.07.2018',1,2,2017,1,0,basis),
('24.07.2018',1,2,2017,2,0,basis),
('25.07.2018',1,2,2017,3,0,basis),
('26.07.2018',1,2,2017,4,0,basis),
('27.07.2018',1,2,2017,5,0,basis),
('28.07.2018',1,2,2017,6,0,basis),
('29.07.2018',1,2,2017,7,0,basis),
('30.07.2018',1,2,2017,8,0,basis),
('31.07.2018',1,2,2017,9,0,basis),
('01.08.2018',1,2,2017,10,0,basis),
('02.08.2018',1,2,2017,11,0,basis),
('03.08.2018',1,2,2017,12,1,basis),
('04.08.2018',1,2,2017,13,1,basis),
('05.08.2018',1,2,2017,14,1,basis),
('06.08.2018',1,2,2017,15,1,basis),
('07.08.2018',1,2,2017,16,1,basis),
('08.08.2018',1,2,2017,17,1,basis),
('09.08.2018',1,2,2017,18,1,basis),
('10.08.2018',1,2,2017,19,1,basis),
('11.08.2018',1,2,2017,20,1,basis),
('12.08.2018',1,2,2017,21,1,basis),
('13.08.2018',1,2,2017,22,1,basis),
('14.08.2018',1,2,2017,23,1,basis),
('15.08.2018',1,2,2017,24,1,basis),
('16.08.2018',1,2,2017,25,1,basis),
('17.08.2018',1,2,2017,26,1,basis),
('18.08.2018',1,2,2017,27,0,basis),
('19.08.2018',1,2,2017,28,0,basis),
('20.08.2018',1,2,2017,29,0,basis),
('21.08.2018',1,2,2017,30,0,basis),
('22.08.2018',1,2,2017,31,0,basis),
('23.08.2018',1,2,2017,32,0,basis),
('24.08.2018',1,2,2017,33,0,basis),
('25.08.2018',1,2,2017,34,0,basis),
('23.07.2018',2,3,2017,1,0,no),
('24.07.2018',2,3,2017,2,0,no),
('25.07.2018',2,3,2017,3,0,no),
('26.07.2018',2,3,2017,4,0,no),
('27.07.2018',2,3,2017,5,0,no),
('28.07.2018',2,3,2017,6,0,no),
('29.07.2018',2,3,2017,7,0,no),
('30.07.2018',2,3,2017,8,0,no),
('31.07.2018',2,3,2017,9,0,no),
('01.08.2018',2,3,2017,10,0,no),
('02.08.2018',2,3,2017,11,0,no),
('03.08.2018',2,3,2017,12,0,no),
('04.08.2018',2,3,2017,13,0,no),
('05.08.2018',2,3,2017,14,0,no),
('06.08.2018',2,3,2017,15,0,no),
('07.08.2018',2,3,2017,16,0,no),
('08.08.2018',2,3,2017,17,0,no),
('09.08.2018',2,3,2017,18,0,no),
('10.08.2018',2,3,2017,19,0,no),
('11.08.2018',2,3,2017,20,0,no),
('12.08.2018',2,3,2017,21,0,no),
('13.08.2018',2,3,2017,22,0,no),
('14.08.2018',2,3,2017,23,0,no),
('15.08.2018',2,3,2017,24,0,no),
('16.08.2018',2,3,2017,25,0,no),
('17.08.2018',2,3,2017,26,0,no),
('18.08.2018',2,3,2017,27,0,no),
('19.08.2018',2,3,2017,28,0,no),
('20.08.2018',2,3,2017,29,0,no),
('21.08.2018',2,3,2017,30,0,no),
('22.08.2018',2,3,2017,31,0,no),
('23.08.2018',2,3,2017,32,0,no),
('24.08.2018',2,3,2017,33,0,no),
('25.08.2018',2,3,2017,34,0,no),
('23.07.2018',3,4,2017,1,1,before),
('24.07.2018',3,4,2017,2,1,before),
('25.07.2018',3,4,2017,3,1,before),
('26.07.2018',3,4,2017,4,1,before),
('27.07.2018',3,4,2017,5,1,before),
('28.07.2018',3,4,2017,6,1,before),
('29.07.2018',3,4,2017,7,1,before),
('30.07.2018',3,4,2017,8,1,before),
('31.07.2018',3,4,2017,9,1,before),
('01.08.2018',3,4,2017,10,1,before),
('02.08.2018',3,4,2017,11,0,before),
('03.08.2018',3,4,2017,12,0,before),
('04.08.2018',3,4,2017,13,0,before),
('05.08.2018',3,4,2017,14,0,before),
('06.08.2018',3,4,2017,15,0,before),
('07.08.2018',3,4,2017,16,0,before),
('08.08.2018',3,4,2017,17,0,before),
('09.08.2018',3,4,2017,18,0,before),
('10.08.2018',3,4,2017,19,0,before),
('11.08.2018',3,4,2017,20,0,before),
('12.08.2018',3,4,2017,21,0,before),
('13.08.2018',3,4,2017,22,0,before),
('14.08.2018',3,4,2017,23,0,before),
('15.08.2018',3,4,2017,24,0,before),
('16.08.2018',3,4,2017,25,0,before),
('17.08.2018',3,4,2017,26,0,before),
('18.08.2018',3,4,2017,27,0,before),
('19.08.2018',3,4,2017,28,0,before),
('20.08.2018',3,4,2017,29,0,before),
('21.08.2018',3,4,2017,30,0,before),
('22.08.2018',3,4,2017,31,0,before),
('23.08.2018',3,4,2017,32,0,before),
('24.08.2018',3,4,2017,33,0,before);

#

I compare by time, i.e. at the same time, i looking for all group that meet the above conditions.
In this example, for the group 1 + 2 + 2017
group 2 + 3 + 2017 did not have action
and the group 3 + 4 + 2017 had a action before starting action for 1 + 2 + 2017
and nothing more no.
NOW Let's work  the next group for example 3 + 4 + 2017, look at the time when it had an action and how it affected other group in th

在指定条件下同时进行。 I.E 3 + 4 + 2017 成为基础。

怎么做?

对于组,必须生成标记。 基础是我们正在寻找比较的组。 以及与之比较的所有东西,标记 “no”,或“before”,或“after”的标志,取决于在时间 sql 中找到的组组合。

换句话说,可以有很多这样的条纹相互组合。

I.E.对于一个组,1 + 2 + 2017可能是基础,而对于另一个,例如10 + 10 + 2017,它根本没有任何动作。

【问题讨论】:

  • 对不起,我在stratastratum 上迷路了。我很难理解。
  • @Squirrel,strata 和 stratum 均值组列。例如 x1 是 shop,x2 是 stuff,x3 是 number 。 IE。超市+可口可乐+123 是团体。 Strata 组的同义词。现在你明白我的意思了吗?
  • 一开始你提到x1 + x2 + x3 1 + 2 + 2017,但在后面的部分,x1, x2, x3 的样本数据是1 2017 1
  • 请同时显示您的预期结果
  • @Squirrel,我提到了 1 2017 1。也许是错误的。我希望我在数据样本中表示的内容声明@t 表......对于我必须同时找到的每个基组。没有动作的组,动作在基础组的动作之前的组,以及动作在基础组的动作之后的组。也许满足一个条件或同时满足所有条件。取决于组。你明白我的意思吗?

标签: sql sql-server ssms


【解决方案1】:

在这里我找到了很好的解决方案。 https://social.msdn.microsoft.com/Forums/sqlserver/en-US/d891c693-2d38-4064-9784-4b21cd2fca11/comparing-data-by-time-in-sql?forum=transactsql 它有效。

set dateformat dmy
go

Declare @t table 
(Dt date,
  x1 int,
  x2 int,
  x3 int,
  sale int,
  action int,
  typegroup varchar(20));
insert into @t values
('23.07.2018',1,2,2017,1,0,''),
('24.07.2018',1,2,2017,2,0,''),
('25.07.2018',1,2,2017,3,0,''),
('26.07.2018',1,2,2017,4,0,''),
('27.07.2018',1,2,2017,5,0,''),
('28.07.2018',1,2,2017,6,0,''),
('29.07.2018',1,2,2017,7,0,''),
('30.07.2018',1,2,2017,8,0,''),
('31.07.2018',1,2,2017,9,0,''),
('01.08.2018',1,2,2017,10,0,''),
('02.08.2018',1,2,2017,11,0,''),
('03.08.2018',1,2,2017,12,1,''),
('04.08.2018',1,2,2017,13,1,''),
('05.08.2018',1,2,2017,14,1,''),
('06.08.2018',1,2,2017,15,1,''),
('07.08.2018',1,2,2017,16,1,''),
('08.08.2018',1,2,2017,17,1,''),
('09.08.2018',1,2,2017,18,1,''),
('10.08.2018',1,2,2017,19,1,''),
('11.08.2018',1,2,2017,20,1,''),
('12.08.2018',1,2,2017,21,1,''),
('13.08.2018',1,2,2017,22,1,''),
('14.08.2018',1,2,2017,23,1,''),
('15.08.2018',1,2,2017,24,1,''),
('16.08.2018',1,2,2017,25,1,''),
('17.08.2018',1,2,2017,26,1,''),
('18.08.2018',1,2,2017,27,0,''),
('19.08.2018',1,2,2017,28,0,''),
('20.08.2018',1,2,2017,29,0,''),
('21.08.2018',1,2,2017,30,0,''),
('22.08.2018',1,2,2017,31,0,''),
('23.08.2018',1,2,2017,32,0,''),
('24.08.2018',1,2,2017,33,0,''),
('25.08.2018',1,2,2017,34,0,''),
('23.07.2018',2,3,2017,1,0,''),
('24.07.2018',2,3,2017,2,0,''),
('25.07.2018',2,3,2017,3,0,''),
('26.07.2018',2,3,2017,4,0,''),
('27.07.2018',2,3,2017,5,0,''),
('28.07.2018',2,3,2017,6,0,''),
('29.07.2018',2,3,2017,7,0,''),
('30.07.2018',2,3,2017,8,0,''),
('31.07.2018',2,3,2017,9,0,''),
('01.08.2018',2,3,2017,10,0,''),
('02.08.2018',2,3,2017,11,0,''),
('03.08.2018',2,3,2017,12,0,''),
('04.08.2018',2,3,2017,13,0,''),
('05.08.2018',2,3,2017,14,0,''),
('06.08.2018',2,3,2017,15,0,''),
('07.08.2018',2,3,2017,16,0,''),
('08.08.2018',2,3,2017,17,0,''),
('09.08.2018',2,3,2017,18,0,''),
('10.08.2018',2,3,2017,19,0,''),
('11.08.2018',2,3,2017,20,0,''),
('12.08.2018',2,3,2017,21,0,''),
('13.08.2018',2,3,2017,22,0,''),
('14.08.2018',2,3,2017,23,0,''),
('15.08.2018',2,3,2017,24,0,''),
('16.08.2018',2,3,2017,25,0,''),
('17.08.2018',2,3,2017,26,0,''),
('18.08.2018',2,3,2017,27,0,''),
('19.08.2018',2,3,2017,28,0,''),
('20.08.2018',2,3,2017,29,0,''),
('21.08.2018',2,3,2017,30,0,''),
('22.08.2018',2,3,2017,31,0,''),
('23.08.2018',2,3,2017,32,0,''),
('24.08.2018',2,3,2017,33,0,''),
('25.08.2018',2,3,2017,34,0,''),
('23.07.2018',3,4,2017,1,1,''),
('24.07.2018',3,4,2017,2,1,''),
('25.07.2018',3,4,2017,3,1,''),
('26.07.2018',3,4,2017,4,1,''),
('27.07.2018',3,4,2017,5,1,''),
('28.07.2018',3,4,2017,6,1,''),
('29.07.2018',3,4,2017,7,1,''),
('30.07.2018',3,4,2017,8,1,''),
('31.07.2018',3,4,2017,9,1,''),
('01.08.2018',3,4,2017,10,1,''),
('02.08.2018',3,4,2017,11,0,''),
('03.08.2018',3,4,2017,12,0,''),
('04.08.2018',3,4,2017,13,0,''),
('05.08.2018',3,4,2017,14,0,''),
('06.08.2018',3,4,2017,15,0,''),
('07.08.2018',3,4,2017,16,0,''),
('08.08.2018',3,4,2017,17,0,''),
('09.08.2018',3,4,2017,18,0,''),
('10.08.2018',3,4,2017,19,0,''),
('11.08.2018',3,4,2017,20,0,''),
('12.08.2018',3,4,2017,21,0,''),
('13.08.2018',3,4,2017,22,0,''),
('14.08.2018',3,4,2017,23,0,''),
('15.08.2018',3,4,2017,24,0,''),
('16.08.2018',3,4,2017,25,0,''),
('17.08.2018',3,4,2017,26,0,''),
('18.08.2018',3,4,2017,27,1,''),
('19.08.2018',3,4,2017,28,1,''),
('20.08.2018',3,4,2017,29,1,''),
('21.08.2018',3,4,2017,30,1,''),
('22.08.2018',3,4,2017,31,1,''),
('23.08.2018',3,4,2017,32,1,''),
('24.08.2018',3,4,2017,33,1,'');

declare @x1 int,
  @x2 int,
  @x3 int,@mindt date,@maxdt date

--pass any group values here
  select @x1 = 1, @x2 = 2,@x3= 2017


  Select @mindt = min(Dt), @maxdt = max(Dt)
from @t
where x1 = @x1
and  x2 = @x2
and x3 = @x3
and action =1

  update r
  set typegroup= type
  from (select *,
  case              when x1=@x1 and x2 = @x2 and x3 = @x3 then 'basis'
                when  action = 1 and max(Dt) over (partition by nxt) > coalesce(@maxdt,'99991231') then 'after'
                when  action = 1 and min(Dt) over (partition by nxt) < coalesce(@mindt,'19000101') then 'before'

                end as type
                from @t t
                outer apply
  (
Select min(Dt) as nxt
from @t
where x1 = t.x1
and  x2 = t.x2
and x3 = t.x3
and action <> t.action
and Dt > t.Dt
)t1)r


select *
from @t
order by x1,x2,x3,sale

/*
Output
-----------------------------------------------------------
Dt  x1  x2  x3  sale    action  typegroup
--------------------------------------------------------------------------
2018-07-23  1   2   2017    1   0   basis
2018-07-24  1   2   2017    2   0   basis
2018-07-25  1   2   2017    3   0   basis
2018-07-26  1   2   2017    4   0   basis
2018-07-27  1   2   2017    5   0   basis
2018-07-28  1   2   2017    6   0   basis
2018-07-29  1   2   2017    7   0   basis
2018-07-30  1   2   2017    8   0   basis
2018-07-31  1   2   2017    9   0   basis
2018-08-01  1   2   2017    10  0   basis
2018-08-02  1   2   2017    11  0   basis
2018-08-03  1   2   2017    12  1   basis
2018-08-04  1   2   2017    13  1   basis
2018-08-05  1   2   2017    14  1   basis
2018-08-06  1   2   2017    15  1   basis
2018-08-07  1   2   2017    16  1   basis
2018-08-08  1   2   2017    17  1   basis
2018-08-09  1   2   2017    18  1   basis
2018-08-10  1   2   2017    19  1   basis
2018-08-11  1   2   2017    20  1   basis
2018-08-12  1   2   2017    21  1   basis
2018-08-13  1   2   2017    22  1   basis
2018-08-14  1   2   2017    23  1   basis
2018-08-15  1   2   2017    24  1   basis
2018-08-16  1   2   2017    25  1   basis
2018-08-17  1   2   2017    26  1   basis
2018-08-18  1   2   2017    27  0   basis
2018-08-19  1   2   2017    28  0   basis
2018-08-20  1   2   2017    29  0   basis
2018-08-21  1   2   2017    30  0   basis
2018-08-22  1   2   2017    31  0   basis
2018-08-23  1   2   2017    32  0   basis
2018-08-24  1   2   2017    33  0   basis
2018-08-25  1   2   2017    34  0   basis
2018-07-23  2   3   2017    1   0   NULL
2018-07-24  2   3   2017    2   0   NULL
2018-07-25  2   3   2017    3   0   NULL
2018-07-26  2   3   2017    4   0   NULL
2018-07-27  2   3   2017    5   0   NULL
2018-07-28  2   3   2017    6   0   NULL
2018-07-29  2   3   2017    7   0   NULL
2018-07-30  2   3   2017    8   0   NULL
2018-07-31  2   3   2017    9   0   NULL
2018-08-01  2   3   2017    10  0   NULL
2018-08-02  2   3   2017    11  0   NULL
2018-08-03  2   3   2017    12  0   NULL
2018-08-04  2   3   2017    13  0   NULL
2018-08-05  2   3   2017    14  0   NULL
2018-08-06  2   3   2017    15  0   NULL
2018-08-07  2   3   2017    16  0   NULL
2018-08-08  2   3   2017    17  0   NULL
2018-08-09  2   3   2017    18  0   NULL
2018-08-10  2   3   2017    19  0   NULL
2018-08-11  2   3   2017    20  0   NULL
2018-08-12  2   3   2017    21  0   NULL
2018-08-13  2   3   2017    22  0   NULL
2018-08-14  2   3   2017    23  0   NULL
2018-08-15  2   3   2017    24  0   NULL
2018-08-16  2   3   2017    25  0   NULL
2018-08-17  2   3   2017    26  0   NULL
2018-08-18  2   3   2017    27  0   NULL
2018-08-19  2   3   2017    28  0   NULL
2018-08-20  2   3   2017    29  0   NULL
2018-08-21  2   3   2017    30  0   NULL
2018-08-22  2   3   2017    31  0   NULL
2018-08-23  2   3   2017    32  0   NULL
2018-08-24  2   3   2017    33  0   NULL
2018-08-25  2   3   2017    34  0   NULL
2018-07-23  3   4   2017    1   1   before
2018-07-24  3   4   2017    2   1   before
2018-07-25  3   4   2017    3   1   before
2018-07-26  3   4   2017    4   1   before
2018-07-27  3   4   2017    5   1   before
2018-07-28  3   4   2017    6   1   before
2018-07-29  3   4   2017    7   1   before
2018-07-30  3   4   2017    8   1   before
2018-07-31  3   4   2017    9   1   before
2018-08-01  3   4   2017    10  1   before
2018-08-02  3   4   2017    11  0   NULL
2018-08-03  3   4   2017    12  0   NULL
2018-08-04  3   4   2017    13  0   NULL
2018-08-05  3   4   2017    14  0   NULL
2018-08-06  3   4   2017    15  0   NULL
2018-08-07  3   4   2017    16  0   NULL
2018-08-08  3   4   2017    17  0   NULL
2018-08-09  3   4   2017    18  0   NULL
2018-08-10  3   4   2017    19  0   NULL
2018-08-11  3   4   2017    20  0   NULL
2018-08-12  3   4   2017    21  0   NULL
2018-08-13  3   4   2017    22  0   NULL
2018-08-14  3   4   2017    23  0   NULL
2018-08-15  3   4   2017    24  0   NULL
2018-08-16  3   4   2017    25  0   NULL
2018-08-17  3   4   2017    26  0   NULL
2018-08-18  3   4   2017    27  1   after
2018-08-19  3   4   2017    28  1   after
2018-08-20  3   4   2017    29  1   after
2018-08-21  3   4   2017    30  1   after
2018-08-22  3   4   2017    31  1   after
2018-08-23  3   4   2017    32  1   after
2018-08-24  3   4   2017    33  1   after

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2018-06-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多