在老外网站发布的一些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. WAMP集成环境

    WAMP Windows下的Apache+Mysql/MariaDB+Perl/PHP/Python,一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有 ...

  2. 牢记负载均衡与HA,高性能是不同的方案。一般的CLUSTER只能实现其中的一种,而ORACLE的RAC可以有两种。

    F5/LVS<—Haproxy<—Squid/Varnish<—AppServer. 现在网站发展的趋势对网络负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术: 第一 ...

  3. hashCode() 和equals() 区别和作用

    HashSet和HashMap一直都是JDK中最常用的两个类,HashSet要求不能存储相同的对象,HashMap要求不能存储相同的键. 那么Java运行时环境是如何判断HashSet中相同对象.Ha ...

  4. stat~~~访问文件状态的利器

    Name stat, fstat, lstat - get file status Synopsis #include <sys/types.h>#include <sys/stat ...

  5. bzoj2096

    本来也不打算写这道题的解题报告的,因为比较水直接维护两个单调队列(最大值,最小值)随便弄弄就行了但是我开始疯狂不知道为什么的RE,然后实在没办法找root要了数据测了之后……王苍,根本就没有错啊……我 ...

  6. 平衡树(Splay):Splaytree POJ 3580 SuperMemo

    SuperMemo         Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...

  7. Computer Vision Algorithm Implementations

    Participate in Reproducible Research General Image Processing OpenCV (C/C++ code, BSD lic) Image man ...

  8. 移动UI自动化-Page Objects Pattern

    移动UI自动化,看起来美好,践行起来却难.做个目光短见的务实主义者.Page Objects Pattern是Selenium官方推崇的方式,最近研究写测试用例最佳实践之Page Objects,同时 ...

  9. [C++关键字] 内置类型

    bool, char, short, char16_t (C++11), int, char32_t (C++11), float, long, double,在64位机器上测试各种类型的大小,代码如 ...

  10. Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理

    题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...