1
CREATE TABLE employee
(
    joining_date date,
    employee_type character varying,
    name character varying 
);
insert into employee VALUES
    (NULL,'as','hjasghg'),
    ('2022-08-12', 'Rs', 'sa'),
    (NULL,'asktyuk','hjasg');
    
create table insrt_st (employee_type varchar, dt date);
insert into insrt_st VALUES ('as', '2022-12-01'),('asktyuk', '2022-12-08') 

What I want to do is write a single query to insert date from insrt_st in employee table where joining_date is null in employee column?

3
  • It's unclear to me what you are trying to achieve. Can't you just run an UPDATE statement after the two INSERTs? Commented Jan 11, 2023 at 13:00
  • you mean this? dbfiddle.uk/xDXPOJm2 add the expected results to make your question clearer. cheers Commented Jan 11, 2023 at 13:13
  • @a_horse_with_no_name, this is a sample. I have more than a thousand such columns. _gotqn provided an appropriate answer. Commented Jan 11, 2023 at 13:30

1 Answer 1

1

You can just write:

insert into employee (joining_date, employee_type, name) VALUES 
    (NULL,'as','hjasghg'),
    ('2022-08-12', 'Rs', 'sa'),
    (NULL,'asktyuk','hjasg');
    
insert into insrt_st (employee_type, dt)
VALUES ('as', '2022-12-01')
     ,('asktyuk', '2022-12-08') ;


UPDATE employee
SET joining_date = I.dt
FROM insrt_st I
WHERE employee.employee_type = I.employee_type 
AND employee.joining_date IS NULL;

SELECT joining_date,employee_type,name
FROM employee

Here is example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.