SELECT MAX(单据号) , MAX(分录号) FROM table
GROUP BY 商品型号

解决方案 »

  1.   

    to  daoke,上面的例子再看一下,你的方法无法实现了
      

  2.   

    SELECT MAX(单据号) , MAX(分录号) FROM table
    where  商品型号=??
    GROUP BY 商品型号
      

  3.   

    to  litsnake1,按照你的sql取出的是2(单据号最大),4(分录号中最大);而不是2(单据号最大),3(在最大单据号的记录中查找最大的分录号)
      

  4.   

    SELECT MAX(单据号) , MAX(分录号) FROM table
    where  单据号=2
    GROUP BY 单据号
      

  5.   

    to  litsnake1,你说的对!不过你说的是最简单的一种情况!
    现在这张表中有许多记录,可能有10000张单据,其中有1500种型号。
    我的目的是要取出每个型号的最大单据号,和最大单据号中的最大分录号
    所在的这条记录的所有字段!
      

  6.   

    select top 1 * from yourtable order by format(单据号,"0000") &  format(分录号,"0000") desc
      

  7.   

    改进:
    select top 1 * from yourtable order by 单据号 desc,分录号 desc
      

  8.   

    加入 Group by 按型号分组试试看。
      

  9.   

    select * from tblType,(SELECT chrType, MAX(intP) AS intPP
    FROM (SELECT tblType.chrType, tblType.intp, tblType.intd
            FROM tblType INNER JOIN
                      (SELECT chrType, MAX(intd) AS intdd
                     FROM tbltype
                     GROUP BY chrtype) maxintd ON tblType.chrType = maxintd.chrType AND 
                  tblType.intD = maxintd.intdd) tblAll
    GROUP BY chrType) as maxintP where tblType.chrType=maxintP.chrType and tblType.intP=maxintP.intPPtblType就是你的表名称
    chrtype就是 商品型号intD 就是 单据号
    intP 就是 分录号
    maxintd 是查询出的临时表,用于取出最大的单据号 intDD字段
    maxintP 是查询出的临时表,用于取出最大的分录号 intPP字段
      

  10.   

    我在sqlserver 2000下测试通过
      

  11.   

    SELECT * FROM table where 单据号 in (select max(单据号) from table) and 分录号 in (select max(分录号) from table where 单据号 in (select max(单据号) from table))
      

  12.   

    Select * From table Where 单据号=(Select Max(单据号) From table) and 分录号=(Select Max(分录号) From table) 
      

  13.   

    Select Max(分录号) From table
    这是不行的
    如果是以下数据就不行了商品型号,单据号,分录号,金额
    例如:          桌子      1          1   100
                    桌子      1          2   200
                    桌子      2          1   300
                    桌子      2          2   400
                    桌子      2          5   500                椅子      1          1   100
                    椅子      1          2   200
                    椅子      2          1   300
                    椅子      2          2   400
                    椅子      2          3   500
    那么椅子的分录号就没有了,查不出来了
    这个时候查出来的就是桌子 的分录号 5 了!!