****求一个SQL语句的写法****正解马上结贴****

小辉 2005-08-24 10:24:02
数据表:
TestTime TestVal

2005/7/6 0:00:00 2
2005/7/6 0:00:10 5
2005/7/6 0:00:20 2

2005/7/6 0:00:30 2
2005/7/6 0:00:40 1
2005/7/6 0:00:50 3

2005/7/6 0:01:00 5
2005/7/6 0:01:10 3
2005/7/6 0:01:20 1

2005/7/6 0:01:30 1
2005/7/6 0:01:40 1
2005/7/6 0:01:40 4
。 。
。 。
。 。

需要结果1:
说明:取每三条的TestVal的最大值,TestTime取最小值
TestTime TestVal

2005/7/6 0:00:00 5
2005/7/6 0:00:30 3
2005/7/6 0:01:00 5
2005/7/6 0:01:30 4

需要结果2:
说明:取每三条的TestVal的平均值,TestTime取最小值
TestTime TestVal

2005/7/6 0:00:00 3
2005/7/6 0:00:30 2
2005/7/6 0:01:00 3
2005/7/6 0:01:30 2
...全文
433 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
lnzyquan 2005-08-24
  • 打赏
  • 举报
回复
原作者:开发者俱乐部,数据库斑竹:潇湘剑公子

见:http://www.dev-club.com/club/bbs/showannounce.asp?id=2532802
lnzyquan 2005-08-24
  • 打赏
  • 举报
回复
转贴别人的答案,我自己不会啊!

--生成测试数据
create table #t(TestTime datetime,TestVal int)
insert into #t select '2005/07/06 00:00:00',2
insert into #t select '2005/07/06 00:00:10',5
insert into #t select '2005/07/06 00:00:20',2
insert into #t select '2005/07/06 00:00:30',2
insert into #t select '2005/07/06 00:00:40',1
insert into #t select '2005/07/06 00:00:50',3
insert into #t select '2005/07/06 00:01:00',5
insert into #t select '2005/07/06 00:01:10',3
insert into #t select '2005/07/06 00:01:20',1
insert into #t select '2005/07/06 00:01:30',1
insert into #t select '2005/07/06 00:01:40',1
insert into #t select '2005/07/06 00:01:40',4



--执行查询:取每三条的TestVal的最大值,TestTime取最小值
select
TestTime = min(TestTime),
TestVal = max(TestVal)
from
(select
a.*,
cnt=isnull(count(b.TestTime),0)
from
#t a,
#t b
where
a.TestTime>b.TestTime
group by
a.TestTime,a.TestVal) c
group by
(c.cnt/3)

--输出结果
/*
TestTime TestVal
------------------- -------
2005-07-06 00:00:10 5
2005-07-06 00:00:30 3
2005-07-06 00:01:00 5
2005-07-06 00:01:30 4
*/


--执行查询:取每三条的TestVal的平均值,TestTime取最小值
select
TestTime = min(TestTime),
TestVal = avg(TestVal)
from
(select
a.*,
cnt=isnull(count(b.TestTime),0)
from
#t a,
#t b
where
a.TestTime>b.TestTime
group by
a.TestTime,a.TestVal) c
group by
(c.cnt/3)

--输出结果
/*
TestTime TestVal
------------------- -------
2005-07-06 00:00:10 3
2005-07-06 00:00:30 2
2005-07-06 00:01:00 3
2005-07-06 00:01:30 2
*/
liuyang1981 2005-08-24
  • 打赏
  • 举报
回复
jinjazz(近身剪(充电中...))

的方法还是很简单的
zhongwanli 2005-08-24
  • 打赏
  • 举报
回复
MARK
liuyang1981 2005-08-24
  • 打赏
  • 举报
回复
存储过程中建临时表,添加一标识字段,然后使用循环为每三行添加1到n为标识,n为表总行数/3 的整数部分,除不尽的如何处理随意~~
再group 就很简单了
xn000 2005-08-24
  • 打赏
  • 举报
回复
jinjazz(近身剪(充电中...))

已经解决了呀。
TechEye 2005-08-24
  • 打赏
  • 举报
回复
只能接时间顺序进行分组,要写一个存储过程.
小辉 2005-08-24
  • 打赏
  • 举报
回复
各位大虾,忘记说了,不用加ID的方法,数据库现在已经定了,不能改动了。。。。
lnzyquan 2005-08-24
  • 打赏
  • 举报
回复
设计确实存在缺陷
Ivony 2005-08-24
  • 打赏
  • 举报
回复
有ID应该是可行的。
用ID / 3 分组。
jinjazz 2005-08-24
  • 打赏
  • 举报
回复
create table test(TestTime varchar(20), TestVal int)

insert into test
select '2005/7/6 0:00:00', 2 union
select '2005/7/6 0:00:10', 5 union
select '2005/7/6 0:00:20', 2 union

select '2005/7/6 0:00:30', 2 union
select '2005/7/6 0:00:40', 1 union
select '2005/7/6 0:00:50', 3 union

select '2005/7/6 0:01:00', 5 union
select '2005/7/6 0:01:10', 3 union
select '2005/7/6 0:01:20', 1 union

select '2005/7/6 0:01:30', 1 union
select '2005/7/6 0:01:40', 1 union
select '2005/7/6 0:01:40', 4


select id=identity(int,1,1),* into #t from test

--select * from #t
--1.
select TestTime=min(TestTime),TestVal=max(TestVal) from #t
group by ceiling(cast(id as float)/3)

/*--
TestTime TestVal
-------------------- ----------
2005/7/6 0:00:00 5
2005/7/6 0:00:30 3
2005/7/6 0:01:00 5
2005/7/6 0:01:30 4
--*/

--2.

select TestTime=min(TestTime),TestVal=avg(TestVal) from #t
group by ceiling(cast(id as float)/3)

/*--
TestTime TestVal
-------------------- -----------
2005/7/6 0:00:00 3
2005/7/6 0:00:30 2
2005/7/6 0:01:00 3
2005/7/6 0:01:30 2
--*/

drop table #t
drop table test
azsoft 2005-08-24
  • 打赏
  • 举报
回复
select top 3 * from 表 order by TestVal Desc,TestTime ASC

参考 Grouping 分组 或 用上面的语句生成临时表,视图也行
pupo 2005-08-24
  • 打赏
  • 举报
回复
你的数据表就设计的不够合理,分组的条件都没有规律的,怎么分,加个分组字段吧
navy_koo 2005-08-24
  • 打赏
  • 举报
回复
是SQLServer数据库吗?

我觉得要写函数的,一句话可能不大好写哦...
lovefootball 2005-08-24
  • 打赏
  • 举报
回复
就算有个id也比较麻烦啊
我先试试~~~~~
okyzx 2005-08-24
  • 打赏
  • 举报
回复
看不懂
小辉 2005-08-24
  • 打赏
  • 举报
回复
就是1-3,4-6,7-8.......
这么分组
不知道能不能实现
baya 2005-08-24
  • 打赏
  • 举报
回复
仅两个字段就可以取出来?怎么分组啊?关注!
小辉 2005-08-24
  • 打赏
  • 举报
回复
太感谢各位了,小弟还要好好加油学习阿。。。。。。
马上给分。。。
lnzyquan 2005-08-24
  • 打赏
  • 举报
回复
感叹高手如林啊,值得学习啊

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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