如何在SQL Server 2005 Compact Edition 3.1中进行此查询?

时间:2010-09-08 08:34:47

标签: sql sql-server-2005

此子查询适用于SQL Server 2005 Compact Edition 4

 SELECT TableA.serialNo  
 FROM   TableA,
        (SELECT MAX(TableB.lotNo) AS lotNo,TableA.codeA,TableA.codeB
         FROM   TableA, TableB
         WHERE  (TableA.codeA =TableB.codeA)
                AND (TableA.codeB = TableB.codeB)
                AND ((LEN(TableA.openDate) > 0) OR TableA.openDate IS NOT NULL)
         GROUP BY 
                TableA.codeA, TableA.codeB) a  
 WHERE  (TableA.lotNo < a.lotNo)  
        AND (TableA.codeA = a.codeA)  
        AND (TableA.codeB = a.codeB) 
        AND ((LEN(TableA.endDate) = 0) OR TableA.endDate IS NULL)

如何在SQL Server 2005 Compact Edition 3.1中创建?

谢谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试使用普通INNER JOINS替换(不太可读的imho)连接语法?

    SELECT  TableA.serialNo   
    FROM    TableA
            INNER JOIN (
              SELECT  MAX(TableB.lotNo) AS lotNo
                      ,TableA.codeA
                      ,TableA.codeB 
              FROM    TableA
                      INNER JOIN TableB ON TableA.codeA = TableB.codeA
                                           AND TableA.codeB = TableB.codeB
              WHERE   LEN(TableA.openDate) > 0
                      OR TableA.openDate IS NOT NULL
              GROUP BY  
                      TableA.codeA, TableA.codeB
            ) a  ON TableA.lotNo < a.lotNo
                    AND TableA.codeA = a.codeA
                    AND TableA.codeB = a.codeB
    WHERE   LEN(TableA.endDate) = 0
            OR TableA.endDate IS NULL

或一些条件的重写

SELECT  a1.serialNo   
FROM    TableA a1
        INNER JOIN (
          SELECT  MAX(b.lotNo) AS lotNo
                  , a.codeA
                  , a.codeB 
          FROM    TableA a
                  INNER JOIN TableB b ON a.codeA = b.codeA
                                         AND a.codeB = b.codeB
          WHERE   a.openDate IS NOT NULL
          GROUP BY  
                  a.codeA, a.codeB
        ) a2  ON a1.lotNo < a2.lotNo
                 AND a1.codeA = a2.codeA
                 AND a1.codeB = a2.codeB
WHERE   LEN(COALESCE(a1.endDate, '')) = 0