SQL题目六

ABC在线销售公司业务系统

表结构:

1、表名:t_category (商品类别表)

字段(字段名/类型/长度):

类别编号 category_id INT

类别名称 category_name VARCHAR2(30)

2、表名:t_goods (商品表)

字段(字段名/类型/长度):

商品编号 goods_no CHAR(3)

商品名称 goods_name VARCHAR2(30)

商品价格 goods_price number(7,2)

所属类别 goods_category INT

点击次数 goods_click_num INT

3、 表名: t_saleinfo (销售信息表)

字段(字段名/类型/长度):

销售流水号 sid INT

商品编号 goods_no CHAR(3)

销售日期 sale_date date

销售数量 quantity INT

销售金额 amount number(10,2)

create table t_category(
category_id int primary key,
category_name varchar2(30)
); create table t_goods(
goods_no char(3) primary key,
goods_name varchar2(30) not null,
goods_price number(7,2) not null,
goods_category int not null,
goods_click_num int default 0,
constraint FK_GOODS_CATEGORY FOREIGN KEY (goods_category) references t_category(category_id)
); create table t_saleinfo(
sid int primary key,
goods_no char(3) not null,
sale_date date not null,
quantity int not null,
amount number(10,2) not null,
constraint FK_SALEINFO_GOODS FOREIGN KEY (goods_no) references t_goods(goods_no)
); -- 增加类别数据
insert into t_category values(1,'酒类');
insert into t_category values(2,'服装');
insert into t_category values(3,'书籍'); -- 商品数据
insert into t_goods values('G01','贵州茅台',550.56,1,128);
insert into t_goods values('G02','福建老酒',5.43,1,24);
insert into t_goods values('G03','泸州老窖',90.56,1,67);
insert into t_goods values('G04','剑南春',80.56,1,88);
insert into t_goods values('G05','七匹狼夹克',350.56,2,348);
insert into t_goods values('G06','七匹狼衬衫',105.43,2,908);
insert into t_goods values('G07','七匹狼男长裤',130.50,2,167);
insert into t_goods values('G08','七匹狼领带',280.00,2,388);
insert into t_goods values('G09','J2EE开发',50.50,3,236);
insert into t_goods values('G10','STRUTS应用',24.50,3,654);
insert into t_goods values('G11','ORACLE 11G',100.50,3,145);
insert into t_goods values('G12','dotnet技术',80.00,3,988); -- 销售数据
insert into t_saleinfo values(1,'G01',to_date('2008-1-1','yyyy-MM-dd'),50,50*550.56);
insert into t_saleinfo values(2,'G01',to_date('2008-1-2','yyyy-MM-dd'),25,25*550.56);
insert into t_saleinfo values(3,'G01',to_date('2008-1-3','yyyy-MM-dd'),31,31*550.56);
insert into t_saleinfo values(4,'G01',to_date('2008-1-4','yyyy-MM-dd'),43,43*550.56);
insert into t_saleinfo values(5,'G01',to_date('2008-1-5','yyyy-MM-dd'),55,55*550.56);
insert into t_saleinfo values(6,'G01',to_date('2008-1-6','yyyy-MM-dd'),102,102*550.56);
insert into t_saleinfo values(7,'G11',to_date('2008-1-6','yyyy-MM-dd'),82,82*100.5);
insert into t_saleinfo values(8,'G11',to_date('2008-1-7','yyyy-MM-dd'),202,202*100.5);

题目:

1、查询酒类商品的总点击量

2、查询各个类别所属商品的总点击量,并按降序排列

3、查询所有类别中最热门的品种(点击量最高),并按点击量降顺序排列

4、查询茅台的销售情况,按日期升序排列

格式如下:

商品编号 商品名称 销售日期 销售数量 销售金额 累计数量 累计金额

1 茅台 2011-12-1 10 7000 10 7000

1 茅台 2011-12-2 15 10500 25 17500

第一个问题思路(查询酒类商品的总点击量)

