SELECT *
FROM CProduct T
WHERE (CustomID = 398)  AND (CTID =
          (SELECT MAX(CTID)
         FROM CProduct
         WHERE productno = T.productno))
ORDER BY ProductNOCTID是自动编号ProductNO是产品编号取出不重复记录的值,以上语句没有取出来,如果CustomID=471就可以取出,不知是哪里问题

解决方案 »

  1.   

    CTID 是不是唯一的,如果不是,那么提取不出来的。
      

  2.   

    SELECT *
    FROM CProduct T
    WHERE (CustomID = 398) AND (CTID =
      (SELECT MAX(CTID)
      FROM CProduct
      WHERE productno = T.productno and CustomID = 398))
    ORDER BY ProductNO
      

  3.   


    --lz的没看出来问题,下面的试试先
    SELECT * FROM CProduct T
    WHERE CustomID = 398 AND 
    not exists(select 1 from CProduct where productno = T.productno and CTID>T.CTID)
    ORDER BY ProductNO
      

  4.   

    你确定你的本意是productno相同则CTID取最大的吗?且CTID唯一?
      

  5.   

    --try:
    SELECT * FROM CProduct T
    WHERE CustomID = 398 AND not exists(
    select 1 from CProduct where productno = T.productno and CTID>T.CTID)
      

  6.   

    SELECT
     * 
    FROM
     CProduct T
    WHERE
     CustomID = 398 
    AND
     not exists(select 1 from CProduct where productno = T.productno and CTID>T.CTID)
      

  7.   

    如果同一Product用多個Customer時會出現樓主的情況
    加上AND CustomID=t.CustomID
    SELECT *
    FROM CProduct T
    WHERE (CustomID = 398) AND (CTID =
      (SELECT MAX(CTID)
      FROM CProduct
      WHERE productno = T.productno AND CustomID=t.CustomID))
    ORDER BY ProductNO
      

  8.   

    SELECT *
    FROM CProduct T
    WHERE CustomID = 398
    having CTID = MAX(CTID)为什么要用子查询呢?说取不出来是因为MAX的原因吧。
    MAX是在where后面执行。
    也就是  SQL会先找出CustomID = 398 的所有数据。然后在取这些数据中的 MAX(CTID)                                                           半年没用SQL的人