我正在获取具有逻辑顺序的一些数据(进入#temp表)。
当我提取数据时,我想添加一个新的序列/计数器,该序列/计数器仅在其他字段中满足某些条件时才递增。
理想情况是这样的:
DECLARE @counter int = 0;
SELECT Item, Date, Event,
@counter = @counter +
(CASE
WHEN Event = 'Something' THEN 1
ELSE 0
END) AS EVENT_SEQ
INTO #tempTable
FROM MyData
ORDER BY Item, Date
Run Code Online (Sandbox Code Playgroud)
SQL给了我两个错误:
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.
Run Code Online (Sandbox Code Playgroud)
要么
A SELECT INTO statement cannot contain a SELECT statement that assigns values to a variable.
Run Code Online (Sandbox Code Playgroud)
理想的结果
ITEM DATE EVENT EVENT_SEQ
blah 2015-01-01 nothing 1
blah 2015-01-02 nothing 1
blah 2015-01-03 nothing 1
blah 2015-01-04 something 2
blah 2015-01-05 nothing 2
blah 2015-01-06 nothing 2
blah 2015-01-07 something 3
blah 2015-01-08 nothing 3
blah 2015-01-09 nothing 3
blah 2015-01-10 nothing 3
blah 2015-01-11 nothing 3
blah 2015-01-12 something 4
blah 2015-01-13 something 5
blah 2015-01-14 nothing 5
blah 2015-01-15 nothing 5
blah 2015-01-16 nothing 5
blah 2015-01-17 nothing 5
blah 2015-01-18 nothing 5
blah 2015-01-19 nothing 5
blah 2015-01-20 something 6
blah 2015-01-21 something 7
blah 2015-01-22 nothing 7
Run Code Online (Sandbox Code Playgroud)
类似的问题:
SQL Server 2012:有条件地递增计数器用户ROW_NUMBER()
我正在使用SQL 2014
这似乎产生了您想要的结果。当试图熟练地编写SQL时,最好是考虑整个结果集,而不是考虑“逐行”处理。
因此,我将您的规范重新铸造为:
EVENT_SEQ是在something当前行之前或之上发生的事件数,+ 1
declare @t table (ITEM char(4), [Date] date, [Event] varchar(9))
insert into @T(ITEM,[DATE],[EVENT]) values
('blah','20150101','nothing'),
('blah','20150102','nothing'),
('blah','20150103','nothing'),
('blah','20150104','something'),
('blah','20150105','nothing'),
('blah','20150106','nothing'),
('blah','20150107','something'),
('blah','20150108','nothing'),
('blah','20150109','nothing'),
('blah','20150110','nothing'),
('blah','20150111','nothing'),
('blah','20150112','something'),
('blah','20150113','something'),
('blah','20150114','nothing'),
('blah','20150115','nothing'),
('blah','20150116','nothing'),
('blah','20150117','nothing'),
('blah','20150118','nothing'),
('blah','20150119','nothing'),
('blah','20150120','something'),
('blah','20150121','something'),
('blah','20150122','nothing')
select *,
SUM(CASE WHEN [event]='something' THEN 1 ELSE 0 END) OVER
(ORDER BY item,date
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW)+1 as EVENT_SEQ
from @t
ORDER BY item,date
Run Code Online (Sandbox Code Playgroud)
在希望看到的地方,我几乎已经将其几乎直接转换为一个窗口聚合函数(SUM() OVER())。结果:
ITEM Date Event EVENT_SEQ
---- ---------- --------- -----------
blah 2015-01-01 nothing 1
blah 2015-01-02 nothing 1
blah 2015-01-03 nothing 1
blah 2015-01-04 something 2
blah 2015-01-05 nothing 2
blah 2015-01-06 nothing 2
blah 2015-01-07 something 3
blah 2015-01-08 nothing 3
blah 2015-01-09 nothing 3
blah 2015-01-10 nothing 3
blah 2015-01-11 nothing 3
blah 2015-01-12 something 4
blah 2015-01-13 something 5
blah 2015-01-14 nothing 5
blah 2015-01-15 nothing 5
blah 2015-01-16 nothing 5
blah 2015-01-17 nothing 5
blah 2015-01-18 nothing 5
blah 2015-01-19 nothing 5
blah 2015-01-20 something 6
blah 2015-01-21 something 7
blah 2015-01-22 nothing 7
Run Code Online (Sandbox Code Playgroud)