我正在使用oracle编写一个查询来获取每年的每个类型的电影号码,但我遇到两个问题: 如果我想添加一个名为2015年之前的新专栏,该专栏将每种类型的2015年之前制作的电影数量相加,我该怎么办?我尝试过解码,但它似乎只能识别像2015这样的实际单词,而不是表达<2015 在下面的类型中,有许多类型的经典科幻小说,如果我想将所有经典类型组合在一起,而不是经典,经典戏剧,经典浪漫,经典战争。我怎样才能做到这一点?

SQL> select decode(genre,null,'Year Total',genre) genre, 2 sum(decode (year, 2015, 1, 0)) Year2015, 3 sum(decode (year, 2016, 1, 0)) Year2016, 4 sum(decode (year, 2017, 1, 0)) Year2017 5 from film 6 group by rollup(genre); GENRE YEAR2015 YEAR2016 YEAR2017 ------------------------- ---------- ---------- ---------- Action and Adventure 1 1 0 Children and Family 0 1 1 Classic 0 0 0 Classic, Drama 0 0 0 Classic, Romance 0 0 0 Classic, War 0 0 0 Comedy 0 0 0 Documentary 0 0 0 Fantasy 1 0 0 Sci-Fi 0 2 1 TV Drama 2 0 1 TV Sci-Fi 0 0 0 Thriller 0 0 0 War 0 0 0 Year Total 4 4 3

这里编辑的是样本数据:

INSERT INTO film VALUES ('11W', 'Wide Screen', 'Gone with the Wind', 1939, 2, null, 233, 'Romance');

INSERT INTO film VALUES ('12F', 'Full Screen', 'The Longest Day', 1962, 1, null, 178, 'War');

INSERT INTO film VALUES ('13F', 'Full Screen', 'Roman Holiday', 1953, 1, 'NR', 118, 'Classic');

INSERT INTO film VALUES ('14F', 'Full Screen', 'Sabrina', 1954, 1,'NR', 113, 'Classic');

INSERT INTO film VALUES ('15W', 'Wide Screen', 'Breakfast at Tiffany', 1961, 1, 'NR', 115, 'Classic');

INSERT INTO film VALUES ('16W', 'Wide Screen', 'My Fair Lady', 1964, 1, 'G', 172, NULL);

INSERT INTO film VALUES ('17F', 'Full Screen', 'The Heart is a Lonely Hunter', 1968, 1, null, 123, 'Drama');

INSERT INTO film VALUES ('23W', 'Full Screen', 'Titanic: The Complete Story', 1994, 2,'NR', 300, 'Documentary');

INSERT INTO film VALUES ('24F', 'Full Screen', 'Rush Hour', 1998, 1, 'PG-13', 98, 'Comedy');

INSERT INTO film VALUES ('28W', 'Wide Screen', 'Pearl Harbor', 2001, 1, 'PG-13', 183, 'War');

INSERT INTO film VALUES ('31W', 'Wide Screen', 'Avatar', 2010, 3,'PG-13', 162,'Sci-Fi');

INSERT INTO film VALUES ('38W', 'Wide Screen', 'Star Wars: The Force Awakens', 2016, 3,'PG-13', 136,'Sci-Fi');

INSERT INTO film VALUES ('40W', 'Wide Screen', 'Harry Potter and the Deathly Hollows: Part I', 2011, 1, 'PG-13', 146, 'Fantasy');

INSERT INTO film VALUES ('41W', 'Multiple Formats', 'Transformers: Age of Extinction', 2014, 1,'PG-13', 164,'Action and Adventure');

INSERT INTO film VALUES ('42F', 'Wide Screen', 'Horrible Bosses', 2011, 1, 'R', 98, 'Comedy');

INSERT INTO film VALUES ('43W', 'Wide Screen', 'Rogue One: A Star Wars Story', 2017, 3,'PG-13', 133,'Sci-Fi');

INSERT INTO film VALUES ('44W', 'Wide Screen', 'The Prince of Persia: The Sands of Time', 2010, 1,'PG-13', 116, 'Action and Adventure');

INSERT INTO film VALUES ('45W', 'Wide Screen', 'Clash of the Titans', 2010, 1,'PG-13', 106, 'Action and Adventure');

INSERT INTO film VALUES ('46W', 'Wide Screen', 'Skyfall', 2012, 1, 'PG-13', 143, 'Action and Adventure');

