search
HomeDatabaseMysql TutorialSqlServer中Cube RollUp 的用法

Cube 、RollUp可以对查询的数据进行汇总,在数据统计中经常用到,尤其是做报表时,用在Select语句中,下面就对两种统计方式进行对比: 先做准备工作: View Code -- --插入随机数据 DECLARE @i INT DECLARE @rand MONEY DECLARE @date DATETIME DECLARE @ind

Cube 、RollUp可以对查询的数据进行汇总,在数据统计中经常用到,尤其是做报表时,用在Select语句中,下面就对两种统计方式进行对比:

先做准备工作:

SqlServer中Cube RollUp 的用法SqlServer中Cube RollUp 的用法View Code

<span>--</span><span>--插入随机数据</span>
<span>DECLARE</span> <span>@i</span> <span>INT</span> 
<span>DECLARE</span> <span>@rand</span> <span>MONEY</span>
<span>DECLARE</span> <span>@date</span> <span>DATETIME</span>
<span>DECLARE</span> <span>@index</span> <span>INT</span> 
<span>DECLARE</span> <span>@DateBase</span> <span>INT</span> 
<span>SET</span> <span>@date</span> <span>=</span> <span>'</span><span>2012-10-23</span><span>'</span>
<span>SET</span> <span>@i</span> <span>=</span> <span>1</span>
<span>WHILE</span> ( <span>@i</span> <span> <span>18</span><span> ) 
    </span><span>BEGIN</span>
        <span>SET</span> <span>@rand</span> <span>=</span> <span>RAND</span>() <span>*</span> <span>20</span>
        <span>SET</span> <span>@index</span> <span>=</span> <span>CAST</span>(<span>RAND</span>() <span>*</span> <span>3</span> <span>AS</span> <span>INT</span><span>)
        </span><span>SET</span> <span>@DateBase</span> <span>=</span> <span>CAST</span>(<span>RAND</span>() <span>*</span> <span>10</span> <span>AS</span> <span>INT</span><span>)

        </span><span>INSERT</span>  <span>INTO</span><span> t_test
                ( id ,
                  productName ,
                  price ,
                  num ,
                  amount ,
                  operatedate  
                
                )
        </span><span>VALUES</span>  ( <span>@i</span><span> ,
                  </span><span>'</span><span>product</span><span>'</span> <span>+</span> <span>CAST</span> (<span>@index</span> <span>AS</span> <span>VARCHAR</span>(<span>10</span><span>)) ,
                  </span><span>@rand</span><span> ,
                  </span><span>100</span><span> ,
                  </span><span>@rand</span> <span>*</span> <span>100</span><span> ,
                  </span><span>@date</span> <span>+</span> <span>@DateBase</span><span>          
                )
        </span><span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>+</span> <span>1</span>
    <span>END</span>

<span>SELECT</span> <span>*</span> <span>FROM</span><span> t_test


分别用两种方式统计:


按 Ctrl</span><span>+</span><span>C 复制代码
</span><span>View</span><span> Code 

</span><span>SELECT</span>  
        <span>CASE</span> <span>WHEN</span> <span>GROUPING</span>(operatedate) <span>=</span> <span>1</span> <span>THEN</span> <span>'</span><span>小计</span><span>'</span>
             <span>ELSE</span> <span>CONVERT</span>(<span>VARCHAR</span>(<span>10</span>), operatedate, <span>120</span><span>)
        </span><span>END</span> <span>AS</span><span> 日期 ,
        </span><span>CASE</span> <span>WHEN</span> <span>GROUPING</span>(productName) <span>=</span> <span>1</span> <span>THEN</span> <span>'</span><span>小计</span><span>'</span>
             <span>ELSE</span><span> productName
        </span><span>END</span> <span>AS</span><span> 产品名称 ,
        </span><span>SUM</span>(amount) <span>/</span> <span>SUM</span>(num) <span>AS</span><span> 平均价格 ,
        </span><span>SUM</span>(num) <span>AS</span><span> 数量 ,
        </span><span>SUM</span>(amount) <span>AS</span><span> 金额
</span><span>FROM</span><span>    t_test
</span><span>GROUP</span> <span>BY</span><span>  operatedate,productName
</span><span>WITH</span> ROLLUP  <span>/*</span><span>WITH Cube</span><span>*/</span><span>
按 Ctrl</span><span>+</span>C 复制代码</span>

 

