和之前发布的TSQL Challenge 1是同一系列的文章,看到那篇学习哪篇,没有固定的顺序,只为锻炼下思维。

  

Compare rows in the same table and group the data

  The challenge is to compare the data of the rows and group the input data. The data needs to be grouped based on the Product ID, Date, TotalLines, LinesOutOfService. You need to check each row in the table to see if that particular product has the different range of values for TotalLines and Linesoutofservice when compared to the rows below. If yes, then insert that date where a different range is encountered into the "End Date" column. If no change in the values are detected then insert a future date "01/12/2050". In the TotalCustomerCalls column you place the sum of CustomerCalls for the given Start Date / End Date group.  

Sample Data

01.ProductID Date       TotalLines LinesOutOfService CustomerCalls
02.--------  ---------- ---------- ----------------- -------------
03.522       2010-04-05 345        5                 100
04.522       2010-04-06 345        5                 80
05.522       2010-04-07 120        4                 50
06.522       2010-04-08 345        5                 60
07.522       2010-04-09 345        5                 40
08.522       2010-04-10 345        5                 70
09.522       2010-04-11 117        20                300
10.522       2010-04-12 345        5                 55
11.522       2010-04-14 345        5                 75
12.522       2010-04-15 260        10                150
13.522       2010-04-16 345        5                 30
14.522       2010-04-17 345        5                 95
15.522       2010-04-19 345        5                 60

Expected Results

01.ProductID Start Date End Date   TotalLines LinesOutOfService TotalCustomerCalls
02.--------- ---------- ---------- ---------- ----------------- ------------------
03.522       2010-04-05 2010-04-06 345        5                 180
04.522       2010-04-07 2010-04-07 120        4                 50
05.522       2010-04-08 2010-04-10 345        5                 170
06.522       2010-04-11 2010-04-11 117        20                300
07.522       2010-04-12 2010-04-14 345        5                 130
08.522       2010-04-15 2010-04-15 260        10                150
09.522       2010-04-16 2050-12-01 345        5                 185

Rules

  1. The output should be ordered by ProductID, Start Date, End Date.
  2. There will be no duplicate dates within a Product ID.
  3. The data will contain more than one ProductID.

Restrictions

  1. The solution should be a single query that starts with a "SELECT" or “;WITH”

The Answe:

  

SELECT ProductId,
[Start Date] = MIN([Date]),
[End Date] = MAX(CASE WHEN [Date] <> gd.ProductMaxDate THEN [DATE] ELSE '2050-12-01' END) ,
TotalLines,
LinesOutOfService,
TotalCustomerCalls = SUM(CustomerCalls)
FROM ( SELECT *,
GroupId = ROW_NUMBER() OVER(ORDER BY ProductId,TotalLines,LinesOutOfService) - ROW_NUMBER() OVER (ORDER BY ProductId,[Date]),
ProductMaxDate = MAX([DATE]) OVER(PARTITION BY ProductId)
FROM TC83) as gd
GROUP BY ProductId,GroupId,TotalLines,LinesOutOfService
ORDER BY productid,[Start Date] ,[End Date]

  

  仍在研究中~~~~~

  发现另一种优雅:

SELECT  *,maxScore=MAX(score)OVER(PARTITION BY id) FROM #tt

