很久没来了,散分&求两条语句,12小时揭帖!

zippro123 2008-09-16 08:56:39
数据结构

a b c
1 x1 11
2 x2 15
3 x3 15
4 x3 14
5 x2 15
6 x4 13


第一条sql,统计列b中有几种情况(题中有x1,x2,x3,x4共四种)
第二条,查询C最大值的最大连续记录集(查询出2和3行),好像有点难,这条给50分
...全文
581 96 打赏 收藏 转发到动态 举报
写回复
用AI写文章
96 条回复
切换为时间正序
请发表友善的回复…
发表回复
gulunhua 2008-09-26
  • 打赏
  • 举报
回复
set nocount on
declare @temp table (k1 int identity,b varchar(10),c1 int)

insert into @temp values('x1',11)
insert into @temp values('x2',13)
insert into @temp values('x3',15)
insert into @temp values('x3',15)
insert into @temp values('x2',12)
insert into @temp values('x9',10)
insert into @temp values('x1',11)
insert into @temp values('x6',15)
insert into @temp values('x3',15)
insert into @temp values('x5',12

declare @max int
select @max=max(c1) from @temp

declare @temp_max table(starts int,ends int,diff int )

insert into @temp_max
select starts=v.k1,ends=isnull(min(case when v.k1<a.k1 and a.c1<>@max then a.k1 else null end )-1,max(case when v.k1<a.k1 then a.k1 else null end)),
isnull(min(case when v.k1<a.k1 and a.c1<>@max then a.k1 else null end )-1,max(case when v.k1<a.k1 then a.k1 else null end))-v.k1 as diff
from @temp v join @temp a on v.c1=@max
group by v.k1
having isnull(min(case when v.k1<a.k1 and a.c1<>@max then a.k1 else null end )-1,max(case when v.k1<a.k1 then a.k1 else null end))
>=v.k1 and
isnull(max(case when v.k1>a.k1 and a.c1<>@max then a.k1 else null end )+1,min(case when v.k1>a.k1 then a.k1 else null end))
=v.k1

select *
from @temp t
where exists (select * from @temp_max where diff=(select max(diff) from @temp_max) and (t.k1 between starts and ends))

运行结果如下:
k1 b c1
----------- ---------- -----------
3 x3 15
4 x3 15
8 x6 15
9 x3 15
michael_sw 2008-09-18
  • 打赏
  • 举报
回复
75楼 的算法 有2个相同的个数的 连续最大集 就会出现错误
Tortoise1023 2008-09-18
  • 打赏
  • 举报
回复
mark.............下次试试
mike1lainet 2008-09-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wenmingcool 的回复:]
接分
[/Quote]


只有引用的内容不允许回复!!
gulunhua 2008-09-18
  • 打赏
  • 举报
回复
就算在表中插入3个连续的集合也没问题的,插入N个也一样的,实在是看不出来在哪儿有问题:

insert into tablename values(2,'x2',15) --连续的15
insert into tablename values(3,'x3',15) --连续的15

insert into tablename values(4,'x3',14)
insert into tablename values(5,'x2',15)
insert into tablename values(6,'x4',13)

insert into tablename values(7,'x4',15) --连续的15
insert into tablename values(8,'x6',15) --连续的15
insert into tablename values(9,'x4',14)

insert into tablename values(10,'x88',15) --连续的15
insert into tablename values(11,'x89',15) --连续的15

运行上面的程序,得到的结果是:
a b c 连续的个数
----------- ---------- ----------- -----------
2 x2 15 2
3 x3 15 2
NULL NULL NULL 2
7 x4 15 2
8 x6 15 2
NULL NULL NULL 2
10 x88 15 2
11 x89 15 2

(所影响的行数为 8 行)


如果把上面的插入的记录改成:
insert into tablename values(2,'x2',15) --连续的15
insert into tablename values(3,'x3',15) --连续的15

insert into tablename values(4,'x3',16) --改为16
insert into tablename values(5,'x2',15)
insert into tablename values(6,'x4',13)

insert into tablename values(7,'x4',15) --连续的15
insert into tablename values(8,'x6',15) --连续的15
insert into tablename values(9,'x4',16) --改为16

insert into tablename values(10,'x88',15) --连续的15
insert into tablename values(11,'x89',15) --连续的15

那得到的结果是:
a b c 连续的个数
----------- ---------- ----------- -----------
4 x3 16 1
NULL NULL NULL 1
9 x4 16 1

