如何获取每个父项的子类别的项目数?

时间:2009-10-27 21:49:17

标签: tsql count categories

这与以下帖子有关:

The function that retrieves the item counts for each folder (category) is:

ALTER FUNCTION [dbo].[GetFolderReceiptCount]
(
    -- Add the parameters for the function here
    @FolderID bigint
)
RETURNS int
AS
BEGIN

    DECLARE @Return int

    SET @Return = 0

    SELECT 
        --H.ReceiptFolderID, 
        @Return = COUNT(H.ReceiptFolderID)
    FROM
        tbl_ReceiptFolderLnk H
        JOIN tbl_Receipt D ON H.ReceiptID = D.ReceiptID
    WHERE ReceiptFolderID=@FolderID
    GROUP BY
        H.ReceiptFolderID

    -- Return the result of the function
    RETURN @Return      

END

如何更改这些以返回每个父母的计数?

1 个答案:

答案 0 :(得分:1)

您应该更改函数以返回表变量,或者使用存储过程以便获取数据集

SQL语句应与此类似:

SELECT 
    H.ReceiptFolderID, COUNT(H.ReceiptFolderID)
FROM
    tbl_ReceiptFolderLnk H
    JOIN tbl_Receipt D ON H.ReceiptID = D.ReceiptID
WHERE ReceiptFolderID=@FolderID
GROUP BY
    H.ReceiptFolderID