求一sql要求如下:表:tables
字段:id 主键
typeid 类别id
cdate 生产时间
ddate 出场时间
re 备注
按typeid分组,取每组中cdate最大的那条记录,如果cdate最大的值有两条,则在这两条中取ddate最大的那条记录。
不要用over分析函数,因为数据库是mysql。
多谢!
字段:id 主键
typeid 类别id
cdate 生产时间
ddate 出场时间
re 备注
按typeid分组,取每组中cdate最大的那条记录,如果cdate最大的值有两条,则在这两条中取ddate最大的那条记录。
不要用over分析函数,因为数据库是mysql。
多谢!
解决方案 »
- 导入表department表PL/SQL Developer中汉字正常,BCB开发软件查询乱码 在线等。。
- 数据库DBlink链接问题
- ora-12535:TNS:操作超时 的问题
- 重新求助一个oracle存储过程题目,谢谢!!!
- 请教个小问题:oracle的内置函数中,有没有判断某个字段值中是不是存在空格的?
- 【走过路过不要错过,复杂报表看我怎么做的哈,看看我写的存储过程~】
- oracle 数据库中,我要把所有的用户的表和日志全部备份出来,该怎么写?
- 请问一个返回表中值得问题!!!
- 在线等待,解决给分
- 请教!关于CLOB???
- 数据库左联接问题,为何WHERE条件中不能添加右边表的查询条件?
- 一个pl/sql问题
你的我只会用分析函数纯sql真不会。
你先按typeid分组,取每组typeid,cdate最大的记录的集合,然后从原表中用IN自己取出所有的记录。
if existe(select count(*)
from tables
group by typeid,cdate
having count(cdate)>=2)
then
select b.*
from
(
select typeid,cdate,max(ddate) as ddate
from tables
group by table,cdate
) a join tables b on a.typeid=b.typeid and a.cdate=b.cdate and a.ddate=b.ddate
else
select b.*
from
(
select typeid,max(cdate) as cdate
from tables
group by typeid) a join tables b on a.typeid=b.typeid and a.cdate=b.cdate方法2
select *
into #aa
from tables
where cdate in
(
select max(cdate) from tables group by typeid
)select * from #aa where ddate in (
select max(ddate)
from #aa
group by typeid
)
(SELECT id FROM tables WHERE typeid=a.typeid ORDER BY cdate desc,ddate desc limit 1,1)
ORDER BY a.id asc
FROM emp a
WHERE a.empno IN ( SELECT empno
FROM emp
WHERE deptno = a.deptno
order by sal desc, ename desc )
这条sql在oracle中为什么不能执行,说缺失右括号,但是去掉order by字句就好了。
from
(
select typeid,cdate,max(ddate) ddate
from tables
group by typeid, cdate
) a, tables b
where a.typeid = b.typeid and a.cdate=b.cdate and a.ddate = b.ddate
into #aa
from tables a join (select typeid,max(cdate) as cdate from tables group by typeid) b on a.typeid=b.typeid and a.cdate=b.cdate
select *
from #aa a join
(select typeid,cdate,max(ddate) from #aa group by typeid,cdate ) b on a.typeid=b.typeid and a.cdate=b.cdate
另外,我曾经就有过这样痛苦的经历,我想我可以提醒你一下:
mysql 没有check (虽然有,但是是假的)
mysql 没有序列
mysql 以前没有事务,现在总算有了
mysql 中没有connectby ,如果你项目中有用到类似bom 的,嘿嘿 那就开心了
当然 mysql免费 这就是为什么无数人用它的原因 oracle每cpu 30w美金吧。。不是随便用得起的
from alan_test a,
(select c.typeid, c.cdate, max(c.ddate) ddate
from alan_test c,
(select typeid, max(cdate) cdate from alan_test group by typeid) b
where c.typeid = b.typeid
and c.cdate = b.cdate
group by c.typeid, c.cdate) d
where a.typeid = d.typeid
and a.cdate = d.cdate
and a.ddate = d.ddate
order by a.typeid ;