目錄sql
ABC在線銷售公司業務系統
表結構:
一、表名:t_category (商品類別表)
字段(字段名/類型/長度):
類別編號 category_id INT
類別名稱 category_name VARCHAR2(30)code
二、表名:t_goods (商品表)
字段(字段名/類型/長度):
商品編號 goods_no CHAR(3)
商品名稱 goods_name VARCHAR2(30)
商品價格 goods_price number(7,2)
所屬類別 goods_category INT
點擊次數 goods_click_num INTblog
三、 表名: 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 茅臺 2011-12-1 10 7000 10 7000
1 茅臺 2011-12-2 15 10500 25 17500it
-- 解一: -- 笛卡爾積 內聯兩張表 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;