oracle查询语句max函数,求教一个sql语句关于min,max函数查询的有关问题

求教一个sql语句关于min,max函数查询的问题

我写了一句这sql语句:

select stu.name,

min(att.checktime),

max(att.checktime)

from student stu,

attendance att

where

att.stu_obid=stu.id

group by s.id

attendance表里面有一个字段是type,记录的是“进校”,“离校”状态,我该怎么写才能让min函数选择的考勤时间是以“进校”这个状态为条件选择,max函数以“离校”状态为条件选择?谢谢~

分享到:

------解决方案--------------------

最笨的办法:

with ax as(

select stu.name name,

att.checktime checktime,

att.type type

from student stu,

attendance att

where

att.stu_obid=stu.id

group by stu.name,att.checktime,att.type

)

select name,min(checktime),0 from ax where type='进校'

union

select name,0,max(checktime) from ax where type='离校'

------解决方案--------------------

min(att.checktime), max(att.checktime)

改为

min(decode(att.type,'进校',att.checktime,null)),

max(decode(att.type,'离校',att.checktime,null))

------解决方案--------------------

引用:Quote: 引用:min(decode(att.type,'进校',att.checktime,null)),

max(decode(att.type,'离校',att.checktime,null))

我能想 在min中先根据条件“进校”拿到分组,然后再从这个分组获取最小那个,不知道行不行呢?

上面那个不是可以满足么

with attendance as

(

select 1 id,11 stu_obid,'2013-9-1 08:12:13' checktime,'进校' type from dual union all

select 2 id,11 stu_obid,'2013-9-1 12:30:13' checktime,'离校' type from dual union all

select 3 id,11 stu_obid,'2013-9-1 14:30:13' checktime,'进校' type from dual union all

select 4 id,11 stu_obid,'2013-9-1 15:30:31' checktime,'离校' type from dual l

)

select stu_obid,

min(decode(type,'进校',checktime)) m_time,

max(decode(type,'离校',checktime)) n_time

from attendance

group by stu_obid

stu_obid       m_time      n_time

---------------------------

1112013-9-1 08:12:132013-9-1 15:30:31

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值