COUNT DISTINCT ROW_NUMBER DENSE_RANK 以及对COUNT去重(非PARTITION)

简介: 1:COUNT DISTINCT         SELECT          COUNT(DISTINCT [QS_QuestionStem].Id)  AS ReqCount1,         [QS_QuestionStem].

1:COUNT DISTINCT

        SELECT 
        COUNT(DISTINCT [QS_QuestionStem].Id)  AS ReqCount1,
        [QS_QuestionStem].Content AS Content
        FROM [EL_QuestionBank].[QS_QuestionStem] AS [QS_QuestionStem]
        INNER JOIN [EL_QuestionBank].[QS_QuestionProperty] AS [QS_QuestionProperty]
        ON [QS_QuestionStem].Id=[QS_QuestionProperty].[QuestionStemCode]
        INNER JOIN [EL_QuestionBank].[QuestionCategory] AS [QuestionCategory]
        ON [QuestionCategory].Id=[QS_QuestionProperty].SubjectCode
        LEFT JOIN [EL_QuestionBank].[QS_QuestionOptions] AS [QS_QuestionOptions]
        ON [QS_QuestionOptions].QuestionStemCode=[QS_QuestionStem].Id
        WHERE [QuestionCategory].Id=@CategoryIds OR charindex(@CategoryIds,[QuestionCategory].ParentIds )>0
        AND [QS_QuestionStem].QuestionFaceCode IS null
        GROUP BY [QS_QuestionStem].Content

image

2:DENSE_RANK Vs ROW_NUMBER

        SELECT 
        DENSE_RANK() OVER (ORDER By [QS_QuestionStem].Id) AS X,
        ROW_NUMBER() OVER (ORDER By [QS_QuestionStem].Id) AS Y,
        COUNT([QS_QuestionStem].Id) OVER()  AS ReqCount2,
        [QS_QuestionStem].Content AS Content
        FROM [EL_QuestionBank].[QS_QuestionStem] AS [QS_QuestionStem]
        INNER JOIN [EL_QuestionBank].[QS_QuestionProperty] AS [QS_QuestionProperty]
        ON [QS_QuestionStem].Id=[QS_QuestionProperty].[QuestionStemCode]
        INNER JOIN [EL_QuestionBank].[QuestionCategory] AS [QuestionCategory]
        ON [QuestionCategory].Id=[QS_QuestionProperty].SubjectCode
        LEFT JOIN [EL_QuestionBank].[QS_QuestionOptions] AS [QS_QuestionOptions]
        ON [QS_QuestionOptions].QuestionStemCode=[QS_QuestionStem].Id
        WHERE [QuestionCategory].Id=@CategoryIds OR charindex(@CategoryIds,[QuestionCategory].ParentIds )>0
        AND [QS_QuestionStem].QuestionFaceCode IS null

image

3:关于 Count

DECLARE         @CategoryIds varchar(max)='5315f8616e174094ac7251f2e3c4d584';                --分类ID
--DECLARE         @CategoryIds varchar(max)='1';                --分类ID
DECLARE         @PageNumber int=1;                        --页码
DECLARE         @PageSize int=20;                            --页大小
DECLARE         @Fettle int=1;                        --冻结状态
DECLARE         @RecieveState int=2;                    --接收状态:0共享的所有数据;1:未接收;2:已接受
DECLARE         @RecieveStateConstraint bit=NULL;            --约束接收状态(如果为真,则严格按照此值进行过滤)
DECLARE         @AuditState int =null;                    --审核状态
DECLARE         @AuditStateCustom int=null;                --审核状态:2:已审核;小于2:未开始、审核中
DECLARE         @Total int=0;                        --输出参数,总条数

--SELECT * FROM EL_QuestionBank.QuestionCategory
--with tempGR as( select row_number() over( order by A.Id) as ReqIndex ,COUNT(A.Id) OVER() as ReqCount , A.*
--                    from EL_CourseMaker.Course A where CATEGORYID = '1' )
--select * from tempGR where ReqIndex between -99 and 0 order by name

