3

我的查询如下

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

我需要从最后一个 select 语句中获得总行数。这意味着正在选择的总行数。

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

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

4

2 回答 2

13

请添加COUNT(*) OVER()您的选择,这将返回选择作为新列的总行数。前任:

SELECT 
    *, 
    COUNT(*) OVER() AS [Total_Rows] 
FROM YourTable
于 2013-09-18T10:26:06.740 回答
2

为了清楚起见,您需要将计数添加到 CTE,而不是外部查询。外部选择仅返回一行,因此计数始终为一。

CTE 应该开始:

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,
        COUNT(*) over () as total_count
于 2013-09-18T12:05:57.857 回答