表格中有一些数据:
date finance
1 5 (income)
3 -3 (spending)
4 -1 (spending)
5 10 (income)
9 -2 (spending)
10 -3 (spending)
15 -3 (spending)
每个正值构成一个组,每个组以正值开头,并包括下一个posivitve值之前的所有负值。
所以我必须识别这样的群体:
date finance rank
1 5 group 1
3 -3 group 1
4 -1 group 1
5 10 group 2
9 -2 group 2
10 -3 group 2
15 -3 group 2
日期总是在增长,而且永远不会相同。
窗口函数t-sql可以吗?得到这个结果的最佳方法是什么?
答案 0 :(得分:1)
这有点无用,但似乎有用
SELECT [date], finance
, 'group '
+ rtrim((SELECT count(1) FROM #Tmp WHERE finance > 0 AND [date] < t.[date])
+ CASE WHEN finance > 0 THEN 1 ELSE 0 END) 'Group'
FROM #Tmp t
答案 1 :(得分:0)
您的分组似乎非常依赖于行的顺序。
假设您的源表中有一个Row#[R],则以下代码应该有效。
--FIND ROWS WITH POSITIVE VALUE
SELECT R2=DENSE_RANK() OVER(ORDER BY R), R
INTO #TEMP
FROM <SOURCE_TABLE>
WHERE FINANCE>0
--CREATE GROUPS BY DETECTING STARTING ROW AND ENDING ROW (ENDING ROW BEING 1 LESS THAN NEXT STARTING ROW)
SELECT GROUP_N=ROW_NUMBER() OVER(ORDER BY LOW.R) --ASSIGN GROUP #S IN THE SAME ORDER AS THEY APPEAR IN THE SOURCE TABLE
,R_START=LOW.R, R_END=(HIG.R-1)
INTO #TEMP2
FROM #TEMP LOW
JOIN #TEMP HIG
ON LOW.R2=(HIG.R2-1)
UNION
--LAST GROUP STARTS AT THE LAST DETECTED STARTING ROW AND ENDS AT THE LAST ROW OF THE SOURCE TABLE
SELECT R_START_LAST_GROUP=(SELECT MAX(R) FROM #TEMP), R_END_LAST_GROUP=(SELECT MAX(R) FROM <SOURCE_TABLE>)
--GET SOURCE DATA AND APPEND GROUP #
SELECT DAT.*, GRP.GROUP_N
FROM #TEMP2 GRP
JOIN <SOURCE_TABLE> DAT
ON DAT.R BETWEEN GRP.R_START AND GRP.R_END