公司SQL考覈及小結(Oracle)

一.數據庫初始化腳本:

Create TABLE HAND_CUSTOMERS
(
 CUSTOMERS_NO Varchar2(10),
 CUSTOMERS_NAME Varchar2(30),
 CUSTOMERS_GENDER Varchar2(3),
 CUSTOMERS_BIRTH_DATE Date
);
comment on TABLE HAND_CUSTOMERS is '顧客表';
comment on COLUMN HAND_CUSTOMERS.CUSTOMERS_NO is '客戶編號';
comment on COLUMN HAND_CUSTOMERS.CUSTOMERS_NAME is '客戶名稱';
comment on COLUMN HAND_CUSTOMERS.CUSTOMERS_GENDER is '客戶性別';
comment on COLUMN HAND_CUSTOMERS.CUSTOMERS_BIRTH_DATE is '客戶出生日期';

 
Create TABLE HAND_SELLERS
(
 SELLER_NO Varchar2(10),
 SELLER_NAME Varchar2(30),
 MANAGER_NO  Varchar2(10)
);
comment on TABLE HAND_SELLERS is '銷售員表';
comment on COLUMN HAND_SELLERS.SELLER_NO is '銷售員編碼';
comment on COLUMN HAND_SELLERS.SELLER_NAME is '銷售員名稱';
comment on COLUMN HAND_SELLERS.MANAGER_NO  is '銷售員經理'; 


---
 
Create Table HAND_GOODS
(
 GOODS_NO Varchar2(10),
 GOODS_NAME Varchar2(30),
 GOODS_PRICE Number
);
comment on table HAND_GOODS is '商品表';
comment on column HAND_GOODS.GOODS_NO is '商品編碼';
comment on column HAND_GOODS.GOODS_NAME is '商品名稱';
comment on column HAND_GOODS.GOODS_PRICE is '商品單價';

----
Create Table HAND_SALES_RECORDS
(
 CUSTOMERS_NO Varchar2(10),
 SELLER_NO Varchar2(10),
 GOODS_NO Varchar2(10),
 SALES_QUANTY Number,
 SALES_DATE Date
);
comment on table HAND_SALES_RECORDS is '銷售記錄表';
comment on column HAND_SALES_RECORDS.CUSTOMERS_NO is '客戶編號';
comment on column HAND_SALES_RECORDS.SELLER_NO is '銷售員編碼';
comment on column HAND_SALES_RECORDS.GOODS_NO is '商品編碼';
comment on column HAND_SALES_RECORDS.SALES_QUANTY is '銷售數量';
comment on column HAND_SALES_RECORDS.SALES_DATE is '銷售記錄';
  




------
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C001', '張三', '', TO_DATE('1990/1/1','YYYY/MM/DD'));
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C002', '李四', '', TO_DATE('1994/3/2','YYYY-MM-DD'));
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C003', '吳鵬', '', TO_DATE('1996/2/19','YYYY-MM-DD'));
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C004', '琴沁', '', TO_DATE('1997/1/4','YYYY-MM-DD'));
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C005', '王麗', '', TO_DATE('1998/1/5','YYYY-MM-DD'));
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C006', '李波', '', TO_DATE('1998/4/6','YYYY-MM-DD'));
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C007', '劉玉', '', TO_DATE('1998/7/7','YYYY-MM-DD'));
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C008', '蕭蓉', '', TO_DATE('1998/8/21','YYYY-MM-DD'));
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C009', '陳蕭曉', '', TO_DATE('1994/12/1','YYYY-MM-DD'));
INSERT INTO HAND_CUSTOMERS(CUSTOMERS_NO, CUSTOMERS_NAME, CUSTOMERS_GENDER, CUSTOMERS_BIRTH_DATE) values 
('C010', '陳美', '', TO_DATE('1999/10/10','YYYY-MM-DD'));