INSERT INTO film VALUES ('47W', 'Wide Screen', 'Hugo', 2012, 1,'PG', 126,'Children and Family');

INSERT INTO film VALUES ('48W', 'Wide Screen', 'Brave', 2012, 1, 'PG', 93, 'Children and Family');

INSERT INTO film VALUES ('49W', 'Wide Screen', 'Zootopia', 2016, 1,'PG', 108,'Children and Family');

INSERT INTO film VALUES ('50W', 'Wide Screen', 'Star Trek', 2010, 1, 'PG-13', 127, 'Sci-Fi');

INSERT INTO film VALUES ('60W', 'Wide Screen', 'Harry Potter and the Deathly Hollows: Part II', 2012, 1, 'PG-13', 130, 'Fantasy');

INSERT INTO film VALUES ('61W', 'Multiple Formats', 'Guardians of the Galaxy', 2014, 1, 'PG-13', 121, 'Sci-Fi');

INSERT INTO film VALUES ('62W', 'Wide Screen', 'Alphas: Season 1', 2012, 3,'NR', 497,'TV Sci-Fi');

INSERT INTO film VALUES ('63W', 'Wide Screen', 'The Dark Knight Rises', 2012, 1, 'PG-13', 165, 'Action and Adventure');

INSERT INTO film VALUES ('64W', 'Wide Screen', 'The Avengers', 2012, 1, 'PG-13', 143, 'Action and Adventure');

INSERT INTO film VALUES ('65W', 'Wide Screen', 'Wrath of the Titans', 2012, 1, 'PG-13', 99, 'Action and Adventure');

INSERT INTO film VALUES ('66W', 'Multiple Formats', 'Scorpion: Season 1', 2015, 6,'NR', 916,'TV Drama');

INSERT INTO film VALUES ('67W', 'Wide Screen', 'Spectre', 2016, 1, 'PG-13', 150, 'Action and Adventure');

INSERT INTO film VALUES ('68W', 'Wide Screen', 'Alphas: Season 2', 2013, 3,'NR', 562,'TV Sci-Fi');

INSERT INTO film VALUES ('71W', 'Wide Screen', 'The Croods', 2013, 1, 'PG', 96, 'Children and Family');

INSERT INTO film VALUES ('72W', 'Wide Screen', 'Moana', 2017, 1,'PG', 107,'Children and Family');

INSERT INTO film VALUES ('73W', 'Wide Screen', 'A Good Day to Die Hard', 2013, 1, 'R', 97, 'Thriller');

INSERT INTO film VALUES ('74W', 'Wide Screen', 'Star Trek: Into Darkness', 2013, 1, 'PG-13', 132, 'Sci-Fi');

INSERT INTO film VALUES ('80W','Wide Screen', 'The Hunger Game: Catching Fire', 2014, 1,'PG-13', 146,'Sci-Fi');

INSERT INTO film VALUES ('81W','Wide Screen', 'The Hobbit: The Desolation of Smaug', 2014, 2,'PG-13', 161,'Fantasy');

INSERT INTO film VALUES ('82W','Wide Screen', 'The Hobbit: The Battle of the Five Armies', 2015, 2,'PG-13', 144,'Fantasy');

INSERT INTO film VALUES ('83W','Wide Screen', 'Lucy', 2015, 2,'R', 90,'Action and Adventure');

INSERT INTO film VALUES ('84M','Multiple Formats', 'Star Trek Beyond',2016,2,'PG-13',122,'Sci-Fi');

1

投票

您可以使用sign()(代表数学中的signum)函数来派生您想要的列:

sum(decode (sign(year - 2015), -1 , 1, 0)) as Before2015

你可以使用这个查询

select decode(substr(genre,1,7),'Classic','Classic',genre),

sum(decode (sign(year - 2015), -1 , 1, 0)) as Before2015,

sum(decode (year, 2015, 1, 0)) Year2015,

sum(decode (year, 2016, 1, 0)) Year2016,

sum(decode (year, 2017, 1, 0)) Year2017

from film

group by rollup(decode(substr(genre,1,7),'Classic','Classic',genre))

结合所有Classic类型电影。

