2015-09-28 114 views
0

以下查詢不會按升序爲0返回文檔結果。它直接從1個文件開始以升序排列,儘管有3個行有0個文件。文檔的順序不會返回正確的順序

SELECT UPPER(HostApplicationLocalData.ParentID)as ParentID, COUNT(Document.ID) as Documentsfrom  HostApplicationLocalData inner join Documenton HostApplicationLocalData.ID=Document.HostApplicationLocalData_ID WHEREHostApplicationLocalData.TransactionType_ID = 6AND Document.Removed=0 ANDHostApplicationLocalData.Company_ID = 9000 AND (SharePointURI is not null or ((SharePointURI is nulland Content_ID is not null ) or((HostApplicationLocalData_ID is not null andHostApplicationLocalData_ID != 0 and Content_ID isnull)))) AND HostApplicationLocalData.ParentIDIN('ACETRAVE0001   ','ADVANCED0001   ','ALLENSON0001   ','AMERICAN0001   ','ASSOCIAT0001   ','ATTRACTI00001  ','AUSTRALIA      ','AUTOFINA0001   ','BEAUMONT0001   ','BERGERON0001   ','BLOOMING0001   ','BURNETTT0001   ','BUSINESS MAG   ','BUSINESS0001   ','CAPITALP0001   ','CARLSONS0001   ','CENTRALC0001   ','CHICAGOC0001   ','CHICAGOR0001   ','CIRCUITD0001   ','COMNETEN0001   ','COMPUTER0001   ','COMVEXIN0001   ','CONSUMER MAG   ','CONTINEN0001   ','COWLEYEN0008   ','CRUGEREN0001   ','DISTANTI0001   ','DOLECKIC0001   ','ELECTRO0001    ','ELECTRON0001   ','ELECTRON0003   ','FABRIKAM0001   ','GARDNERS0001   ','GKCLEANI0001   ','GREENLAK0001   ','GREENWOO0001   ','GUNTHARS0004   ','HARVEYEL0001   ','HILLSBRO0001   ','ILSTATE0001    ','IMAGEMAK0001   ','INLANDRE0001   ','INLINESE0001   ','INNERCIT0001   ','INSTATET0001   ','INTEGRAT0001   ','INTERNAL0001   ','INTERNAT0001   ','INTERNAT0002   ','') group byHostApplicationLocalData.ParentID order by Documents asc 

回答

0

您正在使用內連接,而不是左連接。然後您將只獲得至少有一個文檔的文檔。爲了全部使用左加入像這樣:

SELECT UPPER(HostApplicationLocalData.ParentID) AS ParentID, 
     COUNT(Document.ID) AS Documents 
FROM HostApplicationLocalData 
LEFT JOIN Document ON HostApplicationLocalData.ID = Document.HostApplicationLocalData_ID 
WHERE HostApplicationLocalData.TransactionType_ID = 6 AND 
     Document.Removed = 0 AND 
     HostApplicationLocalData.Company_ID = 9000 AND 
     (
      SharePointURI IS NOT NULL OR 
      (
      (
       SharePointURI IS NULL AND 
       Content_ID IS NOT NULL 
      ) OR 
      (
       (HostApplicationLocalData_ID IS NOT NULL AND 
       HostApplicationLocalData_ID != 0 AND 
       Content_ID IS NULL) 
      ) 
     ) 
     ) AND 
     HostApplicationLocalData.ParentID IN ('ACETRAVE0001 ', 
               'ADVANCED0001 ', 
               'ALLENSON0001 ', 
               'AMERICAN0001 ', 
               'ASSOCIAT0001 ', 
               'ATTRACTI00001 ', 
               'AUSTRALIA  ', 
               'AUTOFINA0001 ', 
               'BEAUMONT0001 ', 
               'BERGERON0001 ', 
               'BLOOMING0001 ', 
               'BURNETTT0001 ', 
               'BUSINESS MAG ', 
               'BUSINESS0001 ', 
               'CAPITALP0001 ', 
               'CARLSONS0001 ', 
               'CENTRALC0001 ', 
               'CHICAGOC0001 ', 
               'CHICAGOR0001 ', 
               'CIRCUITD0001 ', 
               'COMNETEN0001 ', 
               'COMPUTER0001 ', 
               'COMVEXIN0001 ', 
               'CONSUMER MAG ', 
               'CONTINEN0001 ', 
               'COWLEYEN0008 ', 
               'CRUGEREN0001 ', 
               'DISTANTI0001 ', 
               'DOLECKIC0001 ', 
               'ELECTRO0001 ', 
               'ELECTRON0001 ', 
               'ELECTRON0003 ', 
               'FABRIKAM0001 ', 
               'GARDNERS0001 ', 
               'GKCLEANI0001 ', 
               'GREENLAK0001 ', 
               'GREENWOO0001 ', 
               'GUNTHARS0004 ', 
               'HARVEYEL0001 ', 
               'HILLSBRO0001 ', 
               'ILSTATE0001 ', 
               'IMAGEMAK0001 ', 
               'INLANDRE0001 ', 
               'INLINESE0001 ', 
               'INNERCIT0001 ', 
               'INSTATET0001 ', 
               'INTEGRAT0001 ', 
               'INTERNAL0001 ', 
               'INTERNAT0001 ', 
               'INTERNAT0002 ', '') 
GROUP BY HostApplicationLocalData.ParentID 
ORDER BY Documents ASC;