------
INSERT INTO HAND_SELLERS(SELLER_NO, SELLER_NAME, MANAGER_NO ) values 
('X001', '銷售A', '');
INSERT INTO HAND_SELLERS(SELLER_NO, SELLER_NAME, MANAGER_NO ) values 
('X002', '銷售B', 'X001');
INSERT INTO HAND_SELLERS(SELLER_NO, SELLER_NAME, MANAGER_NO ) values 
('X003', '銷售C', 'X001');
INSERT INTO HAND_SELLERS(SELLER_NO, SELLER_NAME, MANAGER_NO ) values 
('X004', '銷售D', 'X003');
INSERT INTO HAND_SELLERS(SELLER_NO, SELLER_NAME, MANAGER_NO ) values 
('X005', '銷售E', 'X003');
-------
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS001', '商品A', 120);
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS002', '商品B', 159);
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS003', '商品C', 349);
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS004', '商品D', 256);
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS005', '商品E', 412);
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS006', '商品F', 342);
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS007', '商品G', 234);
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS008', '商品H', 776);
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS009', '商品I', 123);
INSERT INTO HAND_GOODS(GOODS_NO, GOODS_NAME, GOODS_PRICE) values 
('GOODS010', '商品J', null);
---
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C001', 'X001', 'GOODS001', 32, TO_DATE('2019/6/8','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C002', 'X002', 'GOODS001', 39, TO_DATE('2019/6/18','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C002', 'X003', 'GOODS003', 20, TO_DATE('2019/6/19','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C004', 'X004', 'GOODS004', 4, TO_DATE('2019/6/11','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C005', 'X005', 'GOODS005', 60, TO_DATE('2019/6/12','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C006', 'X003', 'GOODS006', 30, TO_DATE('2019/6/13','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C006', 'X002', 'GOODS007', 36, TO_DATE('2019/6/14','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C004', 'X001', 'GOODS006', 40, TO_DATE('2019/6/15','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C001', 'X003', 'GOODS001', 10, TO_DATE('2019/6/19','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C002', 'X002', 'GOODS002', 41, TO_DATE('2019/4/11','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C003', 'X003', 'GOODS003', 30, TO_DATE('2019/3/12','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C003', 'X003', 'GOODS004', 60, TO_DATE('2019/6/11','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C003', 'X002', 'GOODS005', 76, TO_DATE('2019/6/19','YYYY-MM-DD'));
INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C003', 'X001', 'GOODS006', 68, TO_DATE('2019/6/25','YYYY-MM-DD'));

INSERT INTO HAND_SALES_RECORDS(CUSTOMERS_NO, SELLER_NO, GOODS_NO, SALES_QUANTY, SALES_DATE) values 
('C004', 'X005', 'GOODS005', 32, TO_DATE('2018/6/18','YYYY-MM-DD'));

COMMIT;
View Code

 

二.Sql語句題:

  1.條件:查詢沒有賣出過「商品B」的銷售人員信息 顯示:銷售員編碼,銷售員名稱,管理員編碼,管理員名稱 :

--1
SELECT hs.seller_no
,hs.seller_name
,hsm.seller_no
,hsm.seller_name
FROM hand_sellers hs
,hand_sellers hsm
WHERE hs.manager_no = hsm.seller_no(+)
AND NOT EXISTS (SELECT 1
FROM hand_sales_records hsr
,hand_goods hg
WHERE hsr.goods_no = hg.goods_no
AND hg.goods_name = '商品B'
AND hsr.seller_no = hs.seller_no);
--2
SELECT hs.seller_no
,hs.seller_name
,hsm.seller_no
,hsm.seller_name
FROM hand_sellers hs
,hand_sellers hsm
WHERE hs.manager_no = hsm.seller_no(+)
AND hs.seller_no NOT IN (SELECT hsr.seller_no
FROM hand_sales_records hsr
,hand_goods hg
WHERE hsr.goods_no = hg.goods_no
AND hg.goods_name = '商品B');
--考點:外鏈接、NOT EXISTS 和 NOT IN 的用法及區別     

    小結:

      1.in和exists:

        in是把外表和內表做hash鏈接(會用到外表上的索引),而exists是對外表做loop循環(用到內表上的索引),每次loop循環再對內表進行查詢,若是查詢的兩個表大小至關,那麼用in和exists差異不大;若是兩個表中一個較小一個較大,則子查詢表大的用exists,子查詢表小的用in;正則表達式

      2.not in和not exists:

        not in 邏輯上不徹底等同於not exists。使用not in時,若是子查詢中返回的任意一條記錄含有空值,則查詢將不返回任何記錄。若是子查詢字段有非空限制,這時可使用not in,而且能夠經過提示讓它用hasg_aj或merge_aj鏈接。若是查詢語句使用了not in,那麼對內外表都進行全表掃描,沒有用到索引;而not exists的子查詢依然能用到表上的索引。因此不管哪一個表大,用not exists都比not in 要快數據庫

      參照:https://blog.csdn.net/baidu_37107022/article/details/77278381ide

  2.條件:查詢6 月中旬,全部顧客的消費狀況。顯示:顧客編號、顧客名稱、顧客性別、銷售商品、銷售時間、銷售數量排序:消費數量從高到低。說明:若是沒有消費記錄,銷售商品、銷售時間、銷售數量留空

  