(所影响的行数为 3 行)
这个也没问题的
gulunhua 2008-09-18
  • 打赏
  • 举报
回复
[Quote=引用 92 楼 michael_sw 的回复:]
75楼 的算法 有2个相同的个数的 连续最大集 就会出现错误
[/Quote]

我刚有运行了一下啊,没有问题呀

如果在表中插入了连续的:
insert into tablename values(1,'x1',11)

insert into tablename values(2,'x2',15) --连续的15
insert into tablename values(3,'x3',15) --连续的15

insert into tablename values(4,'x3',14)
insert into tablename values(5,'x2',15)
insert into tablename values(6,'x4',13)

insert into tablename values(7,'x4',15) --连续的15
insert into tablename values(8,'x6',15) --连续的15
insert into tablename values(9,'x4',14)


那么你如果运行下面的原来sql的话,最后结果是:

a b c 连续的个数
----------- ---------- ----------- -----------
2 x2 15 2
3 x3 15 2
NULL NULL NULL 2
7 x4 15 2
8 x6 15 2

(所影响的行数为 5 行)


--原来的sql
create table tablename(a int ,b varchar(10),c int)
insert into tablename values(1,'x1',11)

insert into tablename values(2,'x2',15) --连续的15
insert into tablename values(3,'x3',15) --连续的15

insert into tablename values(4,'x3',14)
insert into tablename values(5,'x2',15)
insert into tablename values(6,'x4',13)

insert into tablename values(7,'x4',15) --连续的15
insert into tablename values(8,'x6',15) --连续的15
insert into tablename values(9,'x4',14)

declare @temp_max table(a int ,b varchar(10),c int)
declare @temp table(a int ,b varchar(10),c int)

declare @maxcount int
set @maxcount=0

declare @max_c int
select @max_c=max(c) from tablename

declare @recordcount int
set @recordcount=0

declare @a int
declare @b varchar(10)
declare @c int

declare max_cursor cursor for
select * from tablename
open max_cursor
fetch max_cursor into @a,@b,@c

while @@fetch_status =0
begin
if(@c=@max_c)
begin
insert into @temp values(@a,@b,@c)
set @recordcount=@recordcount+1
fetch max_cursor into @a,@b,@c

while @@fetch_status=0
begin
if(@c=@max_c)
begin
insert into @temp values(@a,@b,@c)
fetch max_cursor into @a,@b,@c
set @recordcount=@recordcount+1
end
else
begin

break
end
end
end

if(@maxcount=@recordcount and @maxcount<>0)
begin
if(@maxcount>=1)
insert into @temp_max values(null,null,null)
insert into @temp_max
select * from @temp
set @recordcount=0
delete from @temp
end
else
begin
if(@maxcount<@recordcount)
begin
delete from @temp_max

insert into @temp_max
select * from @temp

delete from @temp
set @maxcount=@recordcount
set @recordcount=0
end
else
set @recordcount=0
delete from @temp
end
fetch max_cursor into @a,@b,@c

end

close max_cursor
deallocate max_cursor

select a,b,c,@maxcount as '连续的个数' from @temp_max
datahandler2 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 yygyogfny 的回复:]
/*
a b c
1 x1 11
2 x2 15
3 x3 15
4 x3 14
5 x2 15
6 x4 13
*/


create table #temp
(
a int identity(1,1),
b varchar(20),
c int
)

insert into #temp
select 'x1',11
union all
select 'x2',15
union all
select 'x3',15
union all
select 'x3',14
union all
select 'x2',15
union all
select 'x4',13

select * from #t…
[/Quote]

=============第2个好像是错的吧
datahandler2 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yangpeiyu 的回复:]
第一条可以
select count(*) from
(
select b from 表 group by b
) as C
[/Quote]
好算法~~~~~~~~
michael_sw 2008-09-17
  • 打赏
  • 举报
回复
呵呵 结贴了呀!!!昨天没时间 今天弄了一下 感觉还是很麻烦 不过要比71楼的简单一些
create table #temp
(
a int identity(1,1),
b varchar(20),
c int
)

insert into #temp
select 'x1',11
union all
select 'x2',15
union all
select 'x3',15
union all
select 'x3',15
union all
select 'x2',13
union all
select 'x4',13
union all
select 'x4',15
union all
select 'x4',15

