SqlServer 下 Group by、having、order by、Distinct 总结
文章总结了SqlServer 下 Group by、having、order by、Distinct用法事项,有需要的朋友可学习一下。
直奔主题,如下SQL语句:
代码如下 | 复制代码 |
SELECT COUNT(*) AS COUNT,REQUEST,METHOD FROM REQUESTMETH GROUP BY REQUEST,METHOD HAVING (REQUEST ='FC.OCEAN.JOB.SERVER.CBIZOZBKHEADER' OR REQUEST='FC.Ocean.Job.Server.CBizOzDocHeader') AND COUNT(*) >3 |
ORDER BY REQUEST注意事项:
HAVING后的条件不能用别名COUNT>3 必须使用COUNT(*) >3,否则报:列名 'COUNT' 无效。
having 子句中的每一个元素并不一定要出现在select列表中
如果把该语句写成:
代码如下 | 复制代码 |
SELECT COUNT(*) AS COUNT,REQUEST,METHOD FROM REQUESTMETH GROUP BY REQUEST ORDER BY REQUEST |
那么将报:
选择列表中的列 'REQUESTMETH.method' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
注意:
1、使用GROUP BY 子句时,SELECT 列表中的非汇总列必须为GROUP BY 列表中的项。
2、分组时,所有的NULL值分为一组。
3、GROUP BY 列表中一般不允许出现复杂的表达试、显示标题以及SELECT列表中的位置标号。
如:
代码如下 | 复制代码 |
SELECT REQUEST,METHOD, COUNT(*) AS COUNT FROM REQUESTMETH GROUP BY REQUEST,2 ORDER BY REQUEST |
错误信息为:每个 GROUP BY 表达式都必须包含至少一个列引用。
GROUP BY 中使用 ORDER BY注意事项:
代码如下 | 复制代码 |
SELECT COUNT(*) AS COUNT FROM REQUESTMETH GROUP BY REQUEST,METHOD ORDER BY REQUEST,METHOD-- |
这样是允许的, ORDER BY后面的字段包含在GROUP BY 子句中
代码如下 | 复制代码 |
SELECT COUNT(*) AS COUNTS FROM REQUESTMETH GROUP BY REQUEST ORDER BY COUNT(*) DESC |
--这样是允许的,ORDER BY后面的字段包含在聚合函数中,结果集同下面语句一样
代码如下 | 复制代码 |
SELECT COUNT(*) AS COUNTS FROM REQUESTMETH GROUP BY REQUEST ORDER BY COUNTS DESC |
--这样是允许的,区别于HAVING,HAVING后不允许跟聚集函数的别名作为过滤条件
代码如下 | 复制代码 |
SELECT COUNT(*) AS COUNTS FROM REQUESTMETH GROUP BY REQUEST ORDER BY METHOD-- |
这样是错误的:ORDER BY 子句中的列 "REQUESTMETH.method" 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
SELECT DISTINCT 中使用 ORDER BY注意事项:
代码如下 | 复制代码 |
SELECT DISTINCT BOOKID FROM BOOK ORDER BY BOOKNAME |
以上语句将报:
--如果指定了SELECT DISTINCT,那么ORDER BY 子句中的项就必须出现在选择列表中。
因为以上语句类似
代码如下 | 复制代码 |
SELECT BOOKID FROM BOOK GROUP BY BOOKID ORDER BY BOOKNAME |
其实错误信息也为:
--ORDER BY子句中的列"BOOK.BookName" 无效,因为该列没有包含在聚合函数或GROUP BY 子句中。
应该改为:
代码如下 | 复制代码 |
SELECT DISTINCT BOOKID,BOOKNAME FROM BOOK ORDER BY BOOKNAMESELECT DISTINCT BOOKID,BOOKNAME FROM BOOKSELECT BOOKID,BOOKNAME FROM BOOK GROUP BY BOOKID,BOOKNAME |
以上两句查询结果是一致的,DISTINCT的语句其实完全可以等效的转换为GROUP BY语句

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to optimize orderBy statement in sql? The following article will introduce to you the method of optimizing the orderBy statement in SQL. It has a good reference value and I hope it will be helpful to you.

Let’s talk about the general conclusion first: when the semantics are the same and there is an index: both groupby and distinct can use the index, and the efficiency is the same. In the case of the same semantics and no index: distinct is more efficient than groupby. The reason is that both distinct and groupby will perform grouping operations, but groupby may perform sorting and trigger filesort, resulting in inefficient SQL execution. Based on this conclusion, you may ask: Why are groupby and distinct efficient when they have the same semantics and indexes? Under what circumstances does groupby perform sorting operations? Find answers to these two questions. Next, let’s take a look at dist

The DISTINCT keyword of mysql has many unexpected uses. 1. It can be used when counting non-duplicate records, such as SELECTCOUNT(DISTINCTid) FROMtablename; it is to count how many records with different IDs in the talbebname table. 2. When you need to return records For specific values of different ids, you can use, for example, SELECTDISTINCTidFROMtablename; to return the specific values of different ids in the talbebname table. 3. The above situation 2 will be ambiguous when you need to return results of more than 2 columns in the mysql table, such as SELECTDISTINCTid, type

Detailed explanation of distinct usage in SQL In SQL databases, we often encounter situations where we need to remove duplicate data. At this time, we can use the distinct keyword, which can help us remove duplicate data and make the query results clearer and more accurate. The basic usage of distinct is very simple, just use the distinct keyword in the select statement. For example, the following is a normal select statement: SELECTcolumn_name

Oracle's distinct usage can filter duplicate rows in the result set to ensure that the value of the specified column or columns returned in the "SELECT" clause is unique. The syntax is "SELECT DISTINCT column 1, column 2, column 3... from table name". "distinct" will sort the returned result set and can be used in conjunction with "order by" to improve efficiency.

MySQL is one of the most widely used relational databases currently. In large data storage and query, optimizing database performance is crucial. Among them, DISTINCT is a commonly used deduplication query operator. This article will introduce how to improve database query performance through MySQL DISTINCT optimization. 1. Principles and Disadvantages of DISTINCT The DISTINCT keyword is used to remove duplicate rows from query results. In the case of large amounts of data, there may be multiple duplicate values in the query, resulting in redundant output data.

The reproduced test database is as follows: CREATETABLE`test_distinct`(`id`int(11)NOTNULLAUTO_INCREMENT,`a`varchar(50)CHARACTERSETutf8DEFAULTNULL,`b`varchar(50)CHARACTERSETutf8DEFAULTNULL,PRIMARYKEY(`id`))ENGINE= InnoDBAUTO_INCREMENT=1DEFAULTCHARSET=latin1;The test data in the table is as follows. Now we need to count the deduplicated columns of these three columns.

SQLORDERBY clause The ORDERBY statement is used to sort the result set based on a specified column. The ORDERBY statement sorts records in ascending order by default. If you need to sort records in descending order, use the DESC keyword. "Orders" table: CompanyOrderNumberIBM3532HuluMiao2356Apple4698IBM6953 Example 1: Display company names in alphabetical order: SELECTCompany,OrderNumberFROMOrdersORDERBYCompany Result: CompanyOrderNumberApple4698H
