How to get the item count for sub-categories on each parent?

十年热恋 提交于 2019-12-11 16:41:01

问题


This is related to the following post:

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

How can this be altered to return counts for each parent?


回答1:


You should change the function to return a table variable, or use a stored procedure so that you can get a data set

The SQL Statement should be similar to this:

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


来源:https://stackoverflow.com/questions/1633872/how-to-get-the-item-count-for-sub-categories-on-each-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!