Commit 978f6c8a authored by 三階松隼人's avatar 三階松隼人
Browse files

SQL演習課題

parent 074653d7
--select * from T_SALE;
--select * from M_CUSTOMER;
--select * from M_EMPLOYEE;
--select * from T_SALE order by cuscode asc;
--select * from M_PRODUCT;
/*
必要な情報
・顧客リスト
・従業員リスト→顧客に紐づけ、従業員がついてない顧客を抽出
・顧客が何を買ったか
・顧客が買った商品の価格
顧客リスト
・含まれるデータ : 顧客ID(売上リストと連動), 顧客名, zip_code?, 住所, empcode→従業員ID(従業員リスト連動), enable?
 ↓
・足りないデータ : 対応する従業員情報, 何を買ったか, 商品の価格
従業員リスト
・含まれるデータ : code→従業員ID(顧客リスト連動), 従業員名, 住所, 役職?, enable?
・足りないデータ : 何を買ったか, 商品の価格
売上リスト
・含まれるデータ : 売上ID→管理用?, 顧客ID(顧客リスト連動), procode(商品リスト連動), number→購入した商品数?, 購入日
・足りないデータ : 商品の価格
手順
1.従業員名がない顧客リストをcuscode順に表記
2.各顧客の購入商品を表記, cuscodeで売上リストと左外部連結
3.購入した標品の各売上を表記, procodeで製品リストと連携, 製品リストのprice × 売上リストのnumberで各勝因の合計売上を産出
これを連結しないで、外部参照だけでいけるか?
*/
--select cuscode, empcode from M_CUSTOMER where empcode is null order by cuscode asc;
select M_CUSTOMER.cuscode, empcode, saleid, T_SALE.procode, number, M_PRODUCT.price, (number * M_PRODUCT.price) as uriage from M_CUSTOMER left join T_SALE on M_CUSTOMER.cuscode = T_SALE.cuscode inner join M_PRODUCT on T_SALE.procode = M_PRODUCT.procode where empcode is null order by M_CUSTOMER.cuscode asc;
select sum((number * M_PRODUCT.price)) as sou_uriage from M_CUSTOMER left join T_SALE on M_CUSTOMER.cuscode = T_SALE.cuscode inner join M_PRODUCT on T_SALE.procode = M_PRODUCT.procode where empcode is null;
select * from M_EMPLOYEE where enable = 'true';
\ No newline at end of file
--select * from M_PRODUCT;
select * from T_SALE order by procode asc;
/*
1.製品リストより、50,000円以下の商品に絞る
2.1の製品の平均価格を求める
3.2の平均価格以上の商品を製品リストから絞る → 1のコードをサブクエリに沿えるのでは?
4.3の各商品とT_SALEの売上数(number)の積から、それぞれの商品の売り上げを求める
5.4の売上額を合計する
*/
--select procode, proname, price from M_PRODUCT where price <= 50000;
--select avg(price) from M_PRODUCT where price <= 50000;
--select procode, proname, price from M_PRODUCT where price >= (select avg(price) from M_PRODUCT where price <= 50000);
--select M_PRODUCT.procode, M_PRODUCT.proname, M_PRODUCT.price, T_SALE.number, (M_PRODUCT.price * T_SALE.number) as uriage from M_PRODUCT left join T_SALE on M_PRODUCT.procode = T_SALE.procode where price >= (select avg(price) from M_PRODUCT where price <= 50000);
--select sum(M_PRODUCT.price * T_SALE.number) as sou_uriage from M_PRODUCT left join T_SALE on M_PRODUCT.procode = T_SALE.procode where price >= (select avg(price) from M_PRODUCT where price <= 50000);
select M_PRODUCT.price * T_SALE.number from M_PRODUCT left join T_SALE on M_PRODUCT.procode = T_SALE.procode where price >= (select avg(price) from M_PRODUCT where price <= 50000);
--select * from T_SALE order by procode asc;
--select * from M_PRODUCT order by procode asc;
/*
1. 売上リストを参照する
各商品コード(procode)の売上個数(number)の合計 = 売れた総数が求められる
2. 合計500個以上売れたデータのみに絞る
・Where句:集約前のデータに対して条件を適用するために使用
 →個々の行(レコード)の状態の時に使用して、フィルタリングする。
・Having句:集約後の条件をしてするために使用
 → Group By や Sum, Countなどの集約関数を使用した後、複数行のデータを集計した操作の後に使う
2. 商品価格を知りたい→製品リストを参照する
1のコードをサブクエリにすることで、テーブル同士を連結しなくてもデータを参照できる?
*/
--select procode, sum(number) as cnt_uriage from T_SALE group by procode order by procode; --各procodeの売り上げ数
--select procode, sum(number) as cnt_uriage from T_SALE group by procode having sum(number) >= 500 order by procode; --各procodeの売り上げ数が500を上回るデータ
select M_PRODUCT.procode, proname, price, sum(number) as cnt_uriage, (price * sum(number)) as uriage from M_PRODUCT inner join T_SALE on M_PRODUCT.procode = T_SALE.procode group by M_PRODUCT.procode, proname, price having sum(number) >= 500 order by M_PRODUCT.procode;
/*
1. 売上リストを参照する
group句とhabing→顧客ごとの売上数を求める
売り上げ数の合計が500以上のデータに絞る
2. 顧客リストをベースに考える
*/
--select * from T_SALE order by cuscode;
--select cuscode, sum(number) from T_SALE group by cuscode order by cuscode;
--select cuscode, sum(number) cnt_konyu from T_SALE group by cuscode having sum(number) >= 500 order by cuscode;
select cusname, sum(number)as cnt_konyu from M_CUSTOMER inner join T_SALE on M_CUSTOMER.cuscode = T_SALE.cuscode group by cusname having sum(number) >= 500 order by sum(number) desc;
/*
従業員リストを参照する
※1 各従業員が担当している顧客を知りたい
→顧客リストを参照する(empcodeが共通してる→連結)
※2 その顧客がどの商品を何個購入したか知りたい
※3 購入した商品がいくらかを知りたい
※4 購入した商品の合計金額を子役ごとに出す(cuscodeでくくる)
*/
--select * from M_EMPLOYEE order by empcode;
--select * from M_CUSTOMER where empcode is not null order by empcode;
--select * from T_SALE order by cuscode;
--select * from M_PRODUCT;
--select M_EMPLOYEE.empcode, empname, cuscode, cusname from M_EMPLOYEE left join M_CUSTOMER on M_EMPLOYEE.empcode = M_CUSTOMER.empcode order by M_EMPLOYEE.empcode; --※1
--select cuscode, T_SALE.procode, price, sum(number) as cnt_pro from T_SALE left join M_PRODUCT on T_SALE.procode = M_PRODUCT.procode group by T_SALE.cuscode, T_SALE.procode, M_PRODUCT.price order by cuscode asc, procode asc; --※2
--select cuscode, T_SALE.procode, price, sum(number) as cnt_pro, (price * sum(number)) as pro_uriage from T_SALE left join M_PRODUCT on T_SALE.procode = M_PRODUCT.procode group by T_SALE.cuscode, T_SALE.procode, M_PRODUCT.price order by cuscode asc, procode asc; --※3
--select cuscode, sum(price * number) as sum_uriage from T_SALE left join M_PRODUCT on T_SALE.procode = M_PRODUCT.procode group by T_SALE.cuscode order by cuscode asc; --※4
/*
select
M_EMPLOYEE.empcode,
M_EMPLOYEE.empname,
T_SALE.cuscode,
sum(M_PRODUCT.price * T_SALE.number) as sum_uriage
from
M_EMPLOYEE
left join
M_CUSTOMER on M_EMPLOYEE.empcode = M_CUSTOMER.empcode
left join
T_SALE on M_CUSTOMER.cuscode =T_SALE.cuscode
left join
M_PRODUCT on T_SALE.procode = M_PRODUCT.procode
group by
M_EMPLOYEE.empcode,
M_EMPLOYEE.empname,
T_SALE.cuscode
order by
M_EMPLOYEE.empcode;
*/
select
M_EMPLOYEE.empcode,
M_EMPLOYEE.empname,
sum(M_PRODUCT.price * T_SALE.number) as sum_uriage
from
M_EMPLOYEE
left join
M_CUSTOMER on M_EMPLOYEE.empcode = M_CUSTOMER.empcode
left join
T_SALE on M_CUSTOMER.cuscode = T_SALE.cuscode
left join
M_PRODUCT on T_SALE.procode = M_PRODUCT.procode
group by
M_EMPLOYEE.empcode,
M_EMPLOYEE.empname
order by
sum(M_PRODUCT.price * T_SALE.number) desc;
\ No newline at end of file
/*
単価を求める→製品リストのprice求めて終わり?
単価:商品1つをお店で売る時の値段=実際にやり取りする時の価格
ある商品が通常価格1,000円で販売されている場合、まとめ買いやセールで20%割引が適用されると、値引き後の単価は800円
*/
--select procode, proname, price from M_PRODUCT;
select procode, proname, price from M_PRODUCT where price = (select max(price) from M_PRODUCT);
\ No newline at end of file
select
T_SALE.*,
M_PRODUCT.proname,
M_PRODUCT.price,
case
when cost <= 2000 then 0
else cost
end as cost,
M_PRODUCT.note,
M_PRODUCT.enable
from T_SALE
full join
M_PRODUCT on T_SALE.procode = M_PRODUCT.procode;
\ No newline at end of file
select * from M_EMPLOYEE where address = 'É';
\ No newline at end of file
--select * from M_EMPLOYEE;
select * from M_EMPLOYEE where (address is null) and (note is null);
\ No newline at end of file
--select * from M_EMPLOYEE;
select * from M_EMPLOYEE where (address = 'É') or (address = '');
\ No newline at end of file
--select * from M_EMPLOYEE;
select * from M_EMPLOYEE where (address = 'É') or (address = '') or (address = 'e');
\ No newline at end of file
--select * from M_EMPLOYEE;
select * from M_EMPLOYEE where (address = '') or (address = '֓c') or (address = 'e') or (note = 'Q');
\ No newline at end of file
--select * from M_PRODUCT;
select * from M_PRODUCT where (price > 10000) and (cost < 50000);
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment