SQL插入带条件的Select语句

时间:2017-02-09 03:42:28

标签: sql-server tsql sql-insert

我有一张这样的表:

enter image description here

我想使用Insert into Select语句插入detail_salary_table。 我可以使用此代码

插入salary_component项“Salary”
INSERT INTO detail_salary_table (date_work, id_emp, salary_component, nominal)
    SELECT  
        date_work, id_emp, 'Salary',
        IIF(DATEDIFF(minute, start_work, finish_work) > 480, 10000, round(convert(float(53), datediff(minute, start_work, finish_work)) / 480, 1) * 10000) 
    FROM 
        attendance_table

如何使用T-SQL插入salary_component项“OverTime”,如图像?

如果我使用VB.NET,我可以使用if和loop语句。

注意:

  • 480正在修复。 10.000正在修复。
  • overtime = finish_work - start_work - 480.从overtime_rate_table取名义,其中值接近time_in_minutes
  • 不测量overtime_rate_table中名义的增量。 (所以,我不能使用* 1000)(例子是测量的)

用于创建表和示例数据的SQL代码:

create table employee_table 
(
    id_emp int primary key,
    name_emp varchar(200)
);
GO

create table attendance_table 
(
    id_data int primary key identity(1,1),
    date_work date,
    id_emp int,
    start_work datetime,
    finish_work datetime
);
GO

create table overtime_rate_table  
(
    id_data int,
    time_in_minutes int,
    nominal money
);
GO

create table detail_salary_table 
(
    id_data int primary key identity(1,1),
    date_work date,
    id_emp int,
    salary_component varchar(100),
    nominal money
);
GO

insert into employee_table 
values (1, 'Emp A'), (2, 'Emp B'), (3, 'Emp C'), (4, 'Emp D'), (5, 'Emp E');
GO

insert into attendance_table (date_work, id_emp, start_work, finish_work) 
values 
     ('2017-02-01',1,'2017-02-01 08:00','2017-02-01 16:52'),
     ('2017-02-01',2,'2017-02-01 07:45','2017-02-01 16:48'),
     ('2017-02-01',3,'2017-02-01 08:02','2017-02-01 12:05'),
     ('2017-02-01',4,'2017-02-01 07:56','2017-02-01 16:49'),
     ('2017-02-01',5,'2017-02-01 07:30','2017-02-01 18:05'),
     ('2017-02-02',1,'2017-02-02 07:52','2017-02-02 16:23'),
     ('2017-02-02',2,'2017-02-02 07:19','2017-02-02 18:56'),
     ('2017-02-02',3,'2017-02-02 07:55','2017-02-02 18:23'),
     ('2017-02-02',4,'2017-02-02 08:01','2017-02-02 16:01'),
     ('2017-02-02',5,'2017-02-02 07:31','2017-02-02 16:49'),
     ('2017-02-03',1,'2017-02-03 07:52','2017-02-03 17:44'),
     ('2017-02-03',2,'2017-02-03 07:41','2017-02-03 17:23'),
     ('2017-02-03',3,'2017-02-03 07:06','2017-02-03 17:56'),
     ('2017-02-03',4,'2017-02-03 07:56','2017-02-03 19:00'),
     ('2017-02-03',5,'2017-02-03 07:45','2017-02-03 18:56');
GO

insert into overtime_rate_table 
values (1, 15, 1000), (2, 30, 2000), (3, 45, 3000),
       (4, 60, 4000), (5, 75, 5000), (6, 90, 6000),
       (7, 105, 7000), (8, 120, 8000), (9, 135, 9000),
       (10, 150, 10000), (11, 165, 11000), (12, 180, 12000),
       (13, 195, 13000), (14, 210, 14000), (15, 225, 15000);
GO

1 个答案:

答案 0 :(得分:0)

INSERT INTO detail_salary_table
(date_work,
 id_emp,
 salary_component,
 nominal
)
       SELECT date_work,
              id_emp,
              'OverTime',
              ISNULL(o.Nominal, 0)
       FROM attendance_table a
            LEFT JOIN overtime_rate_table o ON CONVERT( INT, DATEDIFF(minute, a.start_work, a.finish_work) - 480) / 15 * 15 = o.time_in_minutes;