COUNT DISTINCT ROW_NUMBER DENSE_RANK 以及对COUNT去重(非PARTITION)
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
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
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 namewith 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]
4:错误?
我们发现第三步中的 ReqCount 是错误的,那应该如何才是正确的?
要实现它?级联一个 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
COUNT DISTINCT ROW_NUMBER DENSE_RANK 以及对COUNT去重(非PARTITION)的更多相关文章
- count distinct 多个字段 或者 count(*) 统计group by 结果
SELECT COUNT(*) FROM( SELECT 列名 FROM 表名 where ( 条件 )GROUP BY 多字段)临时表名 例如: SELECT COUNT(*) FROM(SELEC ...
- 【hive】count() count(if) count(distinct if) sum(if)的区别
表名: user_active_day (用户日活表) 表内容: user_id(用户id) user_is_new(是否新用户 1:新增用户 0:老用户) location_city(用户所在地 ...
- count(distinct) 与group by 浅析
x在传统关系型数据库中,group by与count(distinct)都是很常见的操作.count(distinct colA)就是将colA中所有出现过的不同值取出来,相信只要接触过数据库的同学都 ...
- 【优化】COUNT(1)、COUNT(*)、COUNT(常量)、COUNT(主键)、COUNT(ROWID)、COUNT(非空列)、COUNT(允许为空列)、COUNT(DISTINCT 列名)
[优化]COUNT(1).COUNT(*).COUNT(常量).COUNT(主键).COUNT(ROWID).COUNT(非空列).COUNT(允许为空列).COUNT(DISTINCT 列名) 1. ...
- mysql count distinct 统计结果去重
1.使用distinct去重(适合查询整张表的总数)有多个学校+教师投稿,需要统计出作者的总数select count(author) as total from files每个作者都投稿很多,这里有 ...
- 关于MySQL count(distinct) 逻辑的一个bug【转】
本文来自:http://dinglin.iteye.com/blog/1976026#comments 背景 客户报告了一个count(distinct)语句返回结果错误,实际结果存在值,但是用cou ...
- SQL server 中 COUNT DISTINCT 函数
目的:统计去重后表中所有项总和. 直观想法: SELECT COUNT(DISTINCT *) FROM [tablename] 结果是:语法错误. 事实上,我们可以一同使用 DISTINCT 和 C ...
- spark 例子count(distinct 字段)
spark 例子count(distinct 字段) 例子描述: 有个网站访问日志,有4个字段:(用户id,用户名,访问次数,访问网站) 需要统计: 1.用户的访问总次数去重 2.用户一共访问了多少种 ...
- SQL Server ->> 间接实现COUNT(DISTINCT XXX) OVER(PARTITION BY YYY)
SQL Server 2005版本开始支持了窗口函数(Windowing Function)和OVER字句.SQL Server 2012版本开始支持了窗口函数的ORDER BY字句实现连续/累计聚合 ...
随机推荐
- Divide by Zero 2018 and Codeforces Round #474 (Div. 1 + Div. 2, combined)
思路:把边看成点,然后每条边只能从下面的边转移过来,我们将边按照u为第一关键字,w为第二关键字排序,这样就能用线段树维护啦. #include<bits/stdc++.h> #define ...
- SpringBoot的搭建
一:搭建 1.环境要求 Java7及以上 Spring Framework4.1.5及以上 2.新建一个maven工程 3.pom.xml(生成) <!-- 必须引入spring-parent, ...
- R语言编程艺术(4)R对数据、文件、字符串以及图形的处理
本文对应<R语言编程艺术> 第8章:数学运算与模拟: 第10章:输入与输出: 第11章:字符串操作: 第12章:绘图 =================================== ...
- k8s 集群中的etcd故障解决
一次在k8s集群中创建实例发现etcd集群状态出现连接失败状况,导致创建实例失败.于是排查了一下原因. 问题来源 下面是etcd集群健康状态: [root@docker01 ~]# cd /opt/k ...
- ES7/8新特性学习随笔
随着每年EcmaScript都会为js带来一些新特性,带来更多美化的编程体验,今天就走进一下es2016/2017所带来的新特性 ES7新特性 includes() 指数操作符 ES8新特性 asyn ...
- codevs 2291 糖果堆
题目描述 Description [Shadow 1]第一题 WJMZBMR买了很多糖果,分成了N堆,排成一列.WJMZBMR说,如果Shadow能迅速求出第L堆到第R堆一共有多少糖果,就把这些糖果都 ...
- Bzoj4237 cdq分治+树状数组+单调栈
二维平面在某区域内点的问题,要么树套树,kdtree,要么就是cdq分治了.然而这题树套树和kdtree都不是很好搞的样子,于是我们就只能cdq分治了.首先把点按照横坐标x排序,在每一层我们需要算出右 ...
- PHP 从基础开始 ——重要知识点笔记
PHP static 关键词 通常,当函数完成/执行后,会删除所有变量.不过,有时我需要不删除某个局部变量.实现这一点需要更进一步的工作. 要完成这一点,请在您首次声明变量时使用 static 关键词 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem J. Joke 水题
Problem J. Joke 题目连接: http://codeforces.com/gym/100714 Description The problem is to cut the largest ...
- SSH公钥登录原理
在平时工作中我们经常要远程登录服务器,这就要用到SSH协议: $ ssh user@host 主要有两种登录方式:第一种为密码口令登录,第二种为公钥登录 密码口令登录 通过密码进行登录,主要流程为: ...