Query and other operations on the order database

Query and other operations on the order database

For table structure:

1. Agent (agent number, name, address, zip code, commission amount, commission ratio)

The agent number is the primary key

Field name type of data Remarks
Agent ID char(4) primary key
Name nvarchar(10)
address nvarchar(20)
Postal code char(6)
Commission amount smallmoney
Commission percentage tinyint

2. Customers (customer number, name, address, zip code, income and expenditure difference, loan limit, agent number)

The customer number is the primary key

Field name type of data Remarks
Customer Number char(4) primary key
Name nvarchar(10)
address nvarchar(20)
Postal code char(6)
Balance of payments smallmoney
Loan limit smallmoney
Agent ID char(4) foreign key

3. Products (product number, description information, inventory, category, warehouse number, price)

The serial number of the product is the primary key

Field name type of data Remarks
Product ID char(4) primary key
Description nvarchar(20)
inventory int
category tinyint
Warehouse ID char(4)
price smallmoney

4. Order (order number, order date, customer number)

Field name type of data Remarks
Order number char(4) primary key
Order date Smalldatetime
Customer Number char(4) foreign key

5. Order items (order number, product number, order quantity, order unit price)

Field name type of data Remarks
Order number char(4) primary key , foreign key
Product ID char(4) primary key, foreign key
Order quantity smallint
Order unit price smallmoney

Query the total inventory of products with warehouse number 1002, display the warehouse number and total inventory

select 仓库编号,sum(库存量) as 总库存量 from 产品
group by 仓库编号
having 仓库编号='1002'

Query the agent with the highest commission amount, showing the agent number, name, address, and commission amount

select 代理商编号,姓名,地址,提成金额 from 代理商
where 提成金额 in (select max(提成金额) from 代理商)

Query the name and address of the agent passed by the customer with the number 300

 select a.姓名,a.地址 from 客户 b inner join 代理商 a
 on a.代理商编号 = b.代理商编号
 where b.客户编号 = '300'

Query the customer number and name of the product number 0033, the order quantity is greater than 100

Note: The order quantity mentioned here is to count the number of products with a total order number of 0033. Because, the same customer may have ordered 0033 products in multiple different orders.

select a.客户编号,a.姓名 from 客户 a inner join 订单 b
on a.客户编号 = b.客户编号
inner join 订货项目 c 
on b.订单编号 = c.订单编号
group by a.客户编号,a.姓名,c.产品编号
having sum(c.订购数量)>100 and c.产品编号='0033'

Query each customer's order, display customer number, customer name, order number and order date

select a.客户编号,a.姓名,b.订单编号,b.订货日期 from 客户 a
inner join 订单 b on a.客户编号 = b.客户编号

Query all order items after 2000 (not including 2000), display and only display order number, product number, order quantity, order unit price

select a.订单编号,a.产品编号,a.订购数量,a.订购单价 from 订货项目 a
inner join 订单 b on a.订单编号 = b.订单编号
where  (DATEPART(yy,b.订货日期))>2000 

Query the customer name corresponding to the agent whose commission amount is more than 100,000 (including 100,000)

select a.姓名 from 客户 a inner join 代理商 b
on a.代理商编号 = b.代理商编号
where b.提成金额>=100000

Create a view containing the order number, the customer number and agent number of the order, called the order information view

create view 订单信息视图 as 
select b.订单编号,a.客户编号,a.代理商编号 from 客户 a
inner join 订单 b on a.客户编号 = b.客户编号

From the order information view, query the product information with order number 111 (including order number, customer number, agent number)

select 订单编号,客户编号,代理商编号 from 订单信息视图
where 订单编号 = '111'

建立一个含有产品编号、产品库存量及产品在订量三个字段的视图,名为产品信息视图

注:① 产品在定量指产品已经累计被订购的数量

② group by 子句可以将查询结果按某一列或多列的值进行分组,但出现在select后面的字段 要么是聚合函数中的,要么是在group by 中的。

create view 产品信息视图 as
select a.产品编号,a.库存量,sum(b.订购数量) as 产品在定量 from 产品 a
inner join 订货项目 b on a.产品编号 = b.产品编号
group by a.产品编号,a.库存量

从产品信息视图中,查询产品在定量大于6100的产品,显示产品编号及产品在定量

select 产品编号,产品在定量 from 产品信息视图
where 产品在定量>6100

建立一个含有客户编号、客户所订的产品编号、该产品的订购单价及该产品的描述四个字段的视图,名为客户订购产品视图

create view 客户订购产品视图 as
select a.客户编号,b.产品编号,b.订购单价,c.描述信息 from 订单 a
inner join 订货项目 b on a.订单编号 = b.订单编号
inner join 产品 c on b.产品编号 = c.产品编号

从客户订购产品视图中,查询所订购的产品的订购单价<=1000的客户编号

select 客户编号 from  客户订购产品视图
where 订购单价 <=1000
group by 客户编号

Guess you like

Origin www.cnblogs.com/lightice/p/12727933.html