Home > Database > Mysql Tutorial > Sql学习第九天

Sql学习第九天

WBOY
Release: 2016-06-07 17:44:46
Original
1103 people have browsed it

Sql学习第九天SQL 关于over 以前总是认为over是与row_number()结合使用的,今天突然发现它还可以与count()结合。现在就来看看它是怎样与over结合的吧! 还是从例子中理解它: 建表( [dbo].[Orders] 字段说明:orderid -- 订单id , customerid -- 消费者i

Sql学习第九天——SQL 关于over

以前总是认为over是与row_number()结合使用的,网站空间,美国空间,今天突然发现它还可以与count()结合。现在就来看看它是怎样与over结合的吧!

还是从例子中理解它:

建表([dbo].[Orders]  字段说明:orderid -- 订单id  , customerid -- 消费者id):

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

向表插入数据:

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

查询插入的数据:

dbo.orders

结果如图:

直接上三条sql语句比较进行对比说明,虚拟主机,这样比较明了。

sql语句一(简单的查询所有的数据):

dbo.Orders

sql语句二(用到了count与over的结合):

select orderid , customerid,count(*) over(partition by customerid) as num_orders from orders

sql语句三(用到了count与over的结合并且带上了条件):

select orderid , customerid,count(*) over(partition by customerid) as num_orders from orders orderid

结果分析图:

 

看完图可能都会明白是怎么一回事儿了,对于partition by 我前面有说(查看请点次链接)。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template