Q
增量組BY BY
-4
A
回答
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
1
總的來說,我提倡不贊同遊標,但在這種情況下,它生病沒有受到傷害,因爲它不適合迭代,總結並且一次完成條件。 另請注意我用FAST_FORWARD
暗示它不會降低性能。
我猜你確實想要@HABO評論。
查看下面的工作示例,它只是總結,直到找到零,重置並重新開始。請注意0處理第一行爲零的情況。
create table dbo.SubscribersCountByDay
(
[Date] date not null
,Subscribers int not null
)
GO
insert into dbo.SubscribersCountByDay
([Date], Subscribers)
values
('2015-10-01', 1)
,('2015-10-02', 2)
,('2015-10-03', 0)
,('2015-10-04', 4)
,('2015-10-05', 5)
,('2015-10-06', 0)
,('2015-10-07', 7)
GO
declare
@Date date
,@Subscribers int
,@Sum int = 0
,@GroupId int = 1
declare @Result as Table
(
GroupName varchar(10) not null
,[Sum] int not null
)
declare ScanIt cursor fast_forward
for
(
select [Date], Subscribers
from dbo.SubscribersCountByDay
union
select '2030-12-31', 0
) order by [Date]
open ScanIt
fetch next from ScanIt into @Date, @Subscribers
while @@FETCH_STATUS = 0
begin
if (@Subscribers = 0 and @Sum > 0)
begin
insert into @Result (GroupName, [Sum]) values ('Group ' + cast(@GroupId as varchar(6)), @Sum)
set @GroupId = @GroupId + 1
set @Sum = 0
end
else begin
set @Sum = @Sum + @Subscribers
end
fetch next from ScanIt into @Date, @Subscribers
end
close ScanIt
deallocate ScanIt
select * from @Result
GO
對於OP:請在下一次寫表,剛剛發佈的圖像是懶惰
1
在現代足以支持熱膨脹係數可以用一個個版本的SQL Server E採用無遊標查詢:
-- 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 * from FlagsAndRows
或select * from Groups
更換最終select
。
相關問題
- 1. SQL小組由by by by?
- 2. SQL增量SELECT column by +1
- 3. Mysql組by by order by not working
- 4. group by by by by mysql
- 5. SQL Group By By By By Count
- 6. Mysql group by by by by?
- 7. order by by group by
- 8. Doctrine2 Order By By Group By
- 9. LINQ group by by by C#
- 10. 如何組合GROUP BY,ORDER BY和HAVING
- 11. 多組by by elasticsearch包括缺失值
- 12. SQL`組by`與`爲了by`性能
- 13. ORDER BY GROUP BY
- 14. group by by desc
- 15. group by by rollup
- 16. group by by django
- 17. $ group by by undefined
- 18. 自動遞增GROUP BY後一個虛擬列,ORDER BY查詢
- 19. MySQL的GROUP BY量
- 20. SQL ORDER BY GROUP BY
- 21. GROUP BY後ORDER BY
- 22. SQL ORDER BY GROUP BY
- 23. Rails/activerecords - group by by
- 24. GROUP BY和ORDER BY
- 25. 多GROUP BY和BY
- 26. 列(PARTITION BY .... ORDER BY ...)
- 27. MySQL:ORDER BY FIELD/GROUP BY
- 28. distinct或group by by
- 29. Group by by Google BigQuery
- 30. group by by multiple colums
請解釋你的輸出。 – DarkKnight
爲'group1','group2'添加表格定義,樣本數據和您的查詢 – Backs
...瞭解序列,對於'無組',它位於行的頂部。 – Ajay2707