2015-01-14 63 views
0

我有一個DB2查詢,如下所示:DISTINCT SQL查詢連接省略了從考慮一列,

SELECT DISTINCT RETAILMASTERFILE.DOIDCD AS "RETAILMASTERFILE_DOIDCD", 
     RETAILMASTERFILE.COCOMO AS "RETAILMASTERFILE_COCOMO", 
     #XENOS.CUSTREF AS "XENOS_CUSTREF", 
     #XENOS.ADDUDT AS "XENOS_ADDUDT", 
     #XENOS.ADUPDD AS "XENOS_ADUPDD", 
     #XENOS.ADUPDT AS "XENOS_ADUPDT", 
     #XENOS.ADSTAT AS "XENOS_ADSTAT" 
FROM  RETAILMASTERFILE INNER JOIN 
     #XENOS ON RETAILMASTERFILE.DOCOMP = #XENOS.ADCOMP 
       AND RETAILMASTERFILE.COCOMO = #XENOS.ADDELN 
WHERE  (RETAILMASTERFILE.DOIDCD = 'CUST008') 
     AND (RETAILMASTERFILE.COCOMO = '345126032') 
     AND (RETAILMASTERFILE.DOCOMP = 'LONDON') 

問題是#XENOS.ADUPDT可能不是唯一的,給了我不希望重複的記錄。 有什麼辦法可以排除這個考慮?迄今爲止,我在有限的知識和對團隊的粗糙理解中迄今爲止所嘗試的所有內容迄今都破壞了我的查詢。

+0

如果有不同#XENOS.ADUPDT,如何選擇哪一個選擇? – jarlh

+0

@jarlh我不太感到困擾,#XENOS.ADUPDT只是時間戳,任何值都非常接近所需的時間。 –

回答

1

使用GROUP BY代替:

SELECT RETAILMASTERFILE.DOIDCD AS "RETAILMASTERFILE_DOIDCD", 
     RETAILMASTERFILE.COCOMO AS "RETAILMASTERFILE_COCOMO", 
     #XENOS.CUSTREF AS "XENOS_CUSTREF", 
     #XENOS.ADDUDT AS "XENOS_ADDUDT", 
     #XENOS.ADUPDD AS "XENOS_ADUPDD", 
     MAX(#XENOS.ADUPDT) AS "XENOS_ADUPDT", 
     #XENOS.ADSTAT AS "XENOS_ADSTAT" 
FROM  RETAILMASTERFILE INNER JOIN 
     #XENOS 
     ON RETAILMASTERFILE.DOCOMP = #XENOS.ADCOMP AND 
      RETAILMASTERFILE.COCOMO = #XENOS.ADDELN 
WHERE (RETAILMASTERFILE.DOIDCD = 'CUST008') AND (RETAILMASTERFILE.COCOMO = '345126032') AND 
     (RETAILMASTERFILE.DOCOMP = 'LONDON') 
GROUP BY RETAILMASTERFILE.DOIDCD, 
     RETAILMASTERFILE.COCOMO, 
     #XENOS.CUSTREF, 
     #XENOS.ADDUDT, 
     #XENOS.ADUPDD, 
     #XENOS.ADSTAT; 
+0

謝謝,我不確定在哪裏放組。我在WHERE聲明之前已經有了它。 –