zoukankan      html  css  js  c++  java
  • SQLServer的学习场景(关于row_number()和COALESCE()的使用)

    --使用Sql语句,统计出每辆汽车每天行驶的里程数(不是总里程)

    以下为脚本

    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)

    解题思路:
    --关键点:如何上一条记录,有rowNum之类的函数没?有! row_number()
    --按照carID排序后,进行rowNum
    SELECT ROW_NUMBER() OVER (order by c.carID ) as rowNum,c.* FROM CarData c

    --按照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 CarData c

    --博友答案
    WITH TWO 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 '增量'
    FROM TWO AS A
    OUTER APPLY (SELECT Mileage AS NextMileage FROM TWO AS B WHERE B.NodeId = A.NodeId - 1 AND B.CarId = A.CarId ) AS B;

    ---补充知识 函数COALESCE
    --说明 至少应有一个参数为 NULL 类型。,返回第一个非NUll类型的值
    select COALESCE(null,null);
    SELECT COALESCE(NULL,NULL,GETDATE());
    SELECT COALESCE(NULL,0);

    --------------------
    做一个精神上的素食主义者。
  • 相关阅读:
    盒子垂直水平居中
    Sahi (2) —— https/SSL配置(102 Tutorial)
    Sahi (1) —— 快速入门(101 Tutorial)
    组织分析(1)——介绍
    Java Servlet (1) —— Filter过滤请求与响应
    CAS (8) —— Mac下配置CAS到JBoss EAP 6.4(6.x)的Standalone模式(服务端)
    JBoss Wildfly (1) —— 7.2.0.Final编译
    CAS (7) —— Mac下配置CAS 4.x的JPATicketRegistry(服务端)
    CAS (6) —— Nginx代理模式下浏览器访问CAS服务器网络顺序图详解
    CAS (5) —— Nginx代理模式下浏览器访问CAS服务器配置详解
  • 原文地址:https://www.cnblogs.com/xfile/p/4980199.html
Copyright © 2011-2022 走看看