--1
SELECT hc.customers_no
,hc.customers_name
,hc.customers_gender
,hg.goods_name
,hsr.sales_date
,hsr.sales_quanty
FROM hand_sales_records hsr
,hand_goods hg
,hand_customers hc
WHERE hsr.goods_no = hg.goods_no(+)
AND hsr.customers_no(+) = hc.customers_no
--AND hsr.sales_date(+) BETWEEN to_date('2019-06-10', 'yyyy-mm-dd') AND
to_date('2019-06-20', 'yyyy-mm-dd')
AND to_char(hsr.sales_date(+), 'MM') = '06'
AND to_char(hsr.sales_date(+), 'DD') BETWEEN 10 AND 20
ORDER BY hsr.sales_quanty DESC;
--2
SELECT hc.customers_no
,hc.customers_name
,hc.customers_gender
,hg.goods_name
,hsr.sales_date
,hsr.sales_quanty
FROM hand_sales_records hsr
,hand_goods hg
,hand_customers hc
WHERE hsr.goods_no = hg.goods_no
AND hsr.customers_no = hc.customers_no
AND hsr.sales_date BETWEEN to_date('2019-06-10', 'yyyy-mm-dd') AND
to_date('2019-06-20', 'yyyy-mm-dd')
ORDER BY hsr.sales_quanty DESC;
--考點:外鏈接,日期函數

    小結:

        1.查兩表關聯列相等的數據用內鏈接。
      2.Col_L是Col_R的子集時用右外鏈接。
      3.Col_R是Col_L的子集時用左外鏈接。
      4.Col_R和Col_L彼此有交集但彼此互不爲子集時候用全外。
      5.求差操做的時候用聯合查詢。函數

  3.條件:查詢購買記錄3條以上的顧客信息。 顯示:顧客編號、顧客名稱、顧客出生日期 :

--1
SELECT hc.customers_no
,hc.customers_name
,hc.customers_birth_date
FROM hand_customers hc
WHERE hc.customers_no IN (SELECT hsr.customers_no
FROM hand_sales_records hsr
GROUP BY hsr.customers_no HAVING COUNT(1) > 3);
--2
SELECT hc.customers_no
,hc.customers_name
,hc.customers_birth_date
FROM hand_customers hc
WHERE EXISTS (SELECT 1
FROM hand_sales_records hsr
WHERE hc.customers_no = hsr.customers_no
GROUP BY hsr.customers_no
HAVING COUNT(1) > 3);

 

    小結:  

      where是篩選行,having是篩選已經查詢出來的字段。oop

  4.條件:查詢顧客中姓氏爲"張""李""劉"的顧客信息顯示:顧客編號、顧客名稱、顧客,出生日期。排序:顧客出生日期從近到遠。說明:正則表達式。除正則表達式之外的其餘方式:

  

--正則
SELECT hc.customers_no
,hc.customers_name
,hc.customers_birth_date
FROM hand_customers hc
WHERE regexp_like(hc.customers_name, '^張.*|^李.*|^劉.*');
--Like
SELECT hc.customers_no
,hc.customers_name
,hc.customers_birth_date
FROM hand_customers hc
WHERE (hc.customers_name LIKE '張%' OR hc.customers_name LIKE '李%' OR
hc.customers_name LIKE '劉%');

     小結:性能

      還可使用substring()等函數提取第一個字符看是否在要查找的條件裏面ui

  5.條件:查詢商品信息,當商品價格在100-199 時 打9 折,在200-299 時打8 折,在300 以上打7 折。顯示:商品編碼、商品名稱、商品打折前單價、商品打折後單價:

SELECT hg.goods_no
,hg.goods_name
,hg.goods_price
,CASE
WHEN hg.goods_price BETWEEN 100 AND 199 THEN
hg.goods_price * 0.9
WHEN hg.goods_price BETWEEN 200 AND 299 THEN
hg.goods_price * 0.8
WHEN hg.goods_price >= 300 THEN
hg.goods_price * 0.7
ELSE
hg.goods_price
END changed_goods_price
FROM hand_goods hg

   6.查詢生日在當前月份的顧客信息。顯示:顧客名稱,顧客生日,顧客購買總數量。排序:顧客生日:

