表aa有bb列(类型为char(1)),数据有百万条。
bb列的值只取值A或B。
现在需要按照ID顺序统计出现以下4种情况的次数
AAA
B
AB
AAB
---------------
例如
AAABBBABAB
统计后
AAA 1
B 3
AB 2
AAB 0
bb列的值只取值A或B。
现在需要按照ID顺序统计出现以下4种情况的次数
AAA
B
AB
AAB
---------------
例如
AAABBBABAB
统计后
AAA 1
B 3
AB 2
AAB 0
1.AA列就是ID列吗?
2.如果你的例子是指BB列的出现顺序,那结果似乎应该是:
AAA 1
B 5
AB 3
AAB 1
与你的结果不太一样.是不是要理解成:已经出现的就不算,那先统计的B,AB就统计不到了.
2.是的:已经出现的就不算,那先统计的B,AB就统计不到了.
就像游标一样,只前不后。
declare @t table (name varchar(20))
insert into @t select 'aaa'
insert into @t select 'b'
insert into @t select 'ab'
insert into @t select 'ab'
insert into @t select 'c'select t.name ,count_= (select count(*) from @t where name=t.name and (charindex('a',name)>0 or charindex('b',name)>0) )
from (select name from @t Group by name)t
/*
aaa 1
ab 2
b 1
c 0
*/
declare @t table (name varchar(20))
insert into @t select 'aaa'
insert into @t select 'b'
insert into @t select 'ab'
insert into @t select 'ab'
insert into @t select 'c'select t.name ,count_= (select count(*) from @t where name=t.name)
from (select 'AAA' name union select 'B'union select 'AB' union select 'AAB')t
/*
AAA 1
AAB 0
AB 2
B 1
*/
insert into @t select 'aaa'
insert into @t select 'b'
insert into @t select 'ab'
insert into @t select 'ab'
insert into @t select 'c'select t.name ,count_= (select count(*) from @t where name=t.name)
from (select 'AAA' name union select 'B'union select 'AB' union select 'AAB')t order by count_ desc
/*
AB 2
B 1
AAA 1
AAB 0
*/
例如
AAABBBABAB
统计后
AAA 1
B 3
AB 2
AAB 0 ----你这个是一条数据‘AAABBBABAB ’就要统计出
AAA 1
B 3
AB 2
AAB 0 这个吗?
例如
ID bb
1 A
2 A
3 A
4 B
5 B
6 B
7 A
8 B
9 A
10 B
统计后
AAA 1
B 3
AB 2
AAB 0