【发布时间】: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,它根本没有任何动作。
【问题讨论】:
-
对不起,我在
strata和stratum上迷路了。我很难理解。 -
@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