计数除状态之外的数据总数

时间:2014-04-28 02:14:26

标签: sql count

美好的一天伙计们。如果事务的状态是PENDING,则不应该考虑计算我怎么能这样做,下面的sql语法是计算所有数据。

ELSE IF @TableName = 'Retail.MiscReceipt'
        BEGIN
            SET @WholeSelect = --@WholeSelect + @Operator + 
                    'SELECT '''+@TransType+''' AS TransType, UserName AS AddedBy, COUNT(*) AS Count ' +
                    'FROM '+@TableName+' AS TRN ' +
                    'LEFT JOIN IT.Users ON TRN.AddedBy = UserID ' +
                    'WHERE ISNULL(TRN.PostedBy,'''') = '''' AND LiquidationDate BETWEEN ''' + CONVERT(varchar(10),@DateFr) + ''' AND ''' + CONVERT(varchar(10),@DateTo) + ''''
        END

1 个答案:

答案 0 :(得分:1)

我想你想改变这一行:

 'WHERE ISNULL(TRN.PostedBy,'''') = '''' AND LiquidationDate BETWEEN ''' + CONVERT(varchar(10),@DateFr) + ''' AND ''' + CONVERT(varchar(10),@DateTo) + ''''

为:

 'WHERE trn.status <> ''Pending'' AND ISNULL(TRN.PostedBy,'''') = '''' AND LiquidationDate BETWEEN ''' + CONVERT(varchar(10),@DateFr) + ''' AND ''' + CONVERT(varchar(10),@DateTo) + ''''