The ninth day of SQL Learning

Source: Internet
Author: User
On the ninth day of SQL study, SQL always considers over to be used in combination with row_number (). Today, we suddenly find that over can be combined with count. Now let's take a look at how it works with over! In this example, we will understand it: Create a table ([dbo]. [Orders] field Description: orderid -- order id, customerid -- consumer I

On the ninth day of SQL study, SQL always considers over to be used in combination with row_number (). Today, we suddenly find that over can be combined with count. Now let's take a look at how it works with over! In this example, we will understand it: Create a table ([dbo]. [Orders] field Description: orderid -- order id, customerid -- consumer I

The ninth day of SQL Learning -- SQL over

In the past, I always thought that over is used in combination with row_number (), website space, and U.S. space. Today I suddenly found that it can be combined with count. Now let's take a look at how it works with over!

Or understand it from the example:

Table creation ([dbo]. [Orders] field Description: orderid -- order id, customerid -- consumer id ):

. (, (5) COLLATE Chinese_PRC_CI_AS NULL ,())

Insert data to a table:

); Insert into dbo. Orders values (7, null );

Query the inserted data:

Dbo. orders

Result

Directly compare the preceding three SQL statements, such as the Virtual Host.

SQL statement 1 (simple query of all data ):

Dbo. Orders

SQL statement 2 (the combination of count and over is used ):

Select orderid, customerid, count (*) over (partition by customerid) as num_ordersfrom orders

SQL statement 3 (combining count and over with conditions ):

Select orderid, customerid, count (*) over (partition by customerid) as num_ordersfrom ordersorderid

Result Analysis diagram:

After reading the graph, you may understand what is going on. For partition by, I mentioned earlier (click here for details ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.