-- 解一:
-- 笛卡尔积 内联两张表
select * from t_goods a inner join t_category b
on a.goods_category=b.category_id; -- 查询酒类 笛卡尔积
select * from t_goods a inner join t_category b
on a.goods_category=b.category_id and b.category_name='酒类'; -- 按照酒名分组查询 获取酒类的总点击量
select b.category_name, sum(a.goods_click_num) total_click
from t_goods a inner join t_category b
on a.goods_category=b.category_id
and b.category_name='酒类'
group by b.category_name; -- '酒类' 替换了category_name列下的值
select '酒类' category_name, sum(a.goods_click_num) total_click
from t_goods a inner join t_category b
on a.goods_category=b.category_id and b.category_name='酒类'; -- 解二:
-- 子查询 解法
select category_id from t_category
where category_name='酒类'; select GOODS_CATEGORY,goods_click_num from t_goods
where goods_category in
(
select category_id
from t_category
where category_name='酒类'
); select '酒类' category_name,sum(goods_click_num) total_click
from t_goods
where goods_category in
(
select category_id
from t_category
where category_name='酒类'
);

第二个问题思路(查询每个类别所属商品的总点击量,并按降序排列)

-- 按所属类别分组 查询商品类别 类别商品总点击量
select goods_category,sum(goods_click_num)
from t_goods
group by goods_category; -- 降序 按商品表类别分组 各个类别商品点击量总和 降序
select goods_category,sum(goods_click_num) total_click
from t_goods
group by goods_category
order by sum(goods_click_num) desc; select goods_category,sum(goods_click_num) total_click
from t_goods a inner join t_category b
on a.goods_category = b.category_id
group by goods_category
order by sum(goods_click_num) desc; -- 划分归属
select goods_category,category_name,sum(goods_click_num) total_click
from t_goods a inner join t_category b
on a.goods_category = b.category_id
group by goods_category,category_name
order by sum(goods_click_num) desc; -- 划别名 按类别分组 按最高点击商品表中各个类别最高点击量 降序
select a.goods_category,b.category_name,sum(a.goods_click_num) total_click
from t_goods a inner join t_category b
on a.goods_category = b.category_id
group by goods_category,category_name
order by sum(a.goods_click_num) desc;

第三个问题思路(查询所有类别中最热门的品种(点击量最高),并按点击量降顺序排列 )

-- 查询商品表各个类别,最大点击量
select goods_category, max(goods_click_num) max_click
from t_goods
group by goods_category; -- 获取一张 按照商品类别分组的 新商品表
select * from
(
select goods_category, max(goods_click_num) max_click
from t_goods
group by goods_category
)a inner join t_goods b
on a.goods_category=b.goods_category
and a.max_click=b.goods_click_num; -- 笛卡尔积三表 用where筛选数据 寻找点击量高的的商品
select * from
(
select goods_category, max(goods_click_num) max_click
from t_goods
group by goods_category
)a,t_goods b,t_category c
where a.goods_category=b.goods_category
and a.max_click=b.goods_click_num
and c.category_id=a.goods_category; -- 按照 原商品表的各个类别热门最大点击量降序
select * from
(
select goods_category, max(goods_click_num) max_click
from t_goods
group by goods_category
)a,t_goods b,t_category c
where a.goods_category=b.goods_category
and a.max_click=b.goods_click_num
and c.category_id=a.goods_category
order by a.max_click desc; -- 查询商品表中点击最高(即 热门)的商品,并查询相关类别信息 按最大点击量降序
select c.category_id,c.category_name,b.goods_no,b.goods_name,a.max_click
from
(
select goods_category, max(goods_click_num) max_click
from t_goods
group by goods_category
)a,t_goods b,t_category c
where a.goods_category=b.goods_category
and a.max_click=b.goods_click_num
and c.category_id=a.goods_category
order by a.max_click desc;

第四个问题思路(查询茅台的销售情况,按日期升序排列)

