在SQL Server中跳过少量行数(*)

时间:2017-04-19 19:23:46

标签: sql-server

我正在处理存档类型功能,在此查询中显示每年一个月的数量。但我的客户要求是他不希望这个档案中有最新的50个帖子。我试过了自己的一面,但它没有用。

表格设计

Create Table tblBlogDetails
(
    UniqueKey UniqueIdentifier,
    BlogText NVARCHAR(MAX),
    BlogType Varchar(40),
    BlogingDateTime Datetime
)

这是工作代码

 SELECT
        DATEPART(YEAR, BlogingDateTime) AS [BlogingYear],
        DATEPART(MONTH, BlogingDateTime) AS [BlogingMonth],
        CASE WHEN DATEPART(MONTH, BlogingDateTime) =1 THEN 'January'
             WHEN DATEPART(MONTH, BlogingDateTime) =2 THEN 'February'
             WHEN DATEPART(MONTH, BlogingDateTime) =3 THEN 'March'
             WHEN DATEPART(MONTH, BlogingDateTime) =4 THEN 'April'
             WHEN DATEPART(MONTH, BlogingDateTime) =5 THEN 'May'
             WHEN DATEPART(MONTH, BlogingDateTime) =6 THEN 'June'
             WHEN DATEPART(MONTH, BlogingDateTime) =7 THEN 'July'
             WHEN DATEPART(MONTH, BlogingDateTime) =8 THEN 'August'
             WHEN DATEPART(MONTH, BlogingDateTime) =9 THEN 'September'
             WHEN DATEPART(MONTH, BlogingDateTime) =10 THEN 'October'
             WHEN DATEPART(MONTH, BlogingDateTime) =11 THEN 'November'
             WHEN DATEPART(MONTH, BlogingDateTime) =12 THEN 'December' END AS BLogingMonthName,
        COUNT(*) AS NumberOfBlog 
    FROM tblBlogdetails
    WHERE UniqueKey=@UniqueKey AND
    BlogType='BLOG' 
    GROUP BY DATEPART(YEAR, BlogingDateTime) ,DATEPART(MONTH, BlogingDateTime) 
    ORDER BY DATEPART(YEAR, BlogingDateTime) DESC ,DATEPART(MONTH, BlogingDateTime);

/*
    Current OutPut
    BlogingYear BlogingMonth BLogingMonthName  NumberOfBlog
    2017        1            January           20
    2017        3            March             25
    2017        4            April             40

    Required OutPut
    2017        1            January           20
    2017        3            March             15
*/

1 个答案:

答案 0 :(得分:2)

我添加了另一个过滤器。看看这是否有效。

 SELECT
    DATEPART(YEAR, BlogingDateTime) AS [BlogingYear],
    DATEPART(MONTH, BlogingDateTime) AS [BlogingMonth],
    CASE WHEN DATEPART(MONTH, BlogingDateTime) =1 THEN 'January'
         WHEN DATEPART(MONTH, BlogingDateTime) =2 THEN 'February'
         WHEN DATEPART(MONTH, BlogingDateTime) =3 THEN 'March'
         WHEN DATEPART(MONTH, BlogingDateTime) =4 THEN 'April'
         WHEN DATEPART(MONTH, BlogingDateTime) =5 THEN 'May'
         WHEN DATEPART(MONTH, BlogingDateTime) =6 THEN 'June'
         WHEN DATEPART(MONTH, BlogingDateTime) =7 THEN 'July'
         WHEN DATEPART(MONTH, BlogingDateTime) =8 THEN 'August'
         WHEN DATEPART(MONTH, BlogingDateTime) =9 THEN 'September'
         WHEN DATEPART(MONTH, BlogingDateTime) =10 THEN 'October'
         WHEN DATEPART(MONTH, BlogingDateTime) =11 THEN 'November'
         WHEN DATEPART(MONTH, BlogingDateTime) =12 THEN 'December' END AS BLogingMonthName,
    COUNT(*) AS NumberOfBlog 
FROM tblBlogdetails
WHERE UniqueKey=@UniqueKey 
       AND
      BlogType='BLOG'
      AND
      UniqueKey NOT IN (SELECT TOP 50 UniqueKey
                        FROM tblBlogdetails
                        ORDER BY BlogingDateTime DESC)
GROUP BY DATEPART(YEAR, BlogingDateTime) ,DATEPART(MONTH, BlogingDateTime) 
ORDER BY DATEPART(YEAR, BlogingDateTime) DESC ,DATEPART(MONTH, BlogingDateTime);