不知道什么时候用Subquery

先上题:
For each instrument, show its type, maker, the owner’s name, the corresponding orchestra name, and the number of concerts (name this column as concert_number) in which the owner played from 2013 to 2016. Take into consideration only instruments produced in 2013 or earlier.

SELECT
instruments.type,
instruments.maker,
members.name,
orchestras.name,
(SELECT COUNT (concerts.orchestra_id)
FROM concerts
WHERE concerts.year BETWEEN 2013 AND 2016 AND concerts.orchestra_id=orchestras.id) AS concert_number

FROM instruments
JOIN members
ON members.id = owner_id
JOIN orchestras
ON orchestras.id = members.orchestra_id
WHERE instruments.production_year<=2013;

为什么不用group by 来计算 number of concerts ?

we have a subquery in the SELECT clause, and we are counting number of concerts in the subquery itself. The way this subquery is running is, it is adding one value for each row(returned by the main query) and that value is same as the count of concerts performed between year 2013 and 2016 by that respective orchestra, that’s why you have c.orchestra_id=o.id.

例题:
For each orchestra, show a report consisting of:

Name of the orchestra as the name column.
Country/Countries in which the orchestra played as the country column.
Number of concerts in this country as the concerts_no column.
Average rating of the concerts played by this orchestra in this country as the avg_rating column.
Show only the rows of the report for which the orchestra played more than once in a given country.

select o.name, cc.country, cc.concerts_no, cc.avg_rating
from orchestras o
join
(select c.orchestra_id, c.country,
count(c.id) as concerts_no, avg(rating) as avg_rating
from concerts c group by c.country, c.orchestra_id
having count(c.id)>1) as cc on o.id=cc.orchestra_id
group by o.name, cc.country, cc.concerts_no, cc.avg_rating

用两个group by嵌套

Subquery 进阶相关推荐

  1. 进阶SQL技巧:subquery, string function, window function

    数据类型 某些聚合函数可以作用于所有数据类型,例如COUNT:而某些聚合函数只能作用于特点数据类型,例如SUM只能作用于数值类型.某些数据看起来是数值,但是很可能是以VARCHAR形式存储的.这就涉及 ...

  2. 翻译(六)——T-SQL的进阶之路:超过基础的2级水平:写子查询

    T-SQL的进阶之路:超过基础的2级水平:写子查询 格雷戈里·拉森(Gregory Larsen),2016/01/01(第一次出版:2014/01/29) 该系列 这篇文章是楼梯系列的一部分:通往T ...

  3. MySQL进阶篇(02):索引体系划分,B-Tree结构说明

    本文源码:GitHub·点这里 || GitEE·点这里 一.索引简介 1.基本概念 首先要明确索引是什么:索引是一种数据结构,数据结构是计算机存储.组织数据的方式,是指相互之间存在一种或多种特定关系 ...

  4. 史上最简单MySQL教程详解(进阶篇)之索引及失效场合总结

    史上最简单MySQL教程详解(进阶篇)之索引及其失效场合总结 什么是索引及其作用 索引的种类 各存储引擎对于索引的支持 简单介绍索引的实现 索引的设置与分析 普通索引 唯一索引(Unique Inde ...

  5. DEV01-GBase 8a MPP Cluster SQL 编码进阶篇

    GBase 8a MPP Cluster SQL 编码进阶篇 一.概述: 二.常用内置函数 (一)函数体系 (二)内置函数的基本概念: (三)内置函数的使用位置: (四)内置函数概览 (五)数学函数 ...

  6. MySQL学习笔记-从基础到进阶

    MySQL自学笔记 MySQL 基础 SQL SQL通用语法 SQL分类 DDL数据操作 数据类型 DML数据操作 DQL数据操作 DCL数据控制 函数 约束 多表查询 事务 进阶 存储引擎 **索引 ...

  7. 【面试题 - mysql】进阶篇 - 索引

    进阶篇 索引 1 索引概述 2 优点.缺点 3 索引结构(B+,Hash) 3.1 二叉树 3.2 红黑树 3.3 B树 3.4 B+树 3.5 hash 4 面试题:为什么 InnoDB 存储引擎选 ...

  8. 黑马SQL入门到精通笔记 —— 进阶篇

    进阶篇 进阶篇 1 存储引擎 1.1 MySQL体系结构: 1.2 InnoDB 1.3 MyISAM 1.4 Memory 1.5 存储引擎特点 1.6 存储引擎的选择 2 索引 2.1 索引结构 ...

  9. 黑马MySQL进阶篇笔记

    目录 MySQL进阶篇 一.存储引擎 1.1 Mysql体系结构图 1.2 存储引擎 1.概念 2.InnoDB 3.MyISAM 4.Memory 5.三种引擎对比 6.引擎的选择 1.3 安装my ...

最新文章

  1. java仿聊天室项目总结_Java团队课程设计-socket聊天室(个人总结)
  2. Spring Cloud实战小贴士:健康检查
  3. rabbitmq 持久化_RabbitMQ原理与相关操作(三)消息持久化
  4. Java限流之 —— Guawa
  5. 深度融合 | 当推荐系统遇见知识图谱(三)
  6. 虚拟主机如何创建svn服务器,虚拟主机搭建svn
  7. 两张图片合成一张_利用溶图技巧把多张图片合成一张奇幻风格图片教程
  8. 软件测试总结(十一)
  9. CFree注册码及破解过程【转】
  10. Java+SSM+Jsp+Mysql项目大学生健康管理系统
  11. 情人节程序员用HTML网页表白【生日祝福】 HTML5生日祝福网页源码 HTML+CSS+JavaScript
  12. google退出中国市场是战略性错误
  13. STM32中挂载SDRAM内存说明
  14. 团队的英文翻译缩写_有感情的一起游戏的团队英文缩写
  15. BBS 与 BLog(博客)的区别到底是什么?[转载]
  16. 线性表 (一) 线性表定义与线性表插入与删除
  17. 微信怎么制作小程序?制作微信小程序流程
  18. 输入一个整数n及一个n阶方阵,判断该方阵是否以主对角线对称,输出“Yes”或“No”。
  19. 西行漫记(6):Diversity
  20. 纯css实现优惠券效果

热门文章

  1. Unrecognized SSL message, plaintext connection?
  2. Qt|解析NTP报文时间戳,并计算时差和延迟,利用时差同步时间
  3. 全球与中国喷雾干燥机械市场深度研究分析报告
  4. Java 日志从入门到实战
  5. DDD专栏12、专栏总结 走出自己的DDD
  6. Linux环境编译运行C语言程序
  7. python中for循环流程图_Python编程中的for循环语句学习教程
  8. Python的数据驱动
  9. 自定义和自动计算的技术指标和覆盖AnyStock
  10. bootstrap图标的使用