2017-10-10 12 views
0

手持ち金額が20未満の項目の数を倉庫別に一覧表示します。 1つ以上の項目が20未満の倉庫を表示します。表を作成 MYSQLで関数とグループをコマンドで使用する

USE premier_products; 

- - 担当者

データベース

CREATE DATABASE premier_products; 

未満でクエリに複数の項目を組み込む方法と混同

Select count(on_hand) as items 
from part 
where on_hand < 20 
group by warehouse; 

IM表

CREATE TABLE rep 
(

rep_num char(2) primary key, 

last_name varchar(15), 

first_name varchar(15), 

street varchar(15), 

city varchar(15), 

state char(2), 

zip char(5), 

commission decimal(7,2), 

rate decimal(3,2) 

); 

- Customerテーブル

CREATE TABLE customer 

(

customer_num char(3) primary key, 

customer_name varchar(30), 

street varchar(15), 

city varchar(15), 

state char(15), 

zip char(5), 

balance decimal(7,2), 

credit_limit decimal(7,2), 

rep_num char(2) 

); 

- Ordersテーブル

CREATE TABLE orders 
(

order_num char(5) primary key, 

order_date timestamp, 

customer_num char(3) 

); 

- ORDER_LINE表

CREATE TABLE order_line 

(

order_num char(5), 

part_num char(4), 

num_ordered int(5), 

quoted_price decimal(7,2), 

PRIMARY KEY (order_num, part_num) 

); 

- パートテーブル

CREATE TABLE part 

(

part_num char(4) primary key, 

description varchar(20), 

on_hand int(4), 

class char(2), 

warehouse char(2), 

price decimal (7,2) 
); 

から

INSERT INTO rep values ('20', 'Kaiser', 'Valerie', '624 Randall', 'Grove', 'FL', '33321', 20542.50, 0.05); 
INSERT INTO rep values ('35', 'Hull', 'Richard', '532 Jackson', 'Sheldon', 'FL', '33553', 39216.00, 0.07); 
INSERT INTO rep values ('65', 'Perez', 'Juan', '1626 Taylor', 'Fillmore', 'FL', '33336', 23487.00, 0.05); 

担当者テーブルを読み込む -

INSERT INTO customer values ('148', 'Al''s Appliance and Sport', '2837 Greenway', 'Fillmore', 'FL', '33336', 
6550.00, 7500.00, '20'); 
INSERT INTO customer values ('282', 'Brookings Direct', '3827 Devon', 'Grove', 'FL', '33321', 431.50, 10000.00, '35'); 
INSERT INTO customer values ('356', 'Ferguson''s', '382 Wildwood', 'Northfield', 'FL', '33146', 5785.00, 7500.00, 
'65'); 
INSERT INTO customer values ('408', 'The Everything Shop', '1828 Raven', 'Crystal', 'FL', '33503', 5285.25, 5000.00, '35'); 
INSERT INTO customer values ('462', 'Bargains Galore', '3829 Central', 'Grove', 'FL', '33321', 3412.00, 10000.00, '65'); 
INSERT INTO customer values ('524', 'Kline''s', '838 Ridgeland', 'Fillmore', 'FL', '33336', 12762.00, 15000.00, '20'); 
INSERT INTO customer values ('608', 'Johnson''s Department Store', '372 Oxford', 'Sheldon', 'FL', '33553', 2106.00, 10000.00, '65'); 
INSERT INTO customer values ('687', 'Lee''s Sport and Appliance', '282 Evergreen', 'Altonville', 'FL', '32543', 2851.00, 5000.00, '35'); 
INSERT INTO customer values ('725', 'Deerfield''s Four Seasons', '282 Columbia', 'Sheldon', 'FL', '33553', 248.00, 7500.00, '35'); 
INSERT INTO customer values ('842', 'All Season', '28 Lakeview', 'Grove', 'FL', '33321', 8221.00, 7500.00, '20'); 

顧客テーブルを読み込む -

INSERT INTO orders value ('21608', '2015-10-20', '148'); 
INSERT INTO orders value ('21610', '2015-10-20', '356'); 
INSERT INTO orders value ('21613', '2015-10-21', '408'); 
INSERT INTO orders value ('21614', '2015-10-21', '282'); 
INSERT INTO orders value ('21617', '2015-10-23', '608'); 
INSERT INTO orders value ('21619', '2015-10-23', '148'); 
INSERT INTO orders value ('21623', '2015-10-23', '608'); 

注文テーブルを読み込む - ORDER_LINEテーブル

INSERT INTO order_line value ('21608', 'AT94', 11, 21.95); 
INSERT INTO order_line value ('21610', 'DR93', 1, 495.00); 
INSERT INTO order_line value ('21610', 'DW11', 1, 399.99); 
INSERT INTO order_line value ('21613', 'KL62', 4, 329.95); 
INSERT INTO order_line value ('21614', 'KT03', 2, 595.00); 
INSERT INTO order_line value ('21617', 'BV06', 2, 794.95); 
INSERT INTO order_line value ('21617', 'CD52', 4, 150.00); 
INSERT INTO order_line value ('21619', 'DR93', 1, 495.00); 
INSERT INTO order_line value ('21623', 'KV29', 2, 1290.00); 

を移入 - ポップ後半部分は、テーブル

INSERT INTO part value ('AT94', 'Iron', 50, 'HW', '3', 24.95); 
INSERT INTO part value ('BV06', 'Home Gym', 45, 'SG', '2', 794.95); 
INSERT INTO part value ('CD52', 'Microwave Oven', 32, 'AP', '1', 165.00); 
INSERT INTO part value ('DL71', 'Cordless Drill', 21, 'HW', '3', 129.95); 
INSERT INTO part value ('DR93', 'Gas Range', 8, 'AP', '2', 495.00); 
INSERT INTO part value ('DW11', 'Washer', 12, 'AP', '3', 399.99); 
INSERT INTO part value ('FD21', 'Stand Mixer', 22, 'HW', '3', 159.95); 
INSERT INTO part value ('KL62', 'Dryer', 12, 'AP', '1', 349.95); 
INSERT INTO part value ('KT03', 'Dishwasher', 8, 'AP', '3', 595.00); 
INSERT INTO part value ('KV29', 'Treadmill', 9, 'SG', '2', 1390.00); 
+0

公開する前に質問をフォーマットしてください。不要なクエリは不要です。あなたは解決のために 'HAVING'キーワードを使う必要があると思います。 –

+0

どうすればいいですか? – Link45

+0

kですが、私が必要とするものではありません。だからこそ助けてくれませんでした。 – Link45

答えて

0

ちょうどあなたのクエリの最後に

HAVING (items > 0) 

を追加:20未満である複数のアイテムを持っている

SELECT COUNT(on_hand) AS items 
FROM part 
WHERE (on_hand < 20) 
GROUP BY warehouse 
HAVING (items > 0) 
0

表示倉庫手近に

したがって、select句にウェアハウスを含める必要があり、group by句は各ウェアハウス値に対して1つのローを形成します。

 
SELECT warehouse, COUNT(on_hand) AS num_low_stocked_parts 
FROM part 
WHERE on_hand < 20 
GROUP BY warehouse 
HAVING COUNT(on_hand) > 0 

集計値をフィルタリングすることができます。

関連する問題