计数oracle,SQL数据透视表子组计数相关推荐

  1. excel透视表计数去重_Excel数据透视表中的唯一计数

    excel透视表计数去重 In a previous article,  Roger Govier showed us a couple of different ways to get a Uniq ...

  2. python数据透视表计数去除重复_excel透视表计数去重_Excel数据透视表中的唯一计数...

    In a previous article,  Roger Govier showed us a couple of different ways to get a Unique count with ...

  3. wps透视表列总计移到顶部_数据透视表总计中的错误

    wps透视表列总计移到顶部 Pivot tables are a quick and easy way to summarize a table full of data, without fancy ...

  4. SQL Server中的报表–如何使用数据透视表和日期计算来获取有价值的报表

    介绍 (Introduction) A few months back I had been working on an interesting proof of concept for a huma ...

  5. 学习SQL:SQL Server数据透视表

    In the previous few articles in this series, we've set the foundations on how to create a report. We ...

  6. 利用 Sql 实现数据透视表功能

    讲过很多次,数据分组和数据透视表很像,Sql 中的数据分组大家应该都很熟悉了,用的就是 group by.数据透视表是作为一个数据分析师最基本,也是使用频率最高的一个功能了,我们可以把明细数据从数据库 ...

  7. SQL Server中数据透视表的Python脚本

    This article talks about Python scripts for creating pivot tables in multiple ways. 本文讨论了以多种方式创建数据透视 ...

  8. sql同时修改两个表的数据_如何用SQL做数据透视表?

    - 点击上方"中国统计网"订阅我吧!- 讲过很多次,数据分组和数据透视表很像,Sql 中的数据分组大家应该都很熟悉了,用的就是 group by. 数据透视表是作为一个数据分析师最 ...

  9. 实战 SQL:销售数据的小计/合计/总计以及数据透视表

    文章目录 小计.合计与总计 多维度交叉统计 自定义统计维度 数据透视表 总结 学习过 SQL 的人都知道,使用聚合函数(AVG.SUM.COUNT.MIN/MAX 等)和分组操作(GROUP BY)可 ...

  10. Microsoft SQL Server 实现数据透视表

    文章目录 使用 CASE 表达式和分组聚合 使用 PIVOT 运算符 创建动态透视表 总结 大家好,我是只谈技术不剪发的 Tony 老师.数据透视表(Pivot Table)是 Excel 中一个非常 ...

最新文章

  1. Cache总义Cache用法之页面声明
  2. 首例利用智能路由网关犯罪嫌疑人被捕:罪名流量劫持
  3. 企业网站推广方案详解
  4. wxWidgets:wxCriticalSectionLocker类用法
  5. 强生CEO加入苹果成为新董事
  6. 华为正加大全球招聘 包括芯片工程师、软件开发员和AI研究员
  7. safari 浏览器提示添加到主屏幕_Safari浏览器的几个小技巧你掌握了吗?
  8. Jenkins实现自动打包Android(安卓)程序
  9. H3C 无线控制器WX2510H-F与无线接入点WA5320-C-EI的安装与关联详细指导书
  10. 串口助手使用16进制发送数据
  11. 堪萨斯州立大学计算机科学,堪萨斯州立大学相当于国内哪所大学?
  12. IOS流媒体播放器V1.1_rtsp、rmtp、m3u8、mms、mmsh
  13. 【Excel】正态分布函数 NORM.DIST / 标准差STDEV.S、STDEV.P
  14. NAOqi.Net(C#)MotionProxy类的一些函数用法(一)
  15. nn.Sequential()
  16. 【量化课程】01_投资与量化投资
  17. 【一篇入魂】redis快速入门—部署、数据类型、持久化、事务、集群
  18. vsCode安装less插件并配置
  19. 【python打包】利用pyinstaller打包详细教程,一把过
  20. ESI ProCAST v2019.0.Suite.Win64 Linux641DVD

热门文章

  1. HyperLogLog算法及原理
  2. Eviction Kill POD选择分析
  3. 高级编程技术,第三周
  4. 2017年终总结——恍恍惚惚又一年
  5. Ubuntu18.04 安装运行windows程序(qq 微信 caj阅读器等)+解决wine中文乱码
  6. python实现马科维茨模型的资本市场线_基于Python的马科维茨投资组合理论的实证研究...
  7. 什么是java full gc_关于Java垃圾回收,你必须要知道FullGC是什么
  8. 京东咚咚架构演讲读后感
  9. Shell脚本-Dong
  10. 什么是JSTL和EL表达式