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

TSQL Challenge 1 - Pair-wise and ordered assignment of objects from two different lists

  This challenge will be interesting for TSQL enthusiasts as well as bird lovers! It involves assigning food items to birds from two different baskets. Here is a ‘relational’ representation of birds, food items and baskets in the form of tables and rows.

  Table Birds lists the birds which are the recipients of food items. The baskets containing the food items are the tables Grains and Fruits. Whenever possible, you must assign the food items in pairs and they must be taken from each table in alphabetical order. When one of the tables no longer has a food item for a bird you must output a null in the corresponding column and continue assigning food items from the other table until that one runs out of food baskets as well.

Sample Data

Birds Table

1.Code Name
2.---- -------
3.1    Pigeon
4.2    Sparrow
5.3    Parrot

Grains Table

1.Code Grain
2.---- ------
3.1    Wheat
4.1    Rice
5.2    Corn
6.2    Millet

Fruits Table

1.Code Fruit
2.---- ------
3.1    Banana
4.1    Mango
5.1    Guava
6.2    Grapes
 

Expected Results

 
1.Code Bird    Grain  Fruit
2.---- ------- ------ ------
3.1    Pigeon  Rice   Banana
4.1    Pigeon  Wheat  Guava
5.1    Pigeon  NULL   Mango
6.2    Sparrow Corn   Grapes
7.2    Sparrow Millet NULL
8.3    Parrot  NULL   NULL

Rules

  1. For a bird with no food basket at all, a single line should be output with the Grain and Fruit columns containing null.
  2. The output should be ordered by Code followed by the order in which the Grain/Fruit pairs were extracted from the Grains/Fruits tables.

Restrictions

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

下面是我自己的答案,欢迎补充:

;WITH cte AS
(SELECT num=DENSE_RANK()OVER(PARTITION BY name ORDER BY GRain ASC ), * FROM TC1_BIRDS AS D OUTER APPLY (SELECT grain FROM TC1_GRAINS WHERE code=d.code ) AS ST)
,cte2 AS
(SELECT num=DENSE_RANK()OVER(PARTITION BY name ORDER BY fruit ASC ), * FROM TC1_BIRDS AS D OUTER APPLY (SELECT fruit FROM TC1_FRUITS WHERE code=d.code ) AS ST)
,cte3 AS
(
SELECT num,code,NAME,fruit,grain FROM cte2 OUTER APPLY (SELECT grain FROM cte WHERE cte.num=cte2.num AND cte.CODE=CTE2.CODE ) AS ST
UNION ALL
SELECT num,code,NAME,fruit,grain FROM cte OUTER APPLY (SELECT fruit FROM cte2 WHERE cte.num=cte2.num AND CTE2.CODE=cte.CODE ) AS ST
)
SELECT DISTINCT num, code,NAME,grain,fruit FROM cte3 ORDER BY code, NAME

  

  想了半天才凑出上面的答案,欢迎看到的朋友贴出自己的答案。

TSQL Challenge 1的更多相关文章

  1. TSQL Challenge 2

    和之前发布的TSQL Challenge 1是同一系列的文章,看到那篇学习哪篇,没有固定的顺序,只为锻炼下思维. Compare rows in the same table and group th ...

  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. unity3d中的http通信 二

    转载自 http://www.cnblogs.com/88999660/archive/2013/03/11/2954279.html 如果侵权,请及时通知我删除! using System; usi ...

  2. SKYLINE

    uvalive4108:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...

  3. Protues中有源蜂鸣器BUZZER不响的解决办法(有图)

    这个是BUZZER有源蜂鸣器的protues连线图(FM是我自己的电压探针,可以删除的) 下面是我个人设置的蜂鸣器的参数(为什么很多人的这个蜂鸣器不响,是参数没有设置正确) 蜂鸣器不响的原因是 Ope ...

  4. Qt之模型/视图(自定义按钮)(使用QStyleOption的子类进行drawControl,和我用的方法完全不一样)

    http://blog.csdn.net/liang19890820/article/details/50974059

  5. bzoj2730

    首先不难想到要先求割顶,求割顶的方法白书上有讲解由于是一个矿崩塌,所以假如一个连通块连接了两个以上割顶,那么这个连通块内显然是不用设出口的连接块只连接了一个割顶,那么出口可以设在这个连通块内任意位置由 ...

  6. 【动态规划】【归并】Vijos P1412 多人背包

    题目链接: https://vijos.org/p/1412 题目大意: 求01背包的前K优解,要求必须装满(1<=K<=50 0<=V<=5000 1<=N<=2 ...

  7. C数组的相关知识

    数组的定义:具有相同数据类型的集合.在内存中开辟连续的存贮空间,从上往下,依次存储 补充:内存是以字节位单位的存储空间,内存中的每一个字节都唯一对应一个编号.这个编号就是地址.只要是存在内存中的数据都 ...

  8. iOS Block中的weakSelf/strongSelf

    Objective C 的 Block 是一个很实用的语法,特别是与GCD结合使用,可以很方便地实现并发.异步任务.但是,如果使用不当,Block 也会引起一些循环引用问题(retain cycle) ...

  9. Marzoni(玛佐尼)意大利顶级西服面料之一_HollandandSherry_新浪博客

    Marzoni(玛佐尼)意大利顶级西服面料之一_HollandandSherry_新浪博客 Marzoni(玛佐尼)意大利顶级西服面料之一 (2013-01-08 17:30:04) 转载▼

  10. tomcat中debug启动和start启动的区别

    debug启动tomcat:修改代码不加方法,不加参数,只是单纯的修改方法,不用重启tomcat(热部署). start启动tamcat:修改代码需要重启tomcat.