请教高手,此SQL如何写?

parss 2010-08-20 02:52:32
表tt中有两个字段,如下:

/*
AA BB
11 0
11 2
11 2
11 3
11 2
11 2
11 2
11 1

*/



用sql统计: bb字段中连续出现的次数大于2, 并且BB字段中的值小于2的不计算
如表中第一条记录不记,第二条记录开始统计,到第四条记录时BB字段为3而上面统计过值为2的记录一共2条,
则不记。从第五条记录开始,到第七条记录值都为2,则记为1.


/*
--结果应该显示为:

AA BB
---------
11 1
*/



...全文
132 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
parss 2010-08-23
  • 打赏
  • 举报
回复
感谢各位热心的朋友们,现在结帐~~~~
百年树人 2010-08-20
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([AA] int,[BB] int)
insert [tb]
select 11,0 union all
select 11,2 union all
select 11,2 union all
select 11,3 union all
select 11,2 union all
select 11,2 union all
select 11,2 union all
select 11,1
go

---查询---
;with josy as
(
select rn1=row_number() over(order by getdate()),* from tb
)
select a.AA,count(1) as BB
from
(select rn2=row_number() over(order by getdate()),*
from josy t
where not exists(select 1 from josy where bb=t.bb and rn1=t.rn1-1)
) a,
(select rn2=row_number() over(order by getdate()),*
from josy t
where not exists(select 1 from josy where bb=t.bb and rn1=t.rn1+1)
) b
where a.bb=b.bb and a.rn2=b.rn2 and b.rn1-a.rn1>=2
group by a.AA

---结果---
AA BB
----------- -----------
11 1

(1 行受影响)

sql2000用临时表,加个自增列
parss 2010-08-20
  • 打赏
  • 举报
回复
表中有一个creationtime(记录创建时间),使用的是sqlserver2005
guguda2008 2010-08-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xman_78tom 的回复:]

SQL code

-- 表中的记录应该用 identity 列标示顺序,依赖存储顺序是不可靠的。
declare @tab table(aa int,bb int);
insert into @tab (aa,bb)
select 11,0 union all select 11,2 union all
select 11,2 union all select 11,3 union al……
[/Quote]
学习了。。。原来可以用GETDATE排序的。。。
羽飞 2010-08-20
  • 打赏
  • 举报
回复
我也请教大侠们,顺序会一定吗?
guguda2008 2010-08-20
  • 打赏
  • 举报
回复
加了ROW_NUMBER物理排序就乱了啊
guguda2008 2010-08-20
  • 打赏
  • 举报
回复
UPDATE加个辅助列
IF OBJECT_ID('TB') IS NOT NULL DROP TABLE TB
GO
CREATE TABLE TB(AA INT,BB INT)
INSERT INTO TB
SELECT 11, 0 UNION ALL
SELECT 11, 2 UNION ALL
SELECT 11, 2 UNION ALL
SELECT 11, 3 UNION ALL
SELECT 11, 2 UNION ALL
SELECT 11, 2 UNION ALL
SELECT 11, 2 UNION ALL
SELECT 11, 1
GO
ALTER TABLE TB ADD CC INT
GO
DECLARE @AA INT,@BB INT,@CC INT
UPDATE TB SET @CC=CASE WHEN @AA=AA AND @BB=BB AND @BB>=2 THEN @CC+1 ELSE 1 END,@AA=AA,@BB=BB,CC=@CC

SELECT AA,COUNT(1) FROM TB
WHERE CC=3
GROUP BY AA
/*
11 1
*/
xman_78tom 2010-08-20
  • 打赏
  • 举报
回复

-- 表中的记录应该用 identity 列标示顺序,依赖存储顺序是不可靠的。
declare @tab table(aa int,bb int);
insert into @tab (aa,bb)
select 11,0 union all select 11,2 union all
select 11,2 union all select 11,3 union all
select 11,2 union all select 11,2 union all
select 11,2 union all select 11,1;

with t0 as(
select row_number() over (order by getdate()) id,aa,bb from @tab
),
t1 as(
select id,aa,bb,
id-ROW_NUMBER() over (partition by bb order by id) grp from t0
),
t2 as(
select id,aa,bb,count(1) over (partition by grp) cnt from t1
)
select aa,bb from t0
where id in (select max(id)+1 from t2
where cnt>2 and bb>=2);
SQLCenter 2010-08-20
  • 打赏
  • 举报
回复
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(AA int, BB int)
insert into #
select 11, 0 union all
select 11, 2 union all
select 11, 2 union all
select 11, 3 union all
select 11, 2 union all
select 11, 2 union all
select 11, 2 union all
select 11, 1

; with a as
(
select row=row_number()over(order by getdate()),* from #
),
b as
(
select id=row-row_number()over(partition by bb order by row),* from a where BB>=2
)
select AA,BB=1 from b group by id,AA having count(1)>2

/*
AA BB
----------- -----------
11 1
*/
lg314 2010-08-20
  • 打赏
  • 举报
回复
没有主键吗?
guguda2008 2010-08-20
  • 打赏
  • 举报
回复
游标或者UPDATE吧
nightmaple 2010-08-20
  • 打赏
  • 举报
回复
没看懂~~~

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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