| |

VerySource

 Forgot password?
 Register
Search
View: 2327|Reply: 16

Simple statistical problem

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-8 07:20:01
| Show all posts |Read mode
create table #t (class varchar (20), spendtime int, orderindex int identity)
insert #t
select 'A', 1 union all
select 'B', 1 union all
select 'B', 2 union all
select 'A', 2 union all
select 'B', 2
select * from #t
--Requires that the result is the same for multiple adjacent classes, and the spendtime is added
A 1
B 3
A 2
B 2
--drop table #t
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-9 06:45:02
| Show all posts
select *, 0 refindex into #a from #t

declare @class varchar (10), @spendtime int, @refindex int
select @class = '', @ spendtime = 0, @ refindex = 0

update #a set @ spendtime = case when class = @ class then @ spendtime + spendtime else spendtime end
, spendtime = @ spendtime, @ refindex = case when class = @ class then @refindex else @ refindex + 1 end
, refindex = @ refindex, @ class = class

select class, max (spendtime)
from #a
group by class, refindex
Reply

Use magic Report

0

Threads

100

Posts

53.00

Credits

Newbie

Rank: 1

Credits
53.00

 China

Post time: 2020-1-9 23:36:01
| Show all posts
Not too complicated.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-10 11:36:01
| Show all posts
Learn
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-10 12:09:02
| Show all posts
create table #t (class varchar (20), spendtime int, orderindex int identity)
insert #t
select 'A', 1 union all
select 'B', 1 union all
select 'B', 2 union all
select 'A', 2 union all
select 'B', 2
--Inquire

select ta.class, spendtime = case when ta.orderindex = tb.orderindex + 1
then isnull (ta.spendtime, 0) + isnull (tb.spendtime, 0)
else isnull (ta.spendtime, 0) end
from
(
select * from #t a
where not exists (select 1 from #t where class = a.class and orderindex = a.orderindex + 1)
) ta
left join
(
select * from #t a
where exists (select 1 from #t where class = a.class and orderindex = a.orderindex + 1)
) tb
on ta.class = tb.class
--result
/ *
class spendtime
-------------------- -----------
A 1
B 3
A 2
B 2

(4 rows affected)

* /
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-10 12:54:02
| Show all posts
Learn
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-10 16:00:01
| Show all posts
? ?
Reply

Use magic Report

0

Threads

49

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-1-10 17:36:01
| Show all posts
select class, sum (spendtime) as spendtime
from
(
select class, spendtime,
orderindex = (select isnull (max (orderindex), 0) + 1 from #t where orderindex <a.orderindex and class <> a.class)
from #t a
tt
group by class, orderindex


A 1
B 3
A 2
B 2
Reply

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-10 19:45:02
| Show all posts
up
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-10 22:27:01
| Show all posts
Processing with CTE:
with temp (class, spendtime, orderindex, refindex)
as
(
select *, orderindex from #t a
where not exists (select 1 from #t where class = a.class and orderindex = a.orderindex-1)

union all

select a.class, a.spendtime, a.orderindex, b.refindex from #t a, temp b
where a.class = b.class and a.orderindex = b.orderindex + 1
)

select class, sum (spendtime) as spendtime from temp group by class, refindex
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list