資料庫實驗四
阿新 • • 發佈:2018-12-10
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