SELECT hc.customers_no
,hc.customers_name
,hc.customers_birth_date
,(select sum(hsr.sales_quanty) from hand_sales_records hsr where
hsr.customers_no = hc.customers_no )
FROM hand_customers hc
WHERE to_char(hc.customers_birth_date,'MM') =to_char(sysdate,'MM') ;
--考點: 日期函數,子查詢

     小結:編碼

        就考察了子查詢,以及to_char函數轉換爲字符串的函數,注意對應的to_date將字符串轉換爲日期spa

  7.條件:查詢商品單價在100 到199 之間的商品信息。顯示:商品名稱、單價。說明:單價爲空的商品也進行顯示:

--1
SELECT hg.goods_no
,hg.goods_name
,hg.goods_price
FROM hand_goods hg
where nvl( hg.goods_price,100) BETWEEN 100 AND 199;
--2
SELECT hg.goods_no
,hg.goods_name
,hg.goods_price
FROM hand_goods hg
WHERE hg.goods_price BETWEEN 100 AND 199
OR hg.goods_price IS NULL

     小結:.net

      就考察了between..and的用法(包括前面,不包括後面),以及nvl()函數,注意nvl(expr1,expr2)和nvl2(expr1,expr2,expr3)的區別

  8.條件:查詢全部銷售員的銷售總數量。顯示:銷售員編碼、銷售員名稱、銷售數量、銷售名次。排序:按照銷售數量進行倒序。說明:若是有銷售數量相同的,銷售名次以下輸出。銷售彙總數量60,60,57 名次 1,1,3:

SELECT hs.seller_no
,hs.seller_name
,SUM(sr.sales_quanty) sales_quanty
,RANK() OVER(ORDER BY SUM(sr.sales_quanty) DESC) rank_level
FROM hand_sales_records sr
,hand_sellers hs
WHERE sr.seller_no(+) = hs.seller_no
GROUP BY hs.seller_no
,hs.seller_name

 

    小結:

      注意開窗函數【分析函數提供一系列比較高級的SQL功能。分析函數時創建在數據窗口(over在必定的數據庫範 圍進行數據分析),在必定的數據範圍進行排序、彙總】over(partition by...order by...)其中partition by表示以什麼分組,若是沒有則使用group by的分組,rank()【用於返回結果集的分區內每行的排名,行的排名是相關行以前的排名數加一,若是排序的標準相同,則排名也相同】和dense_rank()【與rank函數相似,dense_rank函數在生成序號時是連續的,而rank函數生成的序號有可能不連續】,row_number()【爲查詢出來的每一行記錄生成一個序號,依次排序且不會重複】。還值得注意的是MySQL應該是在8.0以後纔有開窗函數(查詢資料),可使用自鏈接比較數量排序。

  9.條件:查詢銷售數量高於商品「商品A」的最大銷售數量的全部銷售記錄。顯示: 商品名稱、銷售員名稱、顧客名稱、銷售數量、銷售日期。 with 不計入(with 和子查詢算一種方法):

--sn1
SELECT g.goods_name
,hs.seller_name
,c.customers_name
,sr.sales_quanty
,sr.sales_date
FROM hand_goods g
,hand_sales_records sr
,hand_sellers hs
,hand_customers c
WHERE 1 = 1
AND sr.seller_no = hs.seller_no(+)
AND sr.goods_no = g.goods_no(+)
AND sr.customers_no = c.customers_no
AND sr.sales_quanty > (SELECT MAX(sr1.sales_quanty)
FROM hand_sales_records sr1
,hand_goods g1
WHERE sr1.goods_no = g1.goods_no
AND g1.goods_name = '商品A');
--sn2
SELECT g.goods_name
,hs.seller_name
,c.customers_name
,sr.sales_quanty
,sr.sales_date
FROM hand_goods g
,hand_sales_records sr
,hand_sellers hs
,hand_customers c
WHERE 1 = 1
AND sr.seller_no = hs.seller_no(+)
AND sr.goods_no = g.goods_no(+)
AND sr.customers_no = c.customers_no
AND sr.sales_quanty > ALL (SELECT sr1.sales_quanty
FROM hand_sales_records sr1
,hand_goods g1
WHERE sr1.goods_no = g1.goods_no
AND g1.goods_name = '商品A');
--sn3
SELECT g.goods_name
,hs.seller_name
,c.customers_name
,sr.sales_quanty
,sr.sales_date
FROM hand_goods g
,hand_sales_records sr
,hand_sellers hs
,hand_customers c
WHERE 1 = 1
AND sr.seller_no = hs.seller_no(+)
AND sr.goods_no = g.goods_no(+)
AND sr.customers_no = c.customers_no
AND EXISTS (SELECT 1
FROM hand_sales_records sr1
,hand_goods g1
WHERE sr1.goods_no = g1.goods_no
AND g1.goods_name = '商品A'
AND sr.sales_quanty > sr1.sales_quanty);

    小結:

      考覈外鏈接語法,Oracle使用+能夠簡化外鏈接SQL(left/right join),主要是找準關係,須要返回哪邊的全部行,而後就是ALL(注意all和any的區別,如題all是大於查詢出的全部,即比結果中的最大的還大,而any只要是其中任意一個就行),EXISTS(exists和in的區別,exists是遍歷外表在查詢,而in1是遍歷查詢出的內表結果)的用法。

  10.條件:分別根據 商品+銷售員+顧客、商品+銷售員 、銷售員、總銷售數量的維度倆統計銷售數量。顯示:商品、銷售員、顧客、彙總銷售數量:

