我的存储过程中有一个插入选择查询。在下面的查询中,我插入了一条记录。我想要做的是复制此记录并插入到同一个表中,但薪水将为负数。我不知道怎么做。帮助将不胜感激
以下是我的代码的一部分
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
答案 0 :(得分:2)
首先,您的INSERT语法完全错误。您可以在文档中轻松查找:INSERT INTO table (columns) SELECT ...
。
然后你说你要复制一条记录,但要使工资为负。这与行聚合(SUM
和GROUP 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_name
,age
,salary
答案 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
这样我还没有测试过代码