3

My query is as follows

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

I need to achieve total row count from last select statement. which mean total row count that is being selected.

or any idea that might be use in this condition

これどうやってするの ..

4

2 に答える 2

2

明確にするために、外側のクエリではなく、CTE にカウントを追加する必要があります。外側の選択は 1 行のみを返すため、カウントは常に 1 になります。

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 に答える