| |

VerySource

 Forgot password?
 Register
Search
Author: yrasd11

Simple statistical problem

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-10 22:36:01
| Show all posts
TO:gtolishun()

Requires that the results are the same for multiple adjacent classes.
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-10 23:18:02
| Show all posts
No upstairs, three or more in a row is not right


select class, sum (spendtime) as spendtime
from
(select class, spendtime, (select isnull (max (orderindex), 0) from #t where orderindex <t.orderindex and class <> t.class) as a from #t t) tt
group by class, a
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-10 23:27: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 'B', 1 union all
select 'B', 2 union all
select 'A', 2 union all
select 'B', 2
go

--query
select class, sum (spendtime) as spendtime
from
(select class, spendtime, (select isnull (max (orderindex), 0) from #t where orderindex <t.orderindex and class <> t.class) as a from #t t) tt
group by class, a


--result
A 1
B 6
A 2
B 2
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-11 11:00:02
| Show all posts
-Modify it.
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 class, spendtime = sum (spendtime), orderindex = max (orderindex) from #t a
where exists (select 1 from #t where class = a.class and orderindex = a.orderindex + 1)
group by class
) tb
on ta.class = tb.class
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-11 11:36:01
| Show all posts
michealhenrydrops like me, hehe
Reply

Use magic Report

0

Threads

49

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-1-11 19:00:01
| Show all posts
Oh, just seeing your test results is different
Didn't see your test data different! Ha ha

Then I support you!
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-13 13:18:01
| Show all posts
Haha ~
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