TSQL Challenge 2
和之前发布的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 CustomerCalls02.-------- ---------- ---------- ----------------- -------------03.522 2010-04-05 345 5 10004.522 2010-04-06 345 5 8005.522 2010-04-07 120 4 5006.522 2010-04-08 345 5 6007.522 2010-04-09 345 5 4008.522 2010-04-10 345 5 7009.522 2010-04-11 117 20 30010.522 2010-04-12 345 5 5511.522 2010-04-14 345 5 7512.522 2010-04-15 260 10 15013.522 2010-04-16 345 5 3014.522 2010-04-17 345 5 9515.522 2010-04-19 345 5 60Expected Results
01.ProductID Start Date End Date TotalLines LinesOutOfService TotalCustomerCalls02.--------- ---------- ---------- ---------- ----------------- ------------------03.522 2010-04-05 2010-04-06 345 5 18004.522 2010-04-07 2010-04-07 120 4 5005.522 2010-04-08 2010-04-10 345 5 17006.522 2010-04-11 2010-04-11 117 20 30007.522 2010-04-12 2010-04-14 345 5 13008.522 2010-04-15 2010-04-15 260 10 15009.522 2010-04-16 2050-12-01 345 5 185Rules
- The output should be ordered by ProductID, Start Date, End Date.
- There will be no duplicate dates within a Product ID.
- The data will contain more than one ProductID.
Restrictions
- 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的更多相关文章
- TSQL Challenge 1
在老外网站发布的一些SQL问题,拿过来自己搞一下,后面我也会陆续转载一些问题,欢迎看到的朋友贴出自己的答案,交流一哈.对于技术问答题的描述,翻译远不不原版来的更好一些,下面我就贴出原版的题目,欢迎参与 ...
- T-SQL Recipes之Separating elements
Separating elements Separating elements is a classic T-SQL challenge. It involves a table called Arr ...
- TSQL Beginners Challenge 3 - Find the Factorial
这是一个关于CTE的应用,这里我们用CTE实现阶乘 Factorial,首先来看一个简单的小实验,然后再来看题目.有的童鞋会问怎么没有2就来3了呢,惭愧,TSQL Beginners Challeng ...
- TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...
- T-SQL学习记录
T-sql是对SQL(structure query language )的升级.可以加函数. 系统数据库:master管理数据库.model模版数据库,msdb备份等操作需要用到的数据库,tempd ...
- 《MSSQL2008技术内幕:T-SQL语言基础》读书笔记(下)
索引: 一.SQL Server的体系结构 二.查询 三.表表达式 四.集合运算 五.透视.逆透视及分组 六.数据修改 七.事务和并发 八.可编程对象 五.透视.逆透视及分组 5.1 透视 所谓透视( ...
- 《MSSQL2008技术内幕:T-SQL语言基础》读书笔记(上)
索引: 一.SQL Server的体系结构 二.查询 三.表表达式 四.集合运算 五.透视.逆透视及分组 六.数据修改 七.事务和并发 八.可编程对象 一.SQL Server体系结构 1.1 数据库 ...
- TSQL 分组集(Grouping Sets)
分组集(Grouping Sets)是多个分组的并集,用于在一个查询中,按照不同的分组列对集合进行聚合运算,等价于对单个分组使用“union all”,计算多个结果集的并集.使用分组集的聚合查询,返回 ...
- T-sql语句查询执行顺序
前言 数据库的查询执行,毋庸置疑是程序员必备技能之一,然而数据库查询执行的过程绚烂多彩,却是很少被人了解,今天哥哥要带你装逼带你飞,深入一下这sql查询的来龙去脉,为查询的性能优化处理打个基础,或许面 ...
随机推荐
- BZOJ 1072 排列
Description 给一个数字串\(s\)和正整数\(d\), 统计\(s\)有多少种不同的排列能被\(d\)整除(可以有前导\(0\)).例如\(123434\)有\(90\)种排列能被\(2\ ...
- IIC协议及其对ACK应答信号的处理
1,SCL一直由Master控制,SDA依照数据传送的方向,读数据时由Slave控制SDA,写数据时由Master控制SDA.当8位数据传送完毕之后,应答位或者否应答位的SDA控制权与数据位传送时相反 ...
- index unique scan
INDEX UNIQUE SCAN 索引唯一扫描.单块读 只可能发生在unique index/primary key 等值查找 等待事件:db file s ...
- [转]关于Icon,Image,ImageIcon的简单的对比参考
转自:http://blog.csdn.net/bcmissrain/article/details/7190886 其实就算是现在,我还是有不少地方概念模糊,但是下面的内容是是没有什么问题的.稍微介 ...
- 【JS模式】单体模式
<JavaScript模式> /** * 单体(Singleton)模式的思想在于保证一个特定类仅有一个实例.这意味着当您第二次使用同一个类创建新对象的时候,每次得到与第一次创建对象完全相 ...
- 【模拟】NCPC 2014 K Train passengers
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1797 题目大意: 有N个车站,火车一共可以坐M个人,每个车站下车Ai,上车Bi个人,在 ...
- Merge into(oracle)
作用:使用一条sql语句进行insert或者update操作,如果存在就update,如果不存在就insert 语法: MERGE INTO table_name t1 USING (table|vi ...
- workflow4.0持久化
WF4中的持久化模型有了很大的变化. SqlWorkflowInstanceStore是WF提供给我们的开箱即用的类,它会将工作流数据保存到SQL Server 2005或是2008中.该类从Inst ...
- Codelab for Android Design Support Library used in I/O Rewind Bangkok session
At the moment I believe that there is no any Android Developer who doesn't know about Material Desig ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...