SELECT g.goods_name
,hs.seller_name
,c.customers_name
,SUM(sr.sales_quanty)
FROM hand_goods g
,hand_sales_records sr
,hand_sellers hs
,hand_customers c
WHERE 1 = 1
AND sr.seller_no = hs.seller_no(+)
AND sr.goods_no = g.goods_no(+)
AND sr.customers_no = c.customers_no
GROUP BY ROLLUP(g.goods_name, hs.seller_name, c.customers_name)

    小結:

      注ROLLUP和CUBE的區別。使用ROLLUP產生常規分組彙總行 以及分組小計,Rollup 後面跟了n個字段,就將進行n+1次分組,從右到左每次減小一個字段進行分組;而後進行 union 【n+1次分組】。CUBE是後面跟了n個字段,就將進行2的N次方的分組運算,而後進行。MySQL中使用with rollup/cube。

  11.條件:查詢商品銷售數量總數量爲前三的商品。顯示:商品編號,商品名稱,銷售數量。排序:按照銷售數量進行排序。說明:若是商品銷售數量的前4 名爲 100,80,67,67 則4 條記錄都顯示:

SELECT g.goods_no
,g.goods_name
,t.total_quanty
FROM hand_goods g
,(SELECT sr.goods_no
,SUM(sr.sales_quanty) total_quanty
,rank() over(ORDER BY SUM(sr.sales_quanty) DESC)
rank_level
FROM hand_sales_records sr
WHERE 1 = 1
GROUP BY sr.goods_no) t
WHERE t.goods_no = g.goods_no
AND t.rank_level <= 3
ORDER BY t.total_quanty DESC

 

    小結:

      考察開窗函數以及rank()的使用(如題若是使用的分組的條件total_quanty,若是這個值相同,那麼排名也相同)。

  12.a.建立一個表HAND_GOODS_XXXXX 包含HAND_GOODS 表裏金額大於200 的商品記錄。b.將HAND_GOODS_XXXXX 的單價更新爲原價的80%。c.將 HAND_GOODS 表裏的記錄合併至HAND_GOODS_XXXXX,經過商品編碼進行匹配,若是表HAND_GOODS_XXXXX 中不存在記錄,進行新建,若是存在記錄則更新HAND_GOODS_XXXXX 的商品單價爲HAND_GOODS 中的商品單價:

--a.
CREATE TABLE HAND_GOODS_25305 AS SELECT * FROM hand_goods g WHERE
g.goods_price > 200
--b.
UPDATE HAND_GOODS_25305 SET goods_price = goods_price * 0.8
--c.
MERGE INTO hand_goods_25305 g_new
USING hand_goods g
ON (g.goods_no = g_new.goods_no)
WHEN MATCHED THEN
UPDATE
SET g_new.goods_price = g.goods_price
WHEN NOT MATCHED THEN
INSERT
VALUES
(g.goods_no
,g.goods_name
,g.goods_price);

 

    小結:

      考察建立,更新表,以及批量插入。

  13.條件:查詢購買商品數量總計最多的顧客信息。顯示:顧客編號,顧客名稱,購買商品數量: 

WITH tab_sum AS
(SELECT sr.customers_no
,SUM(sr.sales_quanty) total_quanty
FROM hand_sales_records sr
WHERE 1 = 1
GROUP BY sr.customers_no)
SELECT t.customers_no, c.customers_name, t.total_quanty
FROM tab_sum t, hand_customers c
WHERE c.customers_no = t.customers_no AND t.total_quanty = (
SELECT MAX(total_quanty)
FROM tab_sum)

 

    小結:

      使用子查詢問,及with方便在後面屢次使用則能夠簡化SQL並適當提升性能 。

相關文章
相關標籤/搜索