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.”
我还用了其它的方法比如嵌套,用存储过程,都不成功。
请高手帮帮忙?

解决方案 »

  1.   

    用min,相同項作為group by 
      

  2.   

    這列regionid顯示是最小記錄,不是取最小值
      

  3.   

    --取大小值
    /******************************************************************************************************************************************************
    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 行受影响)
    */
      

  4.   


    CREATE VIEW informix.v_ming0402_id (regionid,regionname,
    day_code_date,starttime,g0402) mysql不懂。
      

  5.   

    非常感谢ROY_88这么详细的指教。
    也许是我问的不清楚
    我是要取g0402的最小值
    并且要这个最小值对应的regionid 和day_code_date
    也就是说我要返回g0402最小值的那一条记录
    这个我在ORACLE里实现了
    但是在Informix里实现不了
    请问我如果写存储过程怎么写啊?
      

  6.   

    mysql?
    select a.* from tt a
    inner join
    (select min(g0402) as ma from tt) b
    on a.g0402=b.ma
      

  7.   

    select * from a where g0402=(select min(g0402) from a)
      

  8.   

    去掉into temp x1就可以了。
      

  9.   

    CREATE VIEW informix.v_ming0402_id (regionid,regionname, 
    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;
      

  10.   

    select * from a where g0402=(select min(g0402) from a)
    Ok!