create index 预算日期索引 on 作业项目表(预算日期);
create index 结算日期索引 on 作业项目表(结算日期);
create index 入账日期索引 on 作业项目表(入账日期);
--(1)
select *
from 作业项目表
where 预算日期 between '2016/5/1' and '2016/5/28' and 预算单位 =
(select 单位代码
from 单位代码表
where 单位名称 = '采油一矿二队');
--another solution
--select *
--from 作业项目表,单位代码表
--where 单位代码 = 预算单位 and 单位名称 = '采油一矿二队'and 预算日期 between '2016/5/1' and '2016/5/28';
--(2)
select *
from 作业项目表
where 结算日期 between '2016/5/1' and '2016/5/28' and 预算单位 =
(select 单位代码
from 单位代码表
where 单位名称 = '采油一矿二队');
--another solution
--select *
--from 作业项目表,单位代码表
--where 单位名称='采油一矿二队' and 预算单位=单位代码 and 结算日期>='2016-5-1'and 结算日期<='2016-5-28';
--(3)-----------------------------------------------
select *
from 材料费表
where 单据号 =
(select 单据号
from 作业项目表
where 预算日期 between '2016/5/1' and '2016/5/28' and 预算单位 =
(select 单位代码
from 单位代码表
where 单位名称 = '采油一矿二队'));
--(4)------------------------------------------
select *
from 作业项目表
where 入账日期 between '2016/5/1' and '2016/5/28' and 预算单位 =
(select 单位代码
from 单位代码表
where 单位名称 = '采油一矿二队');
--(5)-----------------------------------------
select sum(预算金额) 预算金额总和
from 作业项目表
where 预算日期 between '2016/5/1' and '2016/5/28' and 预算单位 =
(select 单位代码
from 单位代码表
where 单位名称 = '采油一矿二队');
--(6)--------------------------------
select sum(结算金额) 结算金额总和
from 作业项目表
where 结算日期 between '2016/5/1' and '2016/5/28' and 预算单位 =
(select 单位代码
from 单位代码表
where 单位名称 = '采油一矿二队');
--(7)-------------------------------
select sum(入账金额) 入账金额总和
from 作业项目表
where 入账日期 between '2016/5/1' and '2016/5/28' and 预算单位 =
(select 单位代码
from 单位代码表
where 单位名称 = '采油一矿二队');
--(8)-------------------------------
select sum(入账金额) 采油一矿入账金额总和
from 作业项目表,单位代码表
where 入账日期 between '2016/5/1' and '2016/5/28' and 预算单位 =单位代码 and 单位名称 like '采油一矿%';
--another solution
--select sum(入账金额) 入账金额总和
--from 作业项目表
--where 入账日期 between '2016/5/1' and '2016/5/28' and 预算单位 in
--(select 单位代码
--from 单位代码表
--where 单位名称 like '采油一矿%');
--(9)-------------------------------
select distinct 入账人
from 作业项目表
where 入账人 is not null;
--(10)--------------------------------
select 单据号
from 作业项目表
where 结算日期 between '2016/5/1' and '2016/5/28' and 结算金额 is not null and 入账金额 is null;
--(11)----------------------------------
select 单据号
from 作业项目表
where 预算单位 =
(select 单位代码
from 单位代码表
where 单位名称 = '采油一矿二队')
order by 入账金额 desc;
--(12)--------------------
select 施工单位表.施工单位名称,count(结算金额) 项目个数,sum(结算金额) 结算金额总和
from 施工单位表 left join 作业项目表 on (施工单位表.施工单位名称=作业项目表.施工单位)
group by 施工单位表.施工单位名称;
--(13)--------------------------------------------
select *
from 材料费表
where 单据号 = (select 单据号
from 材料费表
where 物码 = (select 物码
from 物码表
where 名称规格 = '材料三')
and 消耗数量*单价>2000);
--(14)-------------------------------------
select distinct 单据号
from 作业项目表
where 施工单位 = '作业公司作业二队';
--(15)--------------------------------
select distinct 单据号
from 作业项目表
where 施工单位 = '作业公司作业一队'
union
select distinct 单据号
from 作业项目表
where 施工单位 = '作业公司作业二队';
--(16)---------------------------
select distinct 施工单位
from 作业项目表,油水井表,单位代码表
where 预算单位 = 单位代码表.单位代码 and 单位代码表.单位代码 = 油水井表.单位代码 and 单位名称 like '采油一矿%';
--another solution
--select distinct 施工单位
--from 作业项目表
--where 井号 in( select 井号
-- from 油水井表
-- where 单位代码 in ( select 单位代码
-- from 单位代码表
-- where 单位名称 like '采油一矿%'));
-----------------------------------------------------------------------------------
drop index 预算日期索引 on 作业项目表;
drop index 结算日期索引 on 作业项目表;
drop index 入账日期索引 on 作业项目表;
--3---------------------------------------------------
--(1)------------------------------------------
create table 结算金额表(
施工单位 char(20),
年月 varchar(7),
结算金额 money
);
--(2)-----------------------------------------
insert
into 结算金额表
select 施工单位,convert(varchar(7),结算日期,111),sum(结算金额)
from 作业项目表
group by 施工单位,convert(varchar(7),结算日期,111);
--another solution
--insert
--into 结算金额表
--select 施工单位,YEAR(结算日期)*100+MONTH(结算日期),sum(结算金额)
--from 作业项目表
--group by 施工单位,YEAR(结算日期)*100+MONTH(结算日期);
--(3)---------------------------------------------------
begin tran
update 作业项目表
set 结算人 = '李兵'
where 预算单位 in (select 单位代码
from 单位代码表
where 单位名称 like '采油一矿%'
);
--(4)-----------------------------------------------------------------------------------------------
delete
from 作业项目表
where 预算单位 in (select 单位代码
from 单位代码表
where 单位名称 like '采油一矿%'
);
rollback
数据库实验四
猜你喜欢
转载自blog.csdn.net/qq_38717135/article/details/80048466
今日推荐
周排行