select py_date as time,py_typ,py_docn,py_padtim,py_mm,py_amt from pyfile2003 where py_date in (select max(py_date) from pyfile2003 where py_com=060401 and py_typ=2 and py_mm<02 and py_status!=3 )
group by py_typ

解决方案 »

  1.   

    select py_date as time,py_typ,py_docn,py_padtim,py_mm,py_amt 
    from pyfile2003 
    where py_date in 
    (select max(py_date) from pyfile2003 where py_com=060401 and py_typ=2 and py_mm<02 and py_status!=3 ) 
    and py_typ=2 and py_mm<02 and py_status!=3
      

  2.   

    既然语句中有了对 py_typ 的限制,那么GROUP BY py_typ 就只能得到唯一的而且是靠前的记录
      

  3.   

    huxin1012(fox) 的方法是正确的,是否能告诉我我得语句为什么错的。
    我得思路老是比较死板,你们都是高手,能否传授一下经验
      

  4.   

    还有一个问题,我得in后面的值应该是有''的,可是这个sql又不能加''
      

  5.   

    你的语句取出的是最大的py_date,以及其他的字段值,而不是py_date取最大值时的其他的字段值。我得in后面的值应该是有''的,可是这个sql又不能加''
    ----------
    没看明白。
      

  6.   

    当输出列包含有除group by列以及聚合函数值之外的输出值时,必须使用相关子查询(内部查询和外部查询通过条件子句互相制约).in后面并不一定得加'',即使是string,datetime类型,只要是从子查询返回集合,可不必考虑''
      

  7.   

    我是认为如果我取得是最大的py_date,那么其他的字段应该也是这条记录的啊???
    就是我得里面的sql语句取道的是一个日期值,如果已得到这个值,那么就应该写成这样:
    select py_date as time,py_typ,py_docn,py_padtim,py_mm,py_amt 
    from pyfile2003 
    where py_date in 
    ('2003-01-10 00:00:00' ) 
    and py_typ=2 and py_mm<02 and py_status!=3,不加引号这个语句是有错误的。
      

  8.   

    我试了,如果是这样
    SELECT py_date, py_typ, py_docn, py_padtim, py_amt
    FROM pyfile2003
    where py_date
    in (
    '2003-01-14 13:39:49'
    )
    and py_com = 060401
    and py_typ = 2
    and py_mm < 02
    and py_status != 3
    就能取道值。
    如果是不加''就有错误,因为这个日期中间包含空格吗?
      

  9.   

    不加引号的话就不是日期型的值了。
    但用(select max(py_date) from 得到的本身就是日期型的,所以是不会出错的。
      

  10.   

    我用的是mysql,是不是跟这个有关系呀。
      

  11.   

    in () 里面直接用sql语句应该不会错的。
    但你把结果填进去的话,就得加''了。
      

  12.   

    我现在的sql语句是这样
    SELECT py_date, py_typ, py_docn, py_padtim, py_amt
    FROM pyfile2003
    where py_date
    in (
    select max( py_date ) 
    FROM pyfile2003
    where py_com = 060401
    and py_typ = 2
    and py_mm < 02
    and py_status != 3
    )
    and py_com = 060401
    and py_typ = 2
    and py_mm < 02
    and py_status != 3 这时mysql给我返回一个错误信息:
    MySQL 返回:
    You have an error in your SQL syntax near 'select max(py_date) FROM pyfile2003 where py_com=060401 and py_typ=2 and py_mm<0' at line 1
      

  13.   

    试试
    SELECT py_date, py_typ, py_docn, py_padtim, py_amt
    FROM pyfile2003
    where py_date
    in ( select '' + max( py_date ) + ''
    FROM pyfile2003
    where py_com = 060401
    and py_typ = 2
    and py_mm < 02
    and py_status != 3 
    )
    and py_com = 060401
    and py_typ = 2
    and py_mm < 02
    and py_status != 3
      

  14.   

    错误
    MySQL 返回:
    You have an error in your SQL syntax near 'SELECT '' + max( py_date ) + '' FROM pyfile2003 where py_com = 060401 and py_typ' at line 1
      

  15.   

    在SQL Server中是可以的。在MySQL中不知是什么原因。机器上也没有MySQL。
    要不,你去MySQL版问问看吧。
      

  16.   

    MySQL 目前不支持 子查询
      

  17.   

    mysql的帮助中存在一个例子:
    SELECT article, dealer, price
    FROM   shop s1
    WHERE  price=(SELECT MAX(s2.price)
                  FROM shop s2
                  WHERE s1.article = s2.article)
      

  18.   

    那就试时
    SELECT py_date, py_typ, py_docn, py_padtim, py_amt
    FROM pyfile2003
    where py_date
    =( select  max( py_date ) 
    FROM pyfile2003
    where py_com = 060401
    and py_typ = 2
    and py_mm < 02
    and py_status != 3 
    )
    and py_com = 060401
    and py_typ = 2
    and py_mm < 02
    and py_status != 3
      

  19.   

    select py_date as time,py_typ,py_docn,py_padtim,py_mm,py_amt 
    from pyfile2003 
    where py_date in (select max(py_date) 
                        from pyfile2003 
                       where py_com=060401 
                             and py_typ=2 
                             and py_mm<02 
                             and py_status!=3 )
          and py_typ=2 and py_mm<02 and py_status!=3
      

  20.   

    '060401'? '02'? 对吗?select py_date as [time],py_typ,py_docn,py_padtim,py_mm,py_amt 
    from pyfile2003 
    where py_date in (select max(py_date) 
                        from pyfile2003 
                       where py_com='060401' 
                             and py_typ=2 
                             and py_mm<02
                             and py_status!=3 )
         and py_com = '060401'      
         and py_typ=2 
         and py_mm<02 
         and py_status!=3
      

  21.   

    time 是关键字,要[],最好别用!