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字句实现连续/累计聚合 ...
随机推荐
- 解决 Could not resolve com.android.tools.build:gradle:3.1.3
android studio 升级到3.1.3后总会遇到莫名其妙的错误,前几天刚解决了项目 dependencies报错的问题. 解决android studio 升级到3.0+之后 项目 dep ...
- 关于调用&&传址
关于调用&&传址//数组int d1[];Function(int *d){} int main(){Function(d1);} //指针int *p;Function(int *q ...
- 001.etcd简介及单节点使用
一 etcd简介 1.1 概述 etcd 是 CoreOS 团队发起的开源项目,是一个管理配置信息和服务发现(service discovery)的项目,它的目标是构建一个高可用的分布式键值(key- ...
- 002.LVM创建
一 LVM创建步骤 创建分区 创建PV 创建VG 创建LV 格式化及挂载 二 创建分区 使用分区工具(如fdisk等)创建LVM分区,却将分区标识为LVM的分区类型8e. [root@kauai ~] ...
- WIN10下使用Anaconda配置opencv、tensorflow、pygame并在pycharm中运用
昨天想运行一段机器学习的代码,在win10系统下配置了一天的python环境,真的是头疼,准备写篇博客来帮助后面需要配置环境的兄弟. 1.下载Anaconda 根据昨天的经历,发现Anaconda真的 ...
- [ 转载 ] Http详解
HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...
- BZOJ.1013.[JSOI2008]球形空间产生器(高斯消元)
题目链接 HDU3571 //824kb 40ms //HDU3571弱化版 跟那个一比这个太水了,练模板吧. //列出$n+1$个二次方程后两两相减,就都是一次方程了. #include <c ...
- BZOJ.2246.[SDOI2011]迷宫探险(DP 记忆化搜索 概率)
题目链接 求最大的存活概率,DP+记忆化. 用f[s][x][y][hp]表示在s状态,(x,y)点,血量为hp时的存活概率. s是个三进制数,记录每个陷阱无害/有害/未知. 转移时比较容易,主要是在 ...
- Xtreme9.0 - Communities 强连通
Xtreme9.0 - Communities 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/c ...
- 数据库操作类——C#
整理数据库操作类以便取用: using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...