Learning Scenarios for SQL Server (about Row_number () and coalesce () use)

Source: Internet
Author: User

BEGIN TRAN

CREATE TABLE [dbo]. [Cardata] (
[Carid] [INT] Null
[Mileage] [INT] Null
[M_year] [INT] Null
[M_month] [INT] Null
[M_day] [INT] Null
) on [PRIMARY]
GO
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (1, 10, 2015, 1, 1)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (1, 15, 2015, 1, 2)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (1, 15, 2015, 1, 5)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (1, 20, 2015, 1, 6)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (1, 26, 2015, 1, 9)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (1, 30, 2015, 1, 10)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (1, 35, 2015, 1, 11)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (2, 20, 2015, 1, 5)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (2, 22, 2015, 1, 8)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (2, 40, 2015, 1, 10)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (2, 45, 2015, 1, 11)
INSERT [dbo]. [Cardata] ([Carid], [mileage], [m_year], [M_month], [M_day]) VALUES (3, 50, 2015, 1, 11)

ROLLBACK

--Use the SQL statement to count the mileage per car (not the total mileage)
--Key points: How to get on a record, do you have a function like rownum? Yes! Row_number ()
--After sorting according to Carid, the rownum
SELECT row_number () over (order by C.carid) as rownum,c.* from Cardata C

--After grouping by Carid
SELECT C.carid, Row_number () over (PARTITION by C.carid ORDER by C.m_year,c.m_month,c.m_day) as RowNum, c.mileage from Car Data C

--Bo Friends answer
With both as (
SELECT row_number () over (PARTITION by Carid ORDER by Carid, M_year, M_month, M_day) as NodeId
, C.carid
, C.mileage
, C.m_year
, C.m_month
, C.m_day
From Cardata as C
)
SELECT a.*
, A.mileage-coalesce (b.nextmileage, 0) as ' incremental '
From both as A
OUTER APPLY (SELECT mileage as nextmileage from both as b WHERE B.nodeid = a.nodeid-1 and B.carid = A.carid) as B;

---supplementary knowledge function coalesce
--Indicates that at least one parameter should be of the NULL type. That returns the value of the first non-null type
Select COALESCE (null,null);
SELECT COALESCE (Null,null,getdate ());
SELECT COALESCE (null,0);

Learning Scenarios for SQL Server (about Row_number () and coalesce () use)

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.