MYSQL新手上路呵呵,
我想在Informix数据库中创建一个视图,a表中有两条记录
regionid regionname day_code_date starttime g0402
----------- ------------- ---------------- ------------------- ---------------------------------
10 海口地区 2008-6-17 2008-6-17 下午9:00:00 95.144140462660826017664790120439
11 海甸地区 2008-6-17 2008-6-17 下午9:00:00 95.41592657700821675497077559356 我要从中选取min(g0402),结果是
regionid regionname day_code_date starttime g0402
----------- ------------- ---------------- ------------------- ---------------------------------
10 海口地区 2008-6-17 2008-6-17 下午9:00:00 95.144140462660826017664790120439 我建了视图
CREATE VIEW informix.v_ming0402_id (regionid,regionname,
day_code_date,starttime,g0402)
as
select regionid ,regionname,day_code_date,min(g0402) as kpi from v_rpt_4_region_busy
group by regionid,regionname,day_code_date into temp x1;提示我“ A syntax error has occurred.”
我还用了其它的方法比如嵌套,用存储过程,都不成功。
请高手帮帮忙?
我想在Informix数据库中创建一个视图,a表中有两条记录
regionid regionname day_code_date starttime g0402
----------- ------------- ---------------- ------------------- ---------------------------------
10 海口地区 2008-6-17 2008-6-17 下午9:00:00 95.144140462660826017664790120439
11 海甸地区 2008-6-17 2008-6-17 下午9:00:00 95.41592657700821675497077559356 我要从中选取min(g0402),结果是
regionid regionname day_code_date starttime g0402
----------- ------------- ---------------- ------------------- ---------------------------------
10 海口地区 2008-6-17 2008-6-17 下午9:00:00 95.144140462660826017664790120439 我建了视图
CREATE VIEW informix.v_ming0402_id (regionid,regionname,
day_code_date,starttime,g0402)
as
select regionid ,regionname,day_code_date,min(g0402) as kpi from v_rpt_4_region_busy
group by regionid,regionname,day_code_date into temp x1;提示我“ A syntax error has occurred.”
我还用了其它的方法比如嵌套,用存储过程,都不成功。
请高手帮帮忙?
解决方案 »
- 关于文件系统和数据库系统存储的问题。
- phpmyadmin 数据库表中字段 插入中文出现乱码的问题?
- 请教MySQL的API函数 mysql_real_query 执行错误问题
- 请教:自动运行存储过程
- 如何使用EMS SQL manager 2007 for mysql 查看数据库的操作日志?
- 请问,VB中如何向MYSQL数据库中插入图片呀..(BLOB数据类型),最好有代码,请高手看过来
- 一个简单的sql语句
- 如何用自身关联查询检索出表内重复的记录
- MYSQL NOT IN 查询不出数据
- MySql 数据量大时索引无效!
- 我用的是mysql5.0的数据库提交中文就报错“Incorrect string value”各位帮忙看看
- 引用了一个“MySQLDriverCS.dll”,但是使用 MySQLTransaction 事务处理老是不成功,
/******************************************************************************************************************************************************
Name相同ID最小或最大的记录整理人:中国风(Roy)日期:2008.06.06
******************************************************************************************************************************************************/--> --> (Roy)生成測試數據
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go
--1、Name相同ID最小的记录(推荐用1,2,3),方法3在SQl05时,效率高于1、2
方法1:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID<a.ID)方法2:
select a.* from #T a join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID方法3:
select * from #T a where ID=(select min(ID) from #T where Name=a.Name)方法4:
select a.* from #T a join #T b on a.Name=b.Name and a.ID>=b.ID group by a.ID,a.Name,a.Memo having count(1)=1 方法5:
select * from #T a group by ID,Name,Memo having ID=(select min(ID)from #T where Name=a.Name)方法6:
select * from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)=0方法7:
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID)方法8:
select * from #T a where ID!>all(select ID from #T where Name=a.Name)方法9:
select * from #T a where ID in(select min(ID) from #T group by Name)--SQL2005:方法10:
select ID,Name,Memo from (select *,min(ID)over(partition by Name) as MinID from #T a)T where ID=MinID
生成结果:
/*
ID Name Memo
----------- ---- ----
1 A A1
4 B B1(2 行受影响)
*/
--2、Name相同ID最大的记录,与min相反:
方法1:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID>a.ID)方法2:
select a.* from #T a join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID order by ID方法3:
select * from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID方法4:
select a.* from #T a join #T b on a.Name=b.Name and a.ID<=b.ID group by a.ID,a.Name,a.Memo having count(1)=1 方法5:
select * from #T a group by ID,Name,Memo having ID=(select max(ID)from #T where Name=a.Name)方法6:
select * from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)=0方法7:
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID desc)方法8:
select * from #T a where ID!<all(select ID from #T where Name=a.Name)方法9:
select * from #T a where ID in(select max(ID) from #T group by Name)--SQL2005:方法10:
select ID,Name,Memo from (select *,max(ID)over(partition by Name) as MinID from #T a)T where ID=MinID生成结果2:
/*
ID Name Memo
----------- ---- ----
3 A A3
5 B B2(2 行受影响)
*/
CREATE VIEW informix.v_ming0402_id (regionid,regionname,
day_code_date,starttime,g0402) mysql不懂。
也许是我问的不清楚
我是要取g0402的最小值
并且要这个最小值对应的regionid 和day_code_date
也就是说我要返回g0402最小值的那一条记录
这个我在ORACLE里实现了
但是在Informix里实现不了
请问我如果写存储过程怎么写啊?
select a.* from tt a
inner join
(select min(g0402) as ma from tt) b
on a.g0402=b.ma
day_code_date,starttime,g0402)
as
select regionid ,regionname,day_code_date,g0402 as kpi from v_rpt_4_region_busy where g0402=(select min(g0402) from v_rpt_4_region_busy) into temp x1;
Ok!