本人用delphi6自带的数据库工具做了一个表,字段分别为:
班级 总分
现在我要把表里的所有班级相同的,分组求出平均总分,于是用了下列语句:
select 班级,avg(总分) from mm.db group by 班级
可是我还想把当天的日期,和按照平均总分排一下后的名次,也加上去,请问该怎么办?
先谢谢了

解决方案 »

  1.   

    select 班级,avg(总分) as 总分合计, SysDate as 当前日期 from mm.db
      group by 班级
      order by 总分合计;SysDate是oracle的日期函数,其他数据库系统的相应函数名称可能不同。
      

  2.   

    sql 的就直接是 date 了
      

  3.   

    另外,这个日期我不想用系统的,而是用自已设定的日期,比如一个从DateTimePicker1.date得到的日期,该怎么办
      

  4.   

    还有,刚才我试了一下,sysdate或者date在delphi自带的数据库里没法用
      

  5.   

    --------建库脚本-----------
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[Test]
    GOCREATE TABLE [dbo].[Test] (
    [ID] [int] IDENTITY (1, 1) NOT NULL ,
    [Class] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [SumNumeric] [numeric](18, 0) NULL 
    ) ON [PRIMARY]
    GOALTER TABLE [dbo].[Test] WITH NOCHECK ADD 
    CONSTRAINT [PK_Test] PRIMARY KEY  CLUSTERED 
    (
    [ID]
    )  ON [PRIMARY] 
    GO
    ---------------功能实现-----------------------------
    if exists(select * from tempdb..sysobjects where name = '##temp')
    drop table ##temp
    go
    select a.* into ##temp from 
    (select Class,Sum(SumNumeric) as SumNumeric,Getdate() as NowTime from test 
    group by class) as a
    select * from  ##temp order by SumNumeric asc
      

  6.   

    楼上的你好,这么多语句在一个小小的query1.sql.add('')里怎么实现呀!用存储过程吗?没用过,能不能给具体说一下
      

  7.   

    select * from testif exists(select * from tempdb..sysobjects where name = '##temp')
    drop table ##temp
    go
    select a.* into ##temp from 
    (select Class,Sum(SumNumeric) as SumNumeric,Getdate() as NowTime from test 
    group by class) as a
    select * from  ##temp order by SumNumeric asc
    select 名次=(select count(*)+1 from ##temp where SumNumeric>a.SumNumeric),Class,SumNumeric,
    Getdate() as NowTime from ##temp as a order by  名次
      

  8.   

    hbqc_zh() ,谢谢你了,我虽然没用过存储过程,不过从你的语句里,我琢磨着,估计不用存储过程和临时表,也能实现,正在想
      

  9.   

    唉,我刚才用getDate()了,可是还是弹出一个Capability not supported
      

  10.   

    select a.* into ##temp from 
    (select Class,Sum(SumNumeric) as SumNumeric,Getdate() as NowTime from test 
    group by class) as a
    弹出这样一个错误Invalid use of keyword.
                    Token:select
                    Line Number:1
      

  11.   

    1、先建个表结构:Test
             ID       Class    SumNumeric
    1 A 2000
    2 A 1000
    3 B 1200
    4 B 1500
    5 C 1000
    6 C 2000
    7 D 1500
    2、在查询分析器中执行以下SQL语句,不会出错的,
    if exists(select * from tempdb..sysobjects where name = '##temp')
    drop table ##temp
    go
    select a.* into ##temp from 
    (select Class,Sum(SumNumeric) as SumNumeric,Getdate() as NowTime from test 
    group by class) as a
    select 名次=(select count(*)+1 from ##temp where SumNumeric>a.SumNumeric),Class,SumNumeric,
    Getdate() as NowTime from ##temp as a order by  名次3、方法是很多的,关键是效率和功能
      

  12.   

    执行结果:1 A 3000 2006-09-26 16:09:44.297
    1 C 3000 2006-09-26 16:09:44.297
    3 B 2700 2006-09-26 16:09:44.297
    4 D 1500 2006-09-26 16:09:44.297
      

  13.   

    我不知道你用的是什么数据库,我用的是delphi自带的paradox7,像这样的语句:
    select a.* from (select * from test) as a
    是根本没法执行的