SELECT *,maxScore=(SELECT MAX(score) FROM #tt WHERE id=a.id)  FROM #tt a

  以后有机会可以在合适的场合使用。

TSQL Challenge 2的更多相关文章

  1. TSQL Challenge 1

    在老外网站发布的一些SQL问题,拿过来自己搞一下,后面我也会陆续转载一些问题,欢迎看到的朋友贴出自己的答案,交流一哈.对于技术问答题的描述,翻译远不不原版来的更好一些,下面我就贴出原版的题目,欢迎参与 ...

  2. T-SQL Recipes之Separating elements

    Separating elements Separating elements is a classic T-SQL challenge. It involves a table called Arr ...

  3. TSQL Beginners Challenge 3 - Find the Factorial

    这是一个关于CTE的应用,这里我们用CTE实现阶乘 Factorial,首先来看一个简单的小实验,然后再来看题目.有的童鞋会问怎么没有2就来3了呢,惭愧,TSQL Beginners Challeng ...

  4. TSQL Beginners Challenge 1 - Find the second highest salary for each department

    很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...

  5. T-SQL学习记录

    T-sql是对SQL(structure query language )的升级.可以加函数. 系统数据库:master管理数据库.model模版数据库,msdb备份等操作需要用到的数据库,tempd ...

  6. 《MSSQL2008技术内幕:T-SQL语言基础》读书笔记(下)

    索引: 一.SQL Server的体系结构 二.查询 三.表表达式 四.集合运算 五.透视.逆透视及分组 六.数据修改 七.事务和并发 八.可编程对象 五.透视.逆透视及分组 5.1 透视 所谓透视( ...

  7. 《MSSQL2008技术内幕:T-SQL语言基础》读书笔记(上)

    索引: 一.SQL Server的体系结构 二.查询 三.表表达式 四.集合运算 五.透视.逆透视及分组 六.数据修改 七.事务和并发 八.可编程对象 一.SQL Server体系结构 1.1 数据库 ...

  8. TSQL 分组集(Grouping Sets)

    分组集(Grouping Sets)是多个分组的并集,用于在一个查询中,按照不同的分组列对集合进行聚合运算,等价于对单个分组使用“union all”,计算多个结果集的并集.使用分组集的聚合查询,返回 ...

  9. T-sql语句查询执行顺序

    前言 数据库的查询执行,毋庸置疑是程序员必备技能之一,然而数据库查询执行的过程绚烂多彩,却是很少被人了解,今天哥哥要带你装逼带你飞,深入一下这sql查询的来龙去脉,为查询的性能优化处理打个基础,或许面 ...

随机推荐

  1. Node.js工具模块

    在Node.js的模块库中提供实用的模块数量. 这些模块都是很常见的,并同时开发基于任何节点的应用程序频繁使用. S.N. 模块的名称和说明 1 OS Module提供基本的操作系统相关的实用功能 2 ...

  2. Kernel PCA 原理和演示

    Kernel PCA 原理和演示 主成份(Principal Component Analysis)分析是降维(Dimension Reduction)的重要手段.每一个主成分都是数据在某一个方向上的 ...

  3. 大用处--PowerShell Management Library for Hyper-V.

    http://pshyperv.codeplex.com/releases 用脚本来收集及集成HYPER-V管理. 越来越似KVM,LIBVIRT啦.

  4. Git各种错误汇总

    1.github上版本和本地上版本冲突的方法,即提交时会提示如下错误: 解决方法,提交时采用如下代码: git push -u origin master -f 参考链接: http://blog.c ...

  5. VS2010安装Visual Assist

    Visual Assist X是一款非常好的Microsoft Visual Studio 2005和Visual Studio .NET插件,支持C/C++,C#,ASP,Visual Basic, ...

  6. 基于Node.js的强大爬虫 能直接发布抓取的文章哦

    基于Node.js的强大爬虫 能直接发布抓取的文章哦 基于Node.js的强大爬虫能直接发布抓取的文章哦!本爬虫源码基于WTFPL协议,感兴趣的小伙伴们可以参考一下 一.环境配置 1)搞一台服务器,什 ...

  7. 账户管理groupadd groupmod groupdel usermod usermod userdel

    http://www.cnblogs.com/ggjucheng/archive/2012/08/21/2648380.html http://blog.csdn.net/qq1603013767/a ...

  8. 构造函数语义学之Copy Constructor构建操作(2)

    二.详述条件 3 和 4 那么好,我又要问大家了,条件1 和 2比较容易理解.因为member object或 base class 含有copy constructor.那么member objec ...

  9. bzoj1005

    有了bzoj1430的经验解决这题就不是什么难事了首先考虑度数确定的点,令tot=sigma(d[i]-1)首先给这tot个数分配prufer编码的位置有C(tot,n-2)种方案每个方案中是可以进行 ...

  10. CH Round #49 - Streaming #4 (NOIP模拟赛Day2)

    A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...