2012-02-14 116 views
1

我有一個查詢(它的一部分),如下所示,當我的GridView加載了這個查詢時,那些沒有得到IssCountUsedCount值的行將是空白的。如何爲這些記錄的這些列設置默認值0使用0作爲默認計數值

SELECT 
    CASE 
    WHEN @SortByTypeCode = 1 THEN ROW_NUMBER() OVER(ORDER BY vt.Code) 
    WHEN @SortByTypeName = 1 THEN ROW_NUMBER() OVER(ORDER BY vt.Name)    
    WHEN @SortByTypeIssued = 1 THEN ROW_NUMBER() OVER(ORDER BY count(X.IssCount)) 
    WHEN @SortByTypeUsed = 1 THEN ROW_NUMBER() OVER(ORDER BY count(Y.UsedCount)) 
    ELSE ROW_NUMBER() OVER(ORDER BY vt.AutoID) 
    END AS RowNum 
    ,vt.AutoID 
    ,vt.Code 
    ,X.IssCount as Issued 
    ,Y.UsedCount as Used 
INTO #tmp_Results --Dont Change This 
FROM VoucherType vt 
LEFT JOIN (
    SELECT VoucherType_AutoID,COUNT(VoucherNo) IssCount 
    FROM Voucher 
    WHERE VoidedBy IS NULL AND VoidedOn IS NULL 
    GROUP BY VoucherType_AutoID  
) X ON vt.AutoID = X.VoucherType_AutoID 
LEFT JOIN (
    SELECT V.VoucherType_AutoID, COUNT(VUsed.AutoID) UsedCount 
    FROM voucherUsedLog VUsed 
    INNER JOIN Voucher V ON VUsed.Voucher_AutoID = V.AutoID 
    WHERE VUsed.VoidedBy IS NULL AND VUsed.VoidedOn IS NULL 
    GROUP BY V.VoucherType_AutoID 
) Y 
ON vt.AutoID = Y.VoucherType_AutoID 

回答

3

你可以只包一個COALESCE周圍X.IssCountY.UsedCount,使他們0,像這樣:

,COALESCE(X.IssCount,0) as Issued 
,COALESCE(Y.UsedCount,0) as Used 
3

嘗試這種方式..

ISNULL(X.IssCount,0) as Issued 
,ISNULL(Y.UsedCount,0) as Used