标签:相加 数据 语法 技术 values anti 令行 书籍 笛卡尔
老规矩,先创建用户le 密码abc(在连接的虚拟机oracle下HR数据库中可以找到 le)
create user le identified by abc;
授予用户le链接 资源使用 权限
grant connect,resource to le;
使用用户le 密码abc 通过tns服务连接mydog变量中的服务器
conn le/abc@mydog
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)
题目:
1、查询酒类商品的总点击量
2、查询各个类别所属商品的总点击量,并按降序排列
3、查询所有类别中最热门的品种(点击量最高),并按点击量降顺序排列
4、查询茅台的销售情况,按日期升序排列
创建语句
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);
第一个问题思路(查询酒类商品的总点击量)
//笛卡尔积 内联两张表
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 替代了 group by b.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;
标签:相加 数据 语法 技术 values anti 令行 书籍 笛卡尔
原文地址:https://www.cnblogs.com/mumuyinxin/p/10852748.html