100分请教:如何统计查询,需要嵌套吗?

hndsome007 2003-08-22 11:22:53
TIME ID STATUS1 STATUS2
8:00 11 0 1
9:00 11 1 2
9:05 12 0 1
9:08 13 1 2
10:00 13 2 1

说明如下:
ID是唯一的,我想做的是:对于一个ID统计它最初时间的STATUS1和最末时间的STATUS2,
比如对于ID=11的,我只需要统计STATUS1=0的信息和STATUS=2的信息,一个ID只统计一次
,这个SELECT语句怎样写?怎样统计?
兄弟是菜鸟,多谢高手指点!
...全文
47 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxiangh 2003-08-22
  • 打赏
  • 举报
回复
select c.id,c.statu1,d.statu2 from
(select a.id,a.statu1 from jl a,(select id,min(time) time0 from jl group by id) b
where a.id=b.id and a.time=b.time0) c,
(select a.id,a.statu2 from jl a,(select id,max(time) time0 from jl group by id) b
where a.id=b.id and a.time=b.time0) d
where c.id=d.id

调试通过!
pieroshi 2003-08-22
  • 打赏
  • 举报
回复
select table.id,a.status1,b.status2
from
table
(select
id,
status1
from
(select min(time) min_time,
id from table
group by id) min,
table
where table.time=min.min_time and table.id=min_time.id(+)) a,
(select
id,
status2
from
(select max(time) max_time,
id from table
group by id) max,
table
where table.time=max.max_time and table.id=max_time.id(+)) b
where table.id=a.id(+) and table.id=b.id(+)

zjhclf 2003-08-22
  • 打赏
  • 举报
回复
学习,我顶!
armu80830 2003-08-22
  • 打赏
  • 举报
回复
select id,min(status1) as status1,max(status2) as status2 from Table
group by id
hndsome007 2003-08-22
  • 打赏
  • 举报
回复
能否得到这样的结果呢?
ID STATUS1 STATUS2
11 0 2
12 0 1
13 1 1

其中ID值不在WHERE中出现
linuxyf 2003-08-22
  • 打赏
  • 举报
回复
select status1 from table where dt =(select min(time) from table where id ='11') and id ='11';
select status2 from table where dt =(select max(time) from table where id ='11') and id ='11';
两条统计语句
hndsome007 2003-08-22
  • 打赏
  • 举报
回复
zxiangh,你在检索的时候是对的
hndsome007 2003-08-22
  • 打赏
  • 举报
回复
zxiangh,你好!非常感谢你!
你的方法基本上可行,但不妨把问题稍微复杂一下:
比如当ID=15时
在5个不同的时间里状态不断的变化:

TIME ID STATUS1 STATUS2
3:00 15 2 0
4:00 15 0 1
5:05 15 1 2
6:08 15 2 1
7:00 15 1 0
这五种情况只能作为一种情况:即3:00的STATUS1=2和
7:00的STATUS2=0的统计
hndsome007 2003-08-22
  • 打赏
  • 举报
回复
TsuLeon,你好。
不好意思,上面我的分类不够精确,
当你采用count(distinct id)时,针对下面的情况
TIME ID STATUS1 STATUS2
8:00 11 0 1
9:00 11 1 2
你选哪一行呢?因为ID一样,我需要的是8:00时的STATUS1和9:00时的STATUS2,
即这两行要综合起来统计,即一个0-2的情形。
上面zxiangh的方法分类比较细
TsuLeon 2003-08-22
  • 打赏
  • 举报
回复
不好意思,没看清是五种情况
修改如下:

select status1 || '-' || status2 "status1-status2",
count(distinct id) "ID Count"
from t
where t.status1 || '-' || t.status2 = '0-1' or
t.status1 || '-' || t.status2 = '1-2' or
t.status1 || '-' || t.status2 = '2-1' or
t.status1 || '-' || t.status2 = '1-0'
group by status1 || '-' || status2
union
select 'status1=status2',
count(distinct id)
from t
where t.status1 = t.status2
TsuLeon 2003-08-22
  • 打赏
  • 举报
回复
select status1 || '-' || status2 "status1-status2",
count(distinct id) "ID Count"
from t
where t.status1 != t.status2
group by status1 || '-' || status2
union
select 'status1=status2',
count(distinct id)
from t
where t.status1 = t.status2
zxiangh 2003-08-22
  • 打赏
  • 举报
回复
select statu,count(*) from
(select id,to_char(statu1)||'--'||to_char(statu2) statu from
(select c.id,c.statu1,d.statu2 from
(select a.id,a.statu1 from jl a,(select id,min(time) time0 from jl group by id) b
where a.id=b.id and a.time=b.time0) c,
(select a.id,a.statu2 from jl a,(select id,max(time) time0 from jl group by id) b
where a.id=b.id and a.time=b.time0) d
where c.id=d.id)
)
group by statu

不知是否符合你所要求的,总之调试通过!
hndsome007 2003-08-22
  • 打赏
  • 举报
回复
ok!
thank you!
进一步再问问:
现在我要分五种情况统计
即:比较STATUS1和STATUS2
统计 ,前面是STATUS1,后面是STATUS2 0 -1,1-2,2-1,1-0,和前后相等(0-01-1,2-2)五种情况的ID个数,同样每个ID都只能被统计一次

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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