--第二种的解决方案
--定义临时表b
declare @b table
(
fid int, --起始位置的id
lid int --结束位置的id
)
declare @a int
declare @i int
declare @j int
declare @d int
set @j=0
--定义游标
declare colaa cursor
for
select a from #temp where c=(select max(c) from #temp) order by a
open colaa
fetch next from colaa into @a
while(@@FETCH_STATUS<>-1)
begin
IF(@@FETCH_STATUS<>-2)
begin
if(@j=0)--第一条
begin
insert into @b values(@a,0) --插入起始位置的id
print @a
set @j=1--标志位
end
else
begin
set @d=(select count(*)from @b)
if(@d<>0 and (@a-1)in(select a from #temp where c=(select max(c) from #temp)))--连续的情况,
begin
update @b set lid=@a where fid=(select top 1 fid from @b order by fid desc) --插入结束位置的id
print @a
end
else--下一下最大值的开始id
begin
insert into @b values(@a,0)--插入下一下最大值的开始id
print @a
end
end
end
fetch next from colaa into @a
end
--关闭游标
close colaa
deallocate colaa

select * from @b

select max(lid-fid)as 连续数 from @b
--查找最大集合
select * from #temp a, @b b where a.a>=b.fid and a.a<=b.lid and (b.lid-b.fid)=(select max(lid-fid)as 连续数 from @b)
gulunhua 2008-09-16
  • 打赏
  • 举报
回复
用sql server 2000
第一题,显示出明细:select b from tablename group by b
显示个数: select count(b) from tablename group by b
zpcoder 2008-09-16
  • 打赏
  • 举报
回复

1. select count(distnict(b)) from 表
2. select a,max(c) from 表 order by c desc
mjjzg 2008-09-16
  • 打赏
  • 举报
回复
jf
michael_sw 2008-09-16
  • 打赏
  • 举报
回复
[Quote=引用 60 楼 hxf66888 的回复:]
第二个:


SELECT * FROM BB WHERE (A001+1 IN(
SELECT A001 FROM BB WHERE A003=(select max(A003) AS A003 FROM BB) ) OR A001-1 IN(
SELECT A001 FROM BB WHERE A003=(select max(A003) AS A003 FROM BB) ) ) AND A003=(select max(A003) AS A003 FROM BB)


SQL2000
我试过的,完全可以查出来.
[/Quote]
这也只是有一个连续集的情况吧!
hxf66888 2008-09-16
  • 打赏
  • 举报
回复
第二个:


SELECT * FROM BB WHERE (A001+1 IN(
SELECT A001 FROM BB WHERE A003=(select max(A003) AS A003 FROM BB) ) OR A001-1 IN(
SELECT A001 FROM BB WHERE A003=(select max(A003) AS A003 FROM BB) ) ) AND A003=(select max(A003) AS A003 FROM BB)


SQL2000
我试过的,完全可以查出来.
zl58859173 2008-09-16
  • 打赏
  • 举报
回复
学习下。
gavinluo 2008-09-16
  • 打赏
  • 举报
回复
select count(*) from 
(
select b from 表 group by b
)
没看懂第二条
zippro123 2008-09-16
  • 打赏
  • 举报
回复
[Quote=引用 54 楼 michael_sw 的回复:]
引用 12 楼 yygyogfny 的回复:
/*
select * from #temp a
where c = (select max(c) from #temp b where a>a.a )

a b c
----------- -------------------- -----------
2 x2 15
3 x3 15


这样只是在这个特定的情况可以,如果
a b c
1 x1 11
2 x2 15
3 x3 15
4 x3 15
5 x2 15

[/Quote]

是这样的!
sunrain_yingxi 2008-09-16
  • 打赏
  • 举报
回复
呵呵,只會第一個
第二個幫頂
CCjian 2008-09-16
  • 打赏
  • 举报
回复
学习
michael_sw 2008-09-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 yygyogfny 的回复:]
/*
select * from #temp a
where c = (select max(c) from #temp b where a>a.a )

a b c
----------- -------------------- -----------
2 x2 15
3 x3 15

[/Quote]
这样只是在这个特定的情况可以,如果
a b c
1 x1 11
2 x2 15
3 x3 15
4 x3 15
5 x2 15
6 x4 13

就不行
a b c
----------- -------------------- -----------
2 x2 15
3 x3 15
4 x3 15
只查出来3条
而且楼主说的是 最大值的最大连续记录集, 所以也有可能有多个连续最大值区域呢, 简单的sql语句应该不行,初步考虑用游标来实现
加载更多回复(74)

62,052

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