sql server increasing packet sequence number

SQL2000 homemade row_number

SELECT (SELECT COUNT (*) FROM main1 p1 WHERE p1. No. <= p2. No.) rowNumber the AS, *
  the FROM P2 the ORDER BY ID MAIN1

Very simple, but the data will be very large when consumption performance

After sql server2005

ROW_NUMBER () OVER () function usage Comments (packet ordering example of multi)
original top ten last published in a San 2018-09-18 19:11:38 reading number 92385 Collection
launched

And San ten java learn together
to learn and share column
a San ten
¥ 9.90
to subscribe
syntax: row_number () over (partition by grouping column sort order by column desc)

row_number () over () packet sorting:

In use row_number () over () function of time, over () inside the packet and later executed in the sort where, group by, order by execution.

Example one:

Table data:

create table TEST_ROW_NUMBER_OVER(
id varchar(10) not null,
name varchar(10) null,
age varchar(10) null,
salary int null
);
select * from TEST_ROW_NUMBER_OVER t;

insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(1,'a',10,8000);
insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(1,'a2',11,6500);
insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(2,'b',12,13000);
insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(2,'b2',13,4500);
insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(3,'c',14,3000);
insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(3,'c2',15,20000);
INTO TEST_ROW_NUMBER_OVER INSERT (ID, name, Age, the salary) values (. 4, 'D', 16,30000);
INSERT INTO TEST_ROW_NUMBER_OVER (ID, name, Age, the salary) values (. 5, 'D2', 17,1800);
a sort: sort query results (no grouping)

select id,name,age,salary,row_number()over(order by salary desc) rn
from TEST_ROW_NUMBER_OVER t
结果:

 

Further sorting: Sort packet id

select id,name,age,salary,row_number()over(partition by id order by salary desc) rank
from TEST_ROW_NUMBER_OVER t
结果:

 

 Again Sort: identify a serial number for each group of data

select * from(select id,name,age,salary,row_number()over(partition by id order by salary desc) rank
from TEST_ROW_NUMBER_OVER t)
where rank <2
结果:

 

Sorting find out the age of 13 years old to 16 years of data, sorted by salary

SELECT ID, name, Age, the salary, ROW_NUMBER () over (order by salary desc) rank
from TEST_ROW_NUMBER_OVER T WHERE Age BETWEEN '13 is' and '16'
results: The results in rank sequence number, in fact, demonstrated over (order by salary desc) is executed after the where age between and

 

Example two:

1. ROW_NUMBER () function number, such as

select email, customerID, ROW_NUMBER () over (order by psd) as rows from QT_Customer
principle: press psd sort order is finished, each of the data to be numbered.

2. In order to sort in ascending order of price, and sorting code to each record is as follows:

DID the SELECT, customerID, the totalPrice, ROW_NUMBER () over (the Order by the totalPrice) AS rows OP_Order from
3. statistics for each of each household and per amount of all orders in ascending order of a customer order, while giving each customer orders are numbered. So you know at every single client a few:

ROW_NUMBER the SELECT () over (Partition by customerID the Order by the totalPrice)
AS rows, customerID, the totalPrice, DID from OP_Order
4. orders statistics each client recently was under the orders of the first few times:

AS tabs with
(
SELECT the ROW_NUMBER () over (Order by customerID Partition by the totalPrice)
AS rows, customerID, the totalPrice, the DID from OP_Order
)
SELECT MAX (rows) AS 'single number', customerID tabs from
Group customerID by
5. The statistics for each the minimum amount a customer to purchase all of the order, and the statistics and change the order, the customer is many times the purchase of:

Idea: to perform this operation using the temporary table.

1. The press group the customers, and then press the timing clients are sorted, and are numbered.

2. Then use the sub-query to find out the minimum price at which each customer purchase.

3. According to locate the appropriate records to find out the minimum price for each customer.

with tabs as
(
select ROW_NUMBER() over(partition by customerID order by insDT)
as rows,customerID,totalPrice, DID from OP_Order
)
select * from tabs
where totalPrice in
(
select MIN(totalPrice)from tabs group by customerID
)
6.筛选出客户第一次下的订单。

Ideas. Use rows = 1 to query customer order records for the first time under.

AS tabs with
(
SELECT the ROW_NUMBER () over (by customerID Partition Order by insDT) AS rows, from OP_Order *
)
SELECT * WHERE rows from tabs. 1 =
SELECT * from OP_Order
7. The NOTE: When using the windowing function over the like, over Night performed inside the grouping and ordering execution in "where, group by, order by " a.

SELECT
the ROW_NUMBER () over (by customerID Partition Order by insDT) AS rows,
customerID, the totalPrice, the DID
from OP_Order WHERE insDT>
 
'2011-07-22' ----------------
Rights Disclaimer: this article is CSDN bloggers, "a San ten" original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_25221835/article/details/82762416

Guess you like

Origin www.cnblogs.com/yclizq/p/12185365.html