我的查询如下

BEGIN

    WITH MyCTE
    AS (
        SELECT T.MusicAlbumTitle
            ,D.musicTitle
            ,D.mVideoID
            ,D.musicFileName
            ,T.ReleaseDate AS ReleasedDate
            ,D.MusicLength
            ,D.musicSinger
            ,D.MusicVideoID
            ,D.ExternalLink
            ,D.CoverImg
            ,ROW_NUMBER() OVER (
                PARTITION BY D.MusicVideoID ORDER BY D.mVideoID
                ) AS row_num
        FROM dbo.Music_Video T
        JOIN dbo.Music_Video_Details D ON T.MusicVideoID = D.MusicVideoID
        WHERE T.PortalID = @PortalID
            AND T.CultureCode = @CultureCode
            AND T.ComingSoon <> 1
        GROUP BY T.MusicAlbumTitle
            ,D.musicTitle
            ,D.mVideoID
            ,T.ReleaseDate
            ,D.musicFileName
            ,D.MusicLength
            ,D.musicSinger
            ,D.MusicVideoID
            ,D.ExternalLink
            ,D.CoverImg
        )
    SELECT a.mVideoID
        ,a.MusicVideoID
        ,a.musicFileName
        ,a.MusicAlbumTitle
        ,a.ReleasedDate
        ,a.row_num
        ,a.CoverImg
        ,a.ExternalLink
        ,a.musicTitle
        ,a.MusicLength
    FROM MyCTE a
    WHERE row_num = 1
    ORDER BY MusicVideoID DESC
END


我需要从最后一个选择语句获得总行数。
这表示正在选择的总行数。

或在这种情况下可能使用的任何想法

我怎样才能做到这一点 ..

最佳答案

请在选择中添加COUNT(*) OVER(),这将返回选择为新列的总行。
例如:

SELECT
    *,
    COUNT(*) OVER() AS [Total_Rows]
FROM YourTable

10-06 02:42