SqlServer中Cube RollUp 的用法          SqlServer中Cube RollUp 的用法

CUBE 会对所有的分组字段进行统计,如上例,先对日期求小计,也就是统计每天的产品总金额,然后统计每个产品的总金额,最后给出总的合计。

ROLLUP 按照分组顺序,先对第一个字段operatedate分组,在组内进行统计,最后给出合计。

区别就是: ROLLUP 不会去统计group by 后面的第一个字段的小计

Grouping(字段名) 用来区分当前行是不是小计产生的行,  Grouping(字段名)=1 说明是统计行,Grouping(字段名)=0 说明是表中行,可以用在case,where 后面

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
sqlserver数据库中已存在名为的对象怎么解决sqlserver数据库中已存在名为的对象怎么解决Apr 05, 2024 pm 09:42 PM

对于 SQL Server 数据库中已存在同名对象,需要采取以下步骤:确认对象类型(表、视图、存储过程)。如果对象为空,可使用 IF NOT EXISTS 跳过创建。如果对象有数据,使用不同名称或修改结构。使用 DROP 删除现有对象(谨慎操作,建议备份)。检查架构更改,确保没有引用删除或重命名的对象。

sqlserver服务无法启动怎么办sqlserver服务无法启动怎么办Apr 05, 2024 pm 10:00 PM

当 SQL Server 服务无法启动时,可采取以下步骤解决:检查错误日志以确定根本原因。确保服务帐户具有启动服务的权限。检查依赖项服务是否正在运行。禁用防病毒软件。修复 SQL Server 安装。如果修复不起作用,重新安装 SQL Server。

怎么查看sqlserver端口号怎么查看sqlserver端口号Apr 05, 2024 pm 09:57 PM

要查看 SQL Server 端口号:打开 SSMS,连接到服务器。在对象资源管理器中找到服务器名称,右键单击它,然后选择“属性”。在“连接”选项卡中,查看“TCP 端口”字段。

sqlserver数据库在哪里sqlserver数据库在哪里Apr 05, 2024 pm 08:21 PM

SQL Server 数据库文件通常存储在以下默认位置:Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data可通过修改数据库文件路径设置来自定义数据库文件位置。

Java连接SqlServer错误如何解决Java连接SqlServer错误如何解决May 01, 2023 am 09:22 AM

问题发现这次使用的是SqlServer数据库,之前并没有使用过,但是问题不大,我按照需求文档的步骤连接好SqlServer之后,启动SpringBoot项目,发现了一个报错,如下:刚开始我以为是SqlServer连接问题呢,于是便去查看数据库,发现数据库一切正常,我首先第一时间问了我的同事,他们是否有这样的问题,发现他们并没有,于是我便开始了我最拿手的环节,面向百度编程。开始解决具体报错信息是这样,于是我便开始了百度报错:ERRORc.a.d.p.DruidDataSource$CreateCo

sqlserver英文安装怎么更改中文sqlserver英文安装怎么更改中文Apr 05, 2024 pm 10:21 PM

SQL Server 英文安装可通过以下步骤更改为中文:下载相应语言包;停止 SQL Server 服务;安装语言包;更改实例语言;更改用户界面语言;重启应用程序。

Win11无法安装SQL Server的原因及解决方案Win11无法安装SQL Server的原因及解决方案Dec 27, 2023 pm 07:48 PM

有网友反馈,在win11上无法安装sqlserver这款软件,不知道是怎么回事,根据目前的测试来看,win11存在硬盘问题,部分接口硬盘无法安装这款软件。win11为啥不能安装sqlserver:答:win11不能安装sqlserver是硬盘的问题。1、据了解,win11存在对于硬盘的检测bug。2、这导致sqlserver无法在“三星m.2接口”硬盘上安装。3、因此,如果我们要安装的话,需要准备一块其他硬盘。4、然后将该硬盘安装到电脑里,如果没有额外插槽的话就要换掉之前的硬盘。5、安装完成后,

sqlserver数据库日志怎么查询sqlserver数据库日志怎么查询Apr 05, 2024 pm 09:06 PM

可以通过以下步骤查询 SQL Server 数据库日志:1. 打开 SQL Server Management Studio,连接到数据库服务器;2. 展开“管理”节点,导航到“SQL Server 日志”;3. 选择要查询的日志文件,右键单击并选择“查看日志文件”;4. 浏览日志记录。其他查询日志方法:使用 Transact-SQL 查询、PowerShell Cmdlet。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version