スーパーマーケットデータベースを作成し、製品情報を照会します

create database Supermakat
on primary
(
	name='sp',
	filename='D:\sp.mdf',
	size=3MB,
	maxsize=unlimited,
	filegrowth=1MB
	
)
log on 
(
	name='sp_log',
	filename='D:\sp_log.ldf',
	size=1MB,
	maxsize=unlimited,
	filegrowth=10%
)
use Supermakat 
create table types--商品类别表
(
	tid int primary key identity(1,1),
	tname varchar(20)	
)
create table ware--商品信息表
(
	wid int primary key identity(1,1),
	wname varchar(20),
	wcarry decimal(10,2) not null,--商品进价
	wprice decimal(10,2) not null,--商品售价
	wcount int not null,--库存量
	tid int foreign key references types(tid)
)
create table emp--收银员信息表
(
	eid int primary key identity(1,1),
	ename varchar(20)not null, --收银员的姓名
	epwd varchar(20) not null--登入密码
)
create table sale--销售表
(
	sid int primary key identity(1,1),
	stime datetime not null,--销售时间
	scount int not null,--销售数量
	wid int foreign key references ware(wid),--商品信息编号
	eid int foreign key references emp(eid)--收银员的编号
)
insert into types(tname)values('自然堂')
insert into types(tname)values('八杯水')
insert into types(tname)values('复配')
insert into types(tname)values('水秘方')
insert into ware(wname,wcarry,wprice,wcount,tid)values('自然堂修复液',50,100,1000,1)
insert into ware(wname,wcarry,wprice,wcount,tid)values('自然堂补水液',60,120,1000,1)
insert into ware(wname,wcarry,wprice,wcount,tid)values('八杯水修复液',50,100,1200,2)
insert into ware(wname,wcarry,wprice,wcount,tid)values('八杯水祛痘液',80,200,1300,2)
insert into ware(wname,wcarry,wprice,wcount,tid)values('复配补水液',110,200,1000,3)
insert into ware(wname,wcarry,wprice,wcount,tid)values('复配祛痘液',50,100,1005,3)
insert into ware(wname,wcarry,wprice,wcount,tid)values('水密码补水液',50,100,1200,4)
insert into ware(wname,wcarry,wprice,wcount,tid)values('水密码修复液',50,100,1000,4)
insert into emp(ename,epwd)values('小爱',100000)
insert into emp(ename,epwd)values('小红',100001)
insert into emp(ename,epwd)values('张爱',100002)
insert into emp(ename,epwd)values('小明',100003)
insert into emp(ename,epwd)values('小米',100004)
insert into emp(ename,epwd)values('张国',100005)
insert into sale(stime,scount,wid,eid)values('2015-01-02',100,1,1)
insert into sale(stime,scount,wid,eid)values('2015-01-03',108,2,3)
insert into sale(stime,scount,wid,eid)values('2015-01-04',107,3,4)
insert into sale(stime,scount,wid,eid)values('2015-01-05',120,4,5)
insert into sale(stime,scount,wid,eid)values('2015-01-06',95,3,3)
insert into sale(stime,scount,wid,eid)values('2015-01-07',101,1,1)
insert into sale(stime,scount,wid,eid)values('2015-01-08',106,2,2)
insert into sale(stime,scount,wid,eid)values('2010-01-02',100,1,1)
insert into sale(stime,scount,wid,eid)values('2010-01-03',108,2,3)
insert into sale(stime,scount,wid,eid)values('2010-01-04',107,3,4)
insert into sale(stime,scount,wid,eid)values('2010-01-05',120,4,5)
insert into sale(stime,scount,wid,eid)values('2010-01-06',95,3,3)
insert into sale(stime,scount,wid,eid)values('2010-01-07',101,1,1)
insert into sale(stime,scount,wid,eid)values('2010-01-08',106,2,2)
select *from types
select *from ware  
select *from emp   
select *from sale
select wid,wname,wprice-wcarry 利润 from ware
select wid,wname,wprice-wcarry 利润 from ware order by 利润 desc
--查询销售量最大的商品编号
select MAX(scount)from sale
select wid from sale where scount=120
select wname from ware where wid=4 
select wname scount from ware inner join sale on(ware.wid=sale.wid )group by wname order by scount desc
--要求显示商品类别和销售量
--查询sale表中的商品编号
--显示商品类别和销售量
select tname,sum(scount) from types t,ware w,sale s where t.tid =w.tid and w.wid=s.wid group by tname  
--统计每种商品到目前的盈利总额
--总利润=(售价-进价)数量   (wprice-wcarry)*scount//单个商品利润 若要找到总利润则要利用聚合函数sum求总和
select wname,sum((wprice-wcarry)*scount) 总利润 from ware w,sale s where w.wid=s.wid  group by wname 
--收银员2号销售5件1号商品
--注意:再向sale表中增销售量 则必须现在库存量中先减去相应的数量
update ware set wcount=wcount-5  where wid=1
insert into sale(wid,eid,stime,scount)values(1,2,'2015-02-10',5)
--按照销售总数对收银员进行排序
select ename,sum(scount) 销售量 from emp e,sale s where e.eid=s.eid group by ename order by 销售量 desc 
-- sum(scount)是指 每个收银员所买出商品使用聚合函数 必须把相同商品和在一起
--查询商品销售量排在前五名的商品名称及销量
select top 5 wname,SUM(scount) 销量 from ware w,sale s where w.wid =s.wid  group by wname order by 销量 desc 
--注释:在sale 表中买的产品并不一起销售出去 所以在算销量时 要用聚合函数sum求得总的销量 利用group by 来把商品合在一起
--统计2010年的商品销售量排在前五名的商品名称及销量
select top 5 wname,SUM(scount) 销售量 from ware w,sale s where w.wid=s.wid and  stime between '2010-01-01'and '2010-12-31'group by wname order by 销售量 desc
--统计每个员工在2010年的销售量,显示员工名称和销售量
select ename,SUM(scount) 销售量 from emp e,sale s where e.eid=s.eid  and stime  between '2010-01-01'and '2010-12-31'group by ename order by 销售量 desc
--注意:where e.eid=s.eid  and stime  between '2010-01-01'and '2010-12-31'他们都是条件 在SQL语句中只允许出现一个where 所以两个条件之间要用and链接
--统计2010年的总利润
select SUM((wprice-wcarry)*scount)from ware w,sale s where  w.wid =s.wid   and stime  between '2010-01-01'and '2010-12-31'
--注释:group by 分组 若不用其则算总的利润

 

おすすめ

転載: blog.csdn.net/weixin_48135624/article/details/115211666