with tempGR as(
    SELECT
        DISTINCT(
            CASE WHEN [QS_QuestionStem].QuestionFaceCode IS NULL THEN [QS_QuestionStem].Id + 'XXXX'
                  WHEN [QS_QuestionStem].QuestionFaceCode IS NOT NULL THEN [QS_QuestionStem].QuestionFaceCode + 'YYYY'
            END) AS XId,
--        DENSE_RANK() OVER (
--            ORDER By (
--                CASE WHEN [QS_QuestionStem].QuestionFaceCode IS NULL THEN [QS_QuestionStem].Id
--                      WHEN [QS_QuestionStem].QuestionFaceCode IS NOT NULL THEN [QS_QuestionStem].QuestionFaceCode
--                END)) AS Y,
        DENSE_RANK() OVER (
            ORDER By (
                CASE WHEN [QS_QuestionStem].QuestionFaceCode IS NULL THEN [QS_QuestionStem].Id
                      WHEN [QS_QuestionStem].QuestionFaceCode IS NOT NULL THEN [QS_QuestionStem].QuestionFaceCode
                END)) AS ReqIndex,
--                MAX(Y),
--        COUNT(*) OVER(ORDER BY [QS_QuestionStem].Id) AS REQCOUNT,
        [QS_QuestionProperty].Difficult AS Difficult,
--        COUNT(distinct coalesce([QS_QuestionStem].Id,[QS_QuestionStem].QuestionFaceCode))  AS ReqCount,
        COUNT((
                CASE WHEN [QS_QuestionStem].QuestionFaceCode IS NULL THEN [QS_QuestionStem].Id
                      WHEN [QS_QuestionStem].QuestionFaceCode IS NOT NULL THEN [QS_QuestionStem].QuestionFaceCode
                END)) OVER()  AS ReqCount,
        (CASE WHEN [QS_QuestionStem].QuestionFaceCode IS NULL THEN [QS_QuestionStem].Content
             WHEN [QS_QuestionStem].QuestionFaceCode IS NOT NULL THEN(SELECT CONTENT FROM [EL_QuestionBank].[QS_QuestionFace] WHERE ID= [QS_QuestionStem].QuestionFaceCode)
        END) AS Content
        FROM [EL_QuestionBank].[QS_QuestionStem] AS [QS_QuestionStem]
        INNER JOIN [EL_QuestionBank].[QS_QuestionProperty] AS [QS_QuestionProperty]
        ON [QS_QuestionStem].Id=[QS_QuestionProperty].[QuestionStemCode]
        INNER JOIN [EL_QuestionBank].[QuestionCategory] AS [QuestionCategory]
        ON [QuestionCategory].Id=[QS_QuestionProperty].SubjectCode
        WHERE [QuestionCategory].Id=@CategoryIds OR charindex(@CategoryIds,[QuestionCategory].ParentIds )>0
--        GROUP BY [QS_QuestionStem].Id,[QS_QuestionStem].QuestionFaceCode
) select * from tempGR  where ReqIndex between 1 and 100


--
--SELECT COUNT(*) FROM [EL_QuestionBank].[QS_QuestionStem] AS [QS_QuestionStem]
--        INNER JOIN [EL_QuestionBank].[QS_QuestionProperty] AS [QS_QuestionProperty]
--        ON [QS_QuestionStem].Id=[QS_QuestionProperty].[QuestionStemCode]
--        INNER JOIN [EL_QuestionBank].[QuestionCategory] AS [QuestionCategory]
--        ON [QuestionCategory].Id=[QS_QuestionProperty].SubjectCode
--        WHERE [QuestionCategory].Id=@CategoryIds OR charindex(@CategoryIds,[QuestionCategory].ParentIds )>0
--        AND [QS_QuestionStem].QuestionFaceCode IS NULL
--   

--SELECT ID FROM [EL_QuestionBank].[QS_QuestionStem] WHERE [QS_QuestionStem].QuestionFaceCode IS null

--SELECT TOP 1 * FROM [EL_QuestionBank].[QS_QuestionFace]

image

4:错误?

我们发现第三步中的 ReqCount 是错误的,那应该如何才是正确的?

image

要实现它?级联一个 with 字句吧:

DECLARE         @CategoryIds varchar(max)='5315f8616e174094ac7251f2e3c4d584';                --分类ID
--DECLARE         @CategoryIds varchar(max)='1';                --分类ID
DECLARE         @PageNumber int=1;                        --页码
DECLARE         @PageSize int=20;                            --页大小
DECLARE         @Fettle int=1;                        --冻结状态
DECLARE         @RecieveState int=2;                    --接收状态:0共享的所有数据;1:未接收;2:已接受
DECLARE         @RecieveStateConstraint bit=NULL;            --约束接收状态(如果为真,则严格按照此值进行过滤)
DECLARE         @AuditState int =null;                    --审核状态
DECLARE         @AuditStateCustom int=null;                --审核状态:2:已审核;小于2:未开始、审核中
DECLARE         @Total int=0;                        --输出参数,总条数