-- 获取'贵州茅台'商品编号
select goods_no from t_goods where goods_name='贵州茅台'; -- 商品编号内联 限制为'贵州茅台'商品编号
select * from t_saleinfo a
inner join t_saleinfo b
on a.goods_no=b.goods_no and a.goods_no=
(
select goods_no from t_goods where goods_name='贵州茅台'
); -- 销售日期 升序
select * from t_saleinfo a
inner join t_saleinfo b
on a.goods_no=b.goods_no and a.goods_no=
(
select goods_no from t_goods where goods_name='贵州茅台'
)
order by a.sale_date; -- 增加销售日期限制
select * from t_saleinfo a
inner join t_saleinfo b
on a.goods_no=b.goods_no and a.goods_no=
(
select goods_no from t_goods where goods_name='贵州茅台'
)
and a.sale_date>=b.sale_date
order by a.sale_date; -- 增加商品编号限制
select a.sale_date,a.quantity,sum(b.quantity),sum(b.amount)
from t_saleinfo a inner join t_saleinfo b
on a.goods_no=b.goods_no
and a.goods_no=
(
select goods_no from t_goods where goods_name='贵州茅台'
)
and a.sale_date>=b.sale_date
group by a.sale_date,a.quantity; -- 销售日期 升序
select a.sale_date,a.quantity,sum(b.quantity),sum(b.amount)
from t_saleinfo a inner join t_saleinfo b
on a.goods_no=b.goods_no
and a.goods_no=
(
select goods_no from t_goods where goods_name='贵州茅台'
)
and a.sale_date>=b.sale_date
group by a.sale_date,a.quantity
order by a.sale_date; select a.sale_date,a.quantity day_quantity,max(a.amount) day_amount,sum(b.quantity) total_quantity,sum(b.amount) total_amount
from t_saleinfo a inner join t_saleinfo b
on a.goods_no=b.goods_no
and a.goods_no=
(
select goods_no from t_goods where goods_name='贵州茅台'
)
and a.sale_date>=b.sale_date
group by a.sale_date,a.quantity
order by a.sale_date; -- 获取商品销售信息
select d.goods_no,d.goods_name,e.category_name,c.*
from
(
select max(a.goods_no) goods_no,
a.sale_date,a.quantity day_quantity,
max(a.amount) day_amount,
sum(b.quantity) total_quantity,
sum(b.amount) total_amount
from t_saleinfo a inner join t_saleinfo b
on a.goods_no=b.goods_no and a.goods_no=
(
select goods_no from t_goods where goods_name='贵州茅台'
)
and a.sale_date>=b.sale_date
group by a.sale_date,a.quantity
order by a.sale_date
)c,t_goods d, t_category e
where c.goods_no=d.goods_no
and d.goods_category=e.category_id;

笔记

视图

-- 创建视图
create view v_day_sale_statis as
select d.goods_no,d.goods_name,e.category_name,c.*
from
(
select max(a.goods_no) goods_no,a.sale_date,a.quantity day_quantity,max(a.amount) day_amount,sum(b.quantity) total_quantity,sum(b.amount) total_amount
from t_saleinfo a inner join t_saleinfo b
on a.goods_no=b.goods_no
and a.goods_no=
(
select goods_no from t_goods where goods_name='贵州茅台'
)
and a.sale_date>=b.sale_date
group by a.sale_date,a.quantity
order by a.sale_date
)c,t_goods d, t_category e
where c.goods_no=d.goods_no
and d.goods_category=e.category_id; -- 查询用户视图
select * from user_views; -- 查询视图
select * from v_day_sale_statis; -- 删除视图
drop view v_day_sale_statis; -- 查找某个视图
select * from dba_views where view_name = "User_tables"; -- 创建视图注释
comment on column v_day_sale_statis.table_name is 'Name of the table';

索引

-- 创建索引(加快查找速度)
create index clicknum_idx on t_goods(goods_click_num); -- 查找用户索引
select * from user_indexes;

