我有下表:
ITEM DATE VALUE
----------------------
ITEM1 2016-05-04 1
ITEM1 2016-05-05 3
ITEM1 2016-05-06 3
ITEM1 2016-05-09 3
ITEM1 2016-05-04 4
ITEM2 2016-05-10 1
ITEM2 2016-05-05 2
ITEM2 2016-05-06 3
ITEM2 2016-05-09 1
ITEM2 2016-05-10 1
而且我希望每个项目退出,有多少条目的时间值与价值列相同(平坦):
ITEM DATE VALUE NUM_FLAT_ENTRYPOINTS
------------------------------
ITEM1 2016-05-04 1 0
ITEM1 2016-05-05 3 0
ITEM1 2016-05-06 3 1
ITEM1 2016-05-09 3 2
ITEM1 2016-05-10 4 0
ITEM2 2016-05-04 1 0
ITEM2 2016-05-05 2 0
ITEM2 2016-05-06 3 0
ITEM2 2016-05-09 1 0
ITEM2 2016-05-10 1 1
我最初的意思是:
select
*,
rank()-1 over (partition by ITEM,VALUE order by DATE) as NUM_FLAT_ENTRYPOINTS
from my_table
然而,这不起作用,因为ITEM2将2016-05-04,2016-05-09和2016-05-10分区在一起,并为最后一行的NUM_FLAT_ENTRYPOINTS显示2而不是1。
我正在使用Microsoft SQL Server 2008。
有什么想法吗?
编辑:
在Oracle(以及可能的其他SQL Server)中,我似乎可以做到
select
count(VALUE)-1 over (partition by ITEM,VALUE order by DATE) as NUM_FLAT_ENTRYPOINTS
from my_table
但据我所知,这种语法在SQL Server 2008中不起作用。有什么方法可以解决它吗?
答案 0 :(得分:1)
假设我对评论中建议的样本数据进行了更正,这似乎适合该法案:
declare @t table (ITEM char(5), Date date, Value tinyint)
insert into @t(ITEM,DATE,VALUE) values
('ITEM1','20160504',1),
('ITEM1','20160505',3),
('ITEM1','20160506',3),
('ITEM1','20160509',3),
('ITEM1','20160510',4),
('ITEM2','20160504',1),
('ITEM2','20160505',2),
('ITEM2','20160506',3),
('ITEM2','20160509',1),
('ITEM2','20160510',1)
;With Ordered as (
select
Item,
Date,
Value,
ROW_NUMBER() OVER (PARTITION BY Item ORDER BY Date) as rn
from @t
)
select
*,
COALESCE(rn -
(select MAX(o2.rn) from Ordered o2
where o2.ITEM = o.ITEM and
o2.rn < o.rn and
o2.Value != o.Value) - 1
, o.rn - 1) as NUM_FLAT_ENTRYPOINTS
from
Ordered o
也就是说,我们分配行号(每个项目分别),然后我们只是找到比Value
不同的当前行号更早的行号。减去这些行号(以及另外1行)会产生我们需要的答案 - 假设可以找到这样一个较早的行。如果没有这样的早期行,那么我们显然处于特定项目开头的序列中 - 所以我们只是从行号中减去1。
我已经选择了“显然是正确的” - 有可能有一种方法可以产生可能表现更好的结果,但我现在并没有瞄准它。
结果:
Item Date Value rn NUM_FLAT_ENTRYPOINTS
----- ---------- ----- -------------------- --------------------
ITEM1 2016-05-04 1 1 0
ITEM1 2016-05-05 3 2 0
ITEM1 2016-05-06 3 3 1
ITEM1 2016-05-09 3 4 2
ITEM1 2016-05-10 4 5 0
ITEM2 2016-05-04 1 1 0
ITEM2 2016-05-05 2 2 0
ITEM2 2016-05-06 3 3 0
ITEM2 2016-05-09 1 4 0
ITEM2 2016-05-10 1 5 1
答案 1 :(得分:1)
它看起来像是间隙和岛屿的变种。
示例数据
DECLARE @T TABLE (ITEM varchar(50), dt date, VALUE int);
INSERT INTO @T(ITEM, dt, VALUE) VALUES
('ITEM1', '2016-05-04', 1),
('ITEM1', '2016-05-05', 3),
('ITEM1', '2016-05-06', 3),
('ITEM1', '2016-05-09', 3),
('ITEM1', '2016-05-10', 4),
('ITEM2', '2016-05-04', 1),
('ITEM2', '2016-05-05', 2),
('ITEM2', '2016-05-06', 3),
('ITEM2', '2016-05-09', 1),
('ITEM2', '2016-05-10', 1);
<强>查询强>
WITH
CTE
AS
(
SELECT
ITEM
,dt
,VALUE
,ROW_NUMBER() OVER (PARTITION BY ITEM ORDER BY dt) AS rn1
,ROW_NUMBER() OVER (PARTITION BY ITEM, VALUE ORDER BY dt) AS rn2
FROM @T
)
SELECT
ITEM
,dt
,VALUE
,rn1-rn2 AS rnDiff
,ROW_NUMBER() OVER
(PARTITION BY ITEM, VALUE, rn1-rn2 ORDER BY dt) - 1 AS NUM_FLAT_ENTRYPOINTS
FROM CTE
ORDER BY ITEM, dt;
<强>结果强>
+-------+------------+-------+--------+----------------------+
| ITEM | dt | VALUE | rnDiff | NUM_FLAT_ENTRYPOINTS |
+-------+------------+-------+--------+----------------------+
| ITEM1 | 2016-05-04 | 1 | 0 | 0 |
| ITEM1 | 2016-05-05 | 3 | 1 | 0 |
| ITEM1 | 2016-05-06 | 3 | 1 | 1 |
| ITEM1 | 2016-05-09 | 3 | 1 | 2 |
| ITEM1 | 2016-05-10 | 4 | 4 | 0 |
| ITEM2 | 2016-05-04 | 1 | 0 | 0 |
| ITEM2 | 2016-05-05 | 2 | 1 | 0 |
| ITEM2 | 2016-05-06 | 3 | 2 | 0 |
| ITEM2 | 2016-05-09 | 1 | 2 | 0 |
| ITEM2 | 2016-05-10 | 1 | 2 | 1 |
+-------+------------+-------+--------+----------------------+
答案 2 :(得分:1)
试试这个:
SELECT ITEM, [DATE], VALUE,
ROW_NUMBER() OVER (PARTITION BY ITEM, VALUE, grp
ORDER BY [DATE]) - 1 AS NUM_FLAT_ENTRYPOINTS
FROM (
SELECT ITEM, [DATE], VALUE,
ROW_NUMBER() OVER (PARTITION BY ITEM ORDER BY [DATE]) -
ROW_NUMBER() OVER (PARTITION BY ITEM, VALUE ORDER BY [DATE]) AS grp
FROM mytable) AS t