增量组BY

时间:2015-10-09 11:22:40

标签: sql sql-server tsql

如何在查询中实现增量分组?

enter image description here

我需要将所有非零值分组到不同的命名组中。

请帮我根据列日期和订阅者编写查询。

2 个答案:

答案 0 :(得分:1)

如果你有SQL Server 2012或更新版本,你可以使用Windows函数的一些技巧来获得这种没有游标的分组,如下所示:

select
  Date, Subscribers,
  case when Subscribers = 0 then 'No group'
  else 'Group' + convert(varchar, GRP) end as GRP
from (
  select
    Date, Subscribers,
    sum (GRP) over (order by Date asc) as GRP
  from (
    select
      *,
      case when Subscribers > 0 and 
        isnull(lag(Subscribers) over (order by Date asc),0) = 0 then 1 else 0 end as GRP
    from SubscribersCountByDay S
  ) X
) Y

https://developer.chrome.com/multidevice/webview/gettingstarted#loading_html_files_from_the_file_system

中的示例

答案 1 :(得分:1)

在足以支持CTE的SQL Server版本中,您可以使用以下无光标查询:

-- Sample data.
declare @SampleData as Table ( Id Int Identity, Subscribers Int );
insert into @SampleData ( Subscribers ) values
  -- ( 0 ), -- Test edge case when we have a zero first row.
  ( 200 ), ( 100 ), ( 200 ),
  ( 0 ), ( 0 ), ( 0 ),
  ( 50 ), ( 50 ), ( 12 ),
  ( 0 ), ( 0 ),
  ( 43 ), ( 34 ), ( 34 );
select * from @SampleData;

-- Run the query.
with ZerosAndRows as (
  -- Add   IsZero   to indicate zero/non-zero and a row number to each row.
  select Id, Subscribers,
    case when Subscribers = 0 then 0 else 1 end as IsZero,
    Row_Number() over ( order by Id ) as RowNumber
    from @SampleData ),
  Groups as (
  -- Add a group number to every row.
  select Id, Subscribers, IsZero, RowNumber, 1 as GroupNumber
    from ZerosAndRows
    where RowNumber = 1
  union all
  select FAR.Id, FAR.Subscribers, FAR.IsZero, FAR.RowNumber,
    -- Increment   GroupNumber   only when we move from a non-zero row to a zero row.
    case when Groups.IsZero = 1 and FAR.IsZero = 0 then Groups.GroupNumber + 1 else Groups.GroupNumber end
    from ZerosAndRows as FAR inner join Groups on Groups.RowNumber + 1 = FAR.RowNumber
  )
-- Display the results.
select Id, Subscribers,
  case when IsZero = 0 then 'no group' else 'Group' + Cast( GroupNumber as VarChar(10) ) end as Grouped
  from Groups
  order by Id;

要查看中间结果,只需将最终select替换为select * from FlagsAndRowsselect * from Groups