CREATE TABLE 工资表 (
员工号 char(10),
工资项目 char(10),
中文说明 char(20),
数据 decimal(16, 2),
是否计算项目 char(3));
INSERT INTO 工资表 VALUES (
'A0001',
'GZ1',
'底薪',
5700.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ2',
'平常加班小时',
10.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ3',
'假日加班小时',
16.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ4',
'加班费',
0,
'YES');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ5',
'所得税',
200.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ6',
'应发工资',
0,
'YES');

解决方案 »

  1.   

    直接用公式替换不行吗?
    select GZ1,GZ2,GZ3,GZ1/22/8*GZ2*1.5 + GZ1/22/8*GZ3*2,GZ5,GZ1+GZ4-GZ5 from [工资表]如果公式是不断变化的,那就麻烦了,考虑使用动态sql
      

  2.   

    CREATE TABLE 公式表 (
    工资项目 char(10),
    公式 char(255),
    说明 char(200));
    INSERT INTO 公式表 VALUES (
    'GZ4',
    'GZ1/22/8*GZ2*1.5 + GZ1/22/8*GZ3*2',
    '底薪/22天/8小时*平时加班小时*1.5倍 + 底薪/22天/8小时*假日加班小时*2倍');
    INSERT INTO 公式表 VALUES (
    'GZ6',
    'GZ1+GZ4-GZ5',
    '底薪 + 加班费 - 所得税');
      

  3.   

    得用游标了,并且还得先计算GZ4,再计算GZ6
      

  4.   

    CREATE TABLE 工资表 (
    员工号 char(10),
    工资项目 char(10),
    中文说明 char(20),
    数据 decimal(16, 2),
    是否计算项目 char(3));
    select 员工号, case... as 工资项目... from 工资表 where
      

  5.   

    TO: yunshiyu(陨石雨) 
        [email protected]: zxyjyzxyjy(星星) 
    是啊,我的公式是动态的.
      

  6.   

    try:
    --------------------------------------------------------------------------
    declare @s varchar(8000)
    declare @v varchar(10)
    declare @t varchar(100)select 员工号,工资项目,中文说明,数据 into temp from 工资表set @s = ''
    select @s = @s+',['+rtrim(工资项目)+'] decimal(16, 2)' 
    from 工资表 group by 工资项目set @s = 'create table ttt(EmployeeID char(10)'+@s+')'
    exec(@s)set @s = ''select @s = @s + ',case when '''+rtrim(是否计算项目)+''' = ''NO'' 
                       then sum(case when '''+rtrim(工资项目)+'''=工资项目 then 数据 else 0 end) 
                         else 0 end' 
    from 工资表 group by 工资项目,是否计算项目
    set @s = 'insert into ttt select 员工号' + @s + ' from 工资表 group by 员工号'
    exec(@s)DECLARE t_cursor CURSOR FOR SELECT 工资项目, 公式 FROM 公式表 order by 工资项目
    OPEN t_cursor
    FETCH NEXT FROM t_cursor INTO @v, @t
    WHILE @@FETCH_STATUS = 0
    BEGIN
      set @s = 'update ttt set '+@v+' = round('+@t+',0)'
      exec(@s)
      set @s = 'update a set a.数据 = b.'+@v+' from temp a,ttt b
                where a.员工号 = b.EmployeeID and a.工资项目 = '''+@v+''''
      exec(@s)
      FETCH NEXT FROM t_cursor INTO @v, @t
    END
    CLOSE t_cursor
    DEALLOCATE t_cursorselect * from temp--删除中间表
    drop table ttt
    drop table temp
      

  7.   

    利用临时表行转列,然后把公式变换成一个update语句。计算后输出结果即可。
      

  8.   

    alter  table  Gz  add  bb   varchar(50)  null
    update   gz  set  bb=fs  from  gz  inner join sf  on gz.sno=sf.sno
    while  
    (select  count(*)  from  (select * from gz  where  bb  is not null) a  inner join  gz  on  (charindex(gz.sno,a.bb)>0)  )>0
    begin
           update   gz    set  bb=replace(a2,b1,b2)  from  (select a1=sno,a2=bb from gz  where  gz.bb  is not null) a  inner join  (select  b1=sno,b2=qty  from Gz) b  on  (charindex(b1,a2)>0)   where  bb  is not null
         
    end
    select  *  from  Gz
    alter  table  Gz  drop  column  bb
      

  9.   

    --------------絕妙的算法,不管多少員工都可以處理-------Create  Table   Gz(workno  varchar(5),sno  varchar(4),memo  varchar(50),qty  Decimal(9,2),pz  varchar(5))
    insert  into  Gz
    select  'A0001','GZ1','底薪', 5700.00,'NO'     union  all
    select 'A0001','GZ2','常加班小时' ,10.00,'NO'    union  all
    select 'A0001','GZ3','假日加班小时' ,16.00,'NO'    union  all
    select 'A0001','GZ4','加班费' ,.00,'YES'   union  all
    select 'A0001','GZ5','所得税' , 200.00,'NO'    union  all
    select 'A0001','GZ6','应发工资' ,.00,'YES'
    ------------------------另一表---------------------------------------------------
    Create   Table   Sf(Sno  varchar(4),fs  varchar(50),me  varchar(100))
    insert  into sf
    select  'GZ4','GZ1/22/8*GZ2*1.5 + GZ1/22/8*GZ3*2','底薪/22天/8小时*平时加班小时*1.5倍 + 底薪/22天/8小时*假日加班小时*2倍'     union  all                                       
    select  'GZ6','GZ1+GZ4-GZ5','底薪 + 加班费 - 所得税'
    ------------------------------------------------------------------------------------
    ---------------------過程實現---------------------------------------------------
    alter  table  Gz  add  bb   varchar(50)  null    --------------結Gz  表加一個字段
    Create  Proc   kkk
    as
    begin
    update   gz  set  bb=fs  from  gz  inner join sf  on gz.sno=sf.sno
    while  
    (select  count(*)  from  (select * from gz  where  bb  is not null) a  inner join  gz  on  (charindex(gz.sno,a.bb)>0)  )>0
    begin
           update   gz    set  bb=replace(a2,b1,b2)  from  (select a1=sno,a2=bb from gz  where  gz.bb  is not null) a  inner join  (select  b1=sno,b2=qty  from Gz) b  on  (charindex(b1,a2)>0)   where  bb  is not null
         
    end
    select  *  from  Gz  where  bb  is not null
    end
    ---------------------------結束-----------------------------------
    exec   kkk   -------------執行過程
    ----------------結果--------------------------
    A0001 GZ4 加班? .00 YES 5700.00/22/8*10.00*1.5 + 5700.00/22/8*16.00*2        
    A0001 GZ6 ??工? .00 YES 5700.00/22/8*10.00*1.5 + 5700.00/22/8*16.00*2----------------------隻算到這一步把 結果表達示放在一個字段裡,隻要對這字段進行處理就可
    這個處理也可放在過程裡 ,不過時間問題 暫時沒有做完
      

  10.   

    PBVC (圆砣) 
    给你发完了,你看看吧,能不能用上