2010-09-08 129 views
0

此子查詢的作品2005精簡版4如何在SQL Server 2005 Compact Edition 3.1中進行此查詢?在SQL Server

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精簡版3.1進行?

謝謝!

+0

一些重寫你知道,它並沒有在3.1的工作?查詢在3.1中給出了具體的錯誤嗎? – 2010-09-08 08:43:02

回答

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 
+0

感謝您的回答,我測試了這兩個答案,但SQL Server 2005 Compact Edition 3.1不支持這樣的子查詢:(... – JulyPeace 2010-09-23 08:54:09

相關問題