日期:2014-05-16 浏览次数:20668 次
create table ProWage(ID int,PName varchar(10),Wage int)
insert ProWage
select 1,'青鸟',2000 union all
select 2,'张三',1300 union all
select 3,'李四',1900 union all
select 4,'二月',3600 union all
select 5,'蓝天',2880
--1
while (select SUM(case when Wage >=3000 then 1 else 0 end)*1.0/COUNT(*)from ProWage)<=0.5
begin
update ProWage set Wage=Wage+100
end
select * from ProWage
--2
select * from ProWage where Wage>=(select SUM(Wage)/COUNT(*)from ProWage)
USE [master]
GO
/****** Object: StoredProcedure [dbo].[IncreaseWage] Script Date: 04/15/2014 13:48:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[IncreaseWage]
AS
BEGIN
declare @total int
declare @temp int
declare @total_wage int
declare @avg_wage int
select @total=COUNT(1) from [master].[dbo].[ProWage]
select @temp=COUNT(1) from [master].[dbo].[ProWage] where Wage>=3000
set @total_wage=0
while (@temp*100/@total)<50
begin
UPDATE [master].[dbo].[ProWage]
SET [Wage] = Wage+100
set @total_wage = @total_wage + @total*100
select @temp=COUNT(1) from [master].[dbo].[ProWage] where Wage>=3000
end
-- 总共加薪
select @total_wage
--加薪后的结果
select * from [master].[dbo].[ProWage]
select @avg_wage = SUM(wage)/@total from [master].[dbo].[ProWage]
--大于平均薪资的员工
select * from [master].[dbo].[ProWage] where Wage> @avg_wage
END