Oracle语法 及 SQL题目(三)的更多相关文章

  1. Oracle语法 及 SQL题目(一)

    目录 课例复制 SQL题目一 SQL题目二 SQL题目三 笔记 课例复制 OCM 全称:Oracle Certified Master 认证大师 含义:Oracle 原厂推出的数据库方向最高级别认证 ...

  2. Oracle语法 及 SQL题目(二)

    目录 课例复制 思考题四 解题思路 思考题五 解题思路 课例复制 思考题四 最近,经过你的努力,你得到了一份工作,成为了百货公司的一位经理. 到位后,你发现你的销售数据库中有两张表,一个是商店促销时间 ...

  3. Oracle中PL/SQL简介、基本语法以及数据类型

    Oracle中PL/SQL简介.基本语法以及数据类型 一.PL/SQL简介. Oracle PL/SQL语言(Procedural Language/SQL)是结合了结构化查询和Oracle自身过程控 ...

  4. 【SQL】Oracle的PL/SQL语法及其拓展数据类型总结

    PL/SQL语法 PL/SQL程序由三部分组成,声明部分.执行部分.异常处理部分. 模板: DECLARE /*变量声明*/ BEGIN /*程序主体*/ EXCEPTION /*异常处理部分*/ E ...

  5. ansi sql 语法 切换为 oracle 语法

        语句粘贴到 工作表 打开查询构建器 勾选 创建oracle连接 over     sql dev 的语法设置调整,否则表别名会右对齐   下面是 转换后的结果,是不是看得舒服多了

  6. Oracle DBA常用SQL

    监控SQL 1.监控事例的等待: select event,sum(decode(wait_time,0,0,1)) prev, sum(decode(wait_time,0,1,0)) curr,c ...

  7. oracle中动态SQL详解

    部分内容参考网上资料 1.静态SQLSQL与动态SQL Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数的编译情 ...

  8. Oracle Database 11g : SQL 基础

    简介 1:课程目标 2:课程 目标 3:Oracle Database 11g 以及相关产品概览 1:Oracle Database 11g :重点领域 2:Oracle Fusion Middlew ...

  9. oracle中动态SQL使用详细介绍

    Oracle编译PL/SQL程序块分为两个种:通常静态SQL采用前一种编译方式,而动态SQL采用后一种编译方式,需要了解的朋友可以参考下     1.静态SQLSQL与动态SQL Oracle编译PL ...

随机推荐

  1. Qt ListWidget item 发起拖放

    第一步:重写类 MyListWidget 继承自 QListWidget 第二步:重写 mousePressEvent 函数 和 mouseMoveEvent 函数 void mousePressEv ...

  2. iview Carousel 轮播图自适应宽高;iview 轮播图 图片重叠问题;iview tabs 高度互相影响问题;vue this问题;

    最终效果图: 一.轮播图中图片自适应宽高:  <Carousel loop v-bind:height="imgHeight+'px'" v-model="caro ...

  3. Python学习日记(十九) 模块导入

    模块导入 当文件夹中有这样一个自定义的command模块 在它的内部写下下列代码: print('这个py文件被调用!') def fuc(): print('这个函数被调用!') 然后我们在comm ...

  4. 【spark】spark应用(分布式估算圆周率+基于Spark MLlib的贷款风险预测)

    注:本章不涉及spark和scala原理的探讨,详情见其他随笔 一.分布式估算圆周率 计算原理:假设正方形的面积S等于x²,而正方形的内切圆的面积C等于Pi×(x/2)²,因此圆面积与正方形面积之比C ...

  5. 【转】国内CPU现状

    首页 博客 学院 下载 图文课 论坛 APP CSDN                            CSDN学院                            问答 商城 VIP会员 ...

  6. docker到底比LXC多了些什么

    看似docker主要的OS级虚拟化操作是借助LXC, AUFS只是锦上添花.那么肯定会有人好奇docker到底比LXC多了些什么.无意中发现 stackoverflow 上正好有人问这个问题, 回答者 ...

  7. Flink源码阅读(二)——checkpoint源码分析

    前言 在Flink原理——容错机制一文中,已对checkpoint的机制有了较为基础的介绍,本文着重从源码方面去分析checkpoint的过程.当然本文只是分析做checkpoint的调度过程,只是尽 ...

  8. js对样式的操作

    本文有:对某个事件的来回操作实现对css样式的来回修改 .比如实现hover效果 <!DOCTYPE html> <html> <head> <meta ch ...

  9. PS批量截取图片

    本文提供的是需要截取大量图片的方法,仅供参考. 打开ps,在菜单栏找到窗口,点击窗口里面的动作 窗口右上方 或者下方会出现一个小窗口 点击小窗口右下角删除图标旁边的图标新建动作,工作名称:截取图片 . ...

  10. LeetCode - 82、删除排序链表中的重复元素 II

    给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5    输出: 1 ...