带条件的t-sql分组数据

jdg4fx2g  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(430)

我有一些数据:
期间| id |账户|浮动|价值
24217 | 303003 | 0
24218 | 303003 | 0
24219 | 303003 | 1
24220 | 303003 | 1
24221 | 303003 | 0
24222 | 303003 | 0
我需要这样对这些数据进行分组:

  1. begin_period | end_period| id_account | float_value
  2. 24217 | 24218 | 303003 | 0
  3. 24219 | 24220 | 303003 | 1
  4. 24221 | 24222 | 303003 | 0

我尝试了按分区和while循环的行数,但没有成功。

ycggw6v2

ycggw6v21#

我把这理解为一个gaps和isald问题,您需要将“相邻”行组合在一起,即具有相同属性的行 float_value ,跨具有相同 id_account 以及 parameter .
在这里,我认为最简单的方法是使用行号之间的差异来计算每条记录属于哪个组:

  1. select
  2. min(period) begin_period,
  3. max(period) end_period,
  4. id_account,
  5. parameter,
  6. float_value
  7. from (
  8. select
  9. t.*,
  10. row_number() over(partition by id_account, parameter order by period) rn1,
  11. row_number() over(partition by id_account, parameter, float_value order by period) rn2
  12. from mytable t
  13. ) t
  14. group by id_account, parameter, float_value, rn1 -rn2
  15. order by id_account, parameter, begin_period

db小提琴演示:

  1. begin_period | end_period | id_account | parameter | float_value
  2. -----------: | ---------: | ---------: | :------------- | ----------:
  3. 24217 | 24218 | 303003 | ACCOUNT_STATUS | 0
  4. 24219 | 24220 | 303003 | ACCOUNT_STATUS | 1
  5. 24221 | 24222 | 303003 | ACCOUNT_STATUS | 0
展开查看全部
i34xakig

i34xakig2#

这是一个缺口和孤岛问题,一种方法是使用行号差异法:

  1. WITH cte AS (
  2. SELECT *, ROW_NUMBER() OVER (PARTITION BY ID_Account ORDER BY Period) rn1,
  3. ROW_NUMBER() OVER (PARTITION BY ID_Account, Float_Value ORDER BY Period) rn2
  4. FROM yourTable
  5. )
  6. SELECT
  7. MIN(Period) AS Begin_Period,
  8. MAX(Period) AS End_Period,
  9. ID_Account,
  10. MAX(Parameter) AS Parameter, -- assuming Parameter just always has the same value
  11. Float_Value
  12. FROM cte
  13. GROUP BY
  14. ID_Account,
  15. Float_Value,
  16. rn1 - rn2
  17. ORDER BY
  18. MIN(Period);

演示

展开查看全部
5cnsuln7

5cnsuln73#

你可以用 row_number 如下所示。这是演示。

  1. with cte as
  2. (
  3. select
  4. *,
  5. rn - row_number() over(order by Float_Value, rn) as nrnk
  6. from
  7. (
  8. select
  9. *,
  10. row_number() over (order by Period) as rn
  11. from Table1
  12. ) subq
  13. )
  14. select
  15. min(Period) as Begin_Period,
  16. max(Period) as End_Period,
  17. ID_Account,
  18. Parameter,
  19. Float_Value
  20. from cte
  21. group by
  22. ID_Account,
  23. Parameter,
  24. Float_Value,
  25. nrnk

输出:

  1. Begin_Period End_Period ID_Account Parameter Float_Value
  2. ---------------------------------------------------------------------
  3. 24217 24218 303003 ACCOUNT_STATUS 0
  4. 24221 24222 303003 ACCOUNT_STATUS 0
  5. 24219 24220 303003 ACCOUNT_STATUS 1
展开查看全部

相关问题