当文档= 0

时间:2015-09-25 15:54:08

标签: sql-server-2008

我有一条没有文件的记录(即Documents= 0)。当我执行以下查询时,它返回零行,但它应该返回我的一行,因为我有一个没有文档的记录。

如何修改它以便它将返回没有文档的记录?

SELECT * FROM (SELECT ROW_NUMBER() OVER( Order By ParentID  ) AS RowNumber_ps,

UPPER(HostApplicationLocalData.ParentID) asParentID,

COUNT(Document.ID) as Documents from HostApplicationLocalData

inner join Document onHostApplicationLocalData.ID=Document.HostApplicationLocalData_ID

WHERE HostApplicationLocalData.TransactionType_ID = 1

AND Document.Removed=0

AND HostApplicationLocalData.Company_ID = 9000

AND ( SharePointURI is not null or ((SharePointURI isnull and Content_ID is not null )

or ((HostApplicationLocalData_ID is not null andHostApplicationLocalData_ID != 0 and Content_ID isnull))))

group by HostApplicationLocalData.ParentID )q

where Documents > 0

And

Documents = '0'

1 个答案:

答案 0 :(得分:0)

查看您的代码,INNER JOIN(以及过滤器Document.Removed=0)很可能排除Document表中没有条目的任何记录,这自然意味着没有值显示COUNT(Document.ID = 0.

尝试使用此版本的查询,将JOIN类型转换为LEFT OUTER,并添加条件,允许Document.Removed过滤器通过Document的记录记录将返回NULL

SELECT * 
FROM 
  (
    SELECT 
        ROW_NUMBER() OVER( Order By ParentID  ) AS RowNumber_ps,
        UPPER(HostApplicationLocalData.ParentID) asParentID,
        COUNT(Document.ID) as Documents 
    FROM
        HostApplicationLocalData
         LEFT OUTER JOIN 
        Document ON
            HostApplicationLocalData.ID=Document.HostApplicationLocalData_ID
    WHERE 
            HostApplicationLocalData.TransactionType_ID = 1
        AND (Document.Removed=0 OR Document.HostApplicationLocalData_ID IS NULL)
        AND HostApplicationLocalData.Company_ID = 9000
        AND 
            (SharePointURI is not null or 
              ((SharePointURI isnull and Content_ID is not null ) or 
                ((HostApplicationLocalData_ID is not null andHostApplicationLocalData_ID != 0 and Content_ID isnull))
              )
            )
    GROUP BY HostApplicationLocalData.ParentID 
  )q
WHERE Documents = 0