如何将记录从一个表复制到同一个表

时间:2016-06-16 04:05:13

标签: sql sql-server

我的存储过程中有一个插入选择查询。在下面的查询中,我插入了一条记录。我想要做的是复制此记录并插入到同一个表中,但薪水将为负数。我不知道怎么做。帮助将不胜感激

以下是我的代码的一部分

Insert into employee_name, age, Sum(salary)
Select employee_name, age, salary from employee
group by employee_name

记录

   employee_name  age    salary
   John           39    $30,000

我想要什么

employee_name   age    salary
John            39    $30,000
John            39    $-30,000

5 个答案:

答案 0 :(得分:2)

首先,您的INSERT语法完全错误。您可以在文档中轻松查找:INSERT INTO table (columns) SELECT ...

然后你说你要复制一条记录,但要使工资为负。这与行聚合(SUMGROUP BY)有什么关系?

看起来你只是想:

insert into employee (employee_name, age, salary)
select employee_name, age, -salary from employee;

答案 1 :(得分:1)

这是insert语句的基本SQL语法。

Insert into employee (employee_name, age, salary) 
Select employee_name, age, -Sum(salary) from employee
group by employee_name, age

答案 2 :(得分:1)

create table employee
(
emp_id int,
employee_name varchar(50),
salary numeric,
Age int)

溶胶:

Insert into employee 
Select emp_id,employee_name, Sum(salary)*-1,age from employee
group by emp_id,employee_name, age

结果:

emp_id  employee_name   salary  age
 101       Rajesh        3900   32
 101       Rajesh       -3900   32

答案 3 :(得分:0)

Sum它在select语句中然后乘以-1

Insert into employee
Select employee_name, age, Sum(salary) * -1 from employee
group by employee_name, age

假设employee表有3列employee_nameagesalary

答案 4 :(得分:-2)

Insert into Employee (employee_name, age, salary)
Select employee_name, age, Sum(salary) from employee
group by employee_name, age

Insert into Employee (employee_name, age, salary)
Select employee_name, age, -Sum(salary)from employee
group by employee_name, age

这样我还没有测试过代码