--SELECT * FROM EL_QuestionBank.QuestionCategory
--with tempGR as( select row_number() over( order by A.Id) as ReqIndex ,COUNT(A.Id) OVER() as ReqCount , A.*
--                    from EL_CourseMaker.Course A where CATEGORYID = '1' )
--select * from tempGR where ReqIndex between -99 and 0 order by name
with tempGR as(
--    SELECT COUNT(*) FROM
--    (
        SELECT
            DISTINCT(
                CASE WHEN [QS_QuestionStem].QuestionFaceCode IS NULL THEN [QS_QuestionStem].Id + 'XXXX'
                    WHEN [QS_QuestionStem].QuestionFaceCode IS NOT NULL THEN [QS_QuestionStem].QuestionFaceCode + 'YYYY'
                END) AS XId,
            DENSE_RANK() OVER (
                ORDER By (
                    CASE WHEN [QS_QuestionStem].QuestionFaceCode IS NULL THEN [QS_QuestionStem].Id
                        WHEN [QS_QuestionStem].QuestionFaceCode IS NOT NULL THEN [QS_QuestionStem].QuestionFaceCode
                    END)) AS ReqIndex,
            [QS_QuestionProperty].Difficult AS Difficult,
            (CASE WHEN [QS_QuestionStem].QuestionFaceCode IS NULL THEN [QS_QuestionStem].Content
                WHEN [QS_QuestionStem].QuestionFaceCode IS NOT NULL THEN(SELECT CONTENT FROM [EL_QuestionBank].[QS_QuestionFace] WHERE ID= [QS_QuestionStem].QuestionFaceCode)
            END) AS Content
            FROM [EL_QuestionBank].[QS_QuestionStem] AS [QS_QuestionStem]
            INNER JOIN [EL_QuestionBank].[QS_QuestionProperty] AS [QS_QuestionProperty]
            ON [QS_QuestionStem].Id=[QS_QuestionProperty].[QuestionStemCode]
            INNER JOIN [EL_QuestionBank].[QuestionCategory] AS [QuestionCategory]
            ON [QuestionCategory].Id=[QS_QuestionProperty].SubjectCode
            WHERE [QuestionCategory].Id=@CategoryIds OR charindex(@CategoryIds,[QuestionCategory].ParentIds )>0
--    )
),MAXGR AS(
    SELECT *, count(xid) over() as x FROM TEMPGR
)select * from MAXGR

Creative Commons License本文基于 Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 http://www.cnblogs.com/luminji(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。
目录
相关文章
|
5月前
|
关系型数据库 MySQL
【随手记】MySQL中ROW_NUMBER()、RANK()和DENSE_RANK()函数的用法
【随手记】MySQL中ROW_NUMBER()、RANK()和DENSE_RANK()函数的用法
142 1
|
数据库
Mysql8.0习题系列(八):窗口函数(一篇学会rank、dense_rank、row_number使用,超详细~)
Mysql8.0习题系列(八):窗口函数(一篇学会rank、dense_rank、row_number使用,超详细~)
|
SQL
SQL中rank(),dense_rank(),row_number()的异同
rank函数用于返回结果集的分区内每行的排名,行的排名是相关行之前的排名数加一。
185 0
SQL中rank(),dense_rank(),row_number()的异同
|
Oracle 关系型数据库 MySQL
MySQL窗口函数——分组排序函数:number_rank(),rank(),dense_rank()
MySQL窗口函数——分组排序函数:number_rank(),rank(),dense_rank()
510 0
MySQL窗口函数——分组排序函数:number_rank(),rank(),dense_rank()
|
关系型数据库 MySQL
MySQL - 排序函数 Rank() Over()、Dense_rank() Over()、Row_number() Over()
MySQL - 排序函数 Rank() Over()、Dense_rank() Over()、Row_number() Over()
383 0
MySQL - 排序函数 Rank() Over()、Dense_rank() Over()、Row_number() Over()
|
关系型数据库 MySQL
MYSQL实现排名函数RANK,DENSE_RANK和ROW_NUMBER
本文介绍了MYSQL实现排名函数RANK,DENSE_RANK和ROW_NUMBER
232 0
|
SQL Go
【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例
【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例目录 0、填充数据1、使用row_number()函数对订单进行编号,按照订单时间倒序。
13076 0
|
SQL
SQL 序号列ROW_NUMBER,RANK,DENSE_RANK、NTILE
原文:SQL 序号列ROW_NUMBER,RANK,DENSE_RANK、NTILE SQL 2005新增加相关函数 : ROW_NUMBER,RANK,DENSE_RANK、NTILE 窗口函数 ...
1286 0