文件的顺序不会返回正确的顺序

时间:2015-09-28 13:55:27

标签: sql-server-2008

以下查询不会返回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

1 个答案:

答案 0 :(得分:0)

您正在使用内部联接,而不是左联接。然后,您将只获得至少包含1个文档的文档。要像所有这样使用LEFT JOIN:

 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;