How to insert value using a table in another table in postgres? - sql

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?

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.

Related

Query for inserting value to the newly created column

I have created a table
CREATE TABLE #employee
(
employee_code VARCHAR(100) NOT NULL,
employee_name VARCHAR(50) NOT NULL,
doj DATE ,
dob DATE
)
INSERT INTO #employee (employee_code, employee_name, doj, dob)
SELECT '1', 'sabith', '01/01/2015', '01/01/1990'
UNION ALL
SELECT '2', 'siraj', '01/01/2016', '01/01/1991'
CREATE TABLE #emp_leave_tables
(
employee_code VARCHAR(50),
leave_ref_no VARCHAR(50),
req_date date,
leave_from_date date,
leave_to_date date
)
INSERT INTO #emp_leave_tables (employee_code, leave_ref_no, req_date, leave_from_date, leave_to_date)
SELECT '1', '1001', '01/01/2020', '01/01/2020', '01/05/2020'
UNION ALL
SELECT '2', '1001', '01/01/2020', '01/01/2020','06/01/2020'
I want to create a temp table #result and a new column no_days to count he employee's number of leave units and I want to insert the value into the temp table
For creating the new temp table, try this:
create table temp_table (
employee_code varchar(50) primary key,
leave_units smallint
);
Then, you may use this to populate the table:
insert into temp_table
values (
select e.employee_code
, max(DATEDIFF(day, el.leave_to_date, el.leave_from_date)) as maximum
, min(DATEDIFF(day, el.leave_to_date, el.leave_from_date)) as minimum
from emp_leaves_table e
group by e.employee_code
);

Rownumber to select non duplicate or distinct rows from a table. Invalid name ''

I am trying to select non-duplicate rows from emp_demo2 table:
Table:
CREATE TABLE Emp_demo3 (
emp_ID INT,
emp_Name NVARCHAR (50),
emp_sal_K INT,
emp_manager INT,
joining_date date,
last_time date)
GO
INSERT INTO Emp_demo3 VALUES (1,'Ali', 200,2,'2010-01-28','2015-05-09')
INSERT INTO Emp_demo3 VALUES (2,'Zaid', 770,4,'2008-01-28','2015-05-09')
INSERT INTO Emp_demo3 VALUES (3,'Mohd', 1140,2,'2007-01-28','2015-05-09')
INSERT INTO Emp_demo3 VALUES (4,'LILY', 770,Null,'2013-01-28','2015-05-09')
INSERT INTO Emp_demo3 VALUES (5,'John', 1240,6,'2016-01-28','2015-05-09')
INSERT INTO Emp_demo3 VALUES (6,'Mike', 1140,4,'2018-01-28','2015-05-09')
INSERT INTO Emp_demo3 VALUES (5,'John', 1240,6,'2017-01-28','2015-05-09')
INSERT INTO Emp_demo3 VALUES (3,'Mohd', 1140,2,'2010-01-28','2015-05-09')
Code to add column date_difference
alter table emp_demo3
add date_diff date
go
update emp_demo3 set date_diff = datediff(day,joining_date, last_time)
I am trying to calculate date difference in days between 2 dates. Please note that this is just a random table I created. I cannot change date formats in my original table. So please tell how to get date difference with existing date formats.
Error
Operand type clash: int is incompatible with date
You can use ROW_Number to make partitions by columns which are the same. Then it is necessary just filter rows which have rownum = 1:
An example:
DECLARE #Emp_demo2 TABLE (
emp_ID INT,
emp_Name NVARCHAR (50),
emp_sal_K INT,
emp_manager INT)
INSERT INTO #Emp_demo2 VALUES (1,'Ali', 200,2)
INSERT INTO #Emp_demo2 VALUES (2,'Zaid', 770,4)
INSERT INTO #Emp_demo2 VALUES (3,'Mohd', 1140,2)
INSERT INTO #Emp_demo2 VALUES (4,'LILY', 770,Null)
INSERT INTO #Emp_demo2 VALUES (5,'John', 1240,6)
INSERT INTO #Emp_demo2 VALUES (6,'Mike', 1140,4)
INSERT INTO #Emp_demo2 VALUES (5,'John', 1240,6)
INSERT INTO #Emp_demo2 VALUES (3,'Mohd', 1140,2)
SELECT * FROM
(
SELECT
t.emp_ID
, t.emp_Name
, t.emp_sal_K
, t.emp_manager
, ROW_NUMBER() OVER (PARTITION BY t.emp_Name, t.emp_sal_K, t.emp_manager
ORDER BY t.emp_Name) AS RowNum
FROM #Emp_demo2 AS t
)q
WHERE q.RowNum = 1
ORDER BY q.emp_ID
Following query is to find duplicates
select
distinct emp_ID,
emp_Name,
emp_sal_K,
emp_manager
from
(
select *,
count(*) over (partition by emp_id) as total
from Emp_demo2 e
) val
where total > 1
order by
emp_ID
If you want only distinct values then you can use following
select
distinct emp_ID,
emp_Name,
emp_sal_K,
emp_manager
from Emp_demo2 e
order by
emp_ID
METHOD 1:
SELECT DISTINCT * from #EmP_demo2
METHOD 2:
;WITH CTE AS(
SELECT * , ROW_NUMBER() OVER(PARTITION BY EMP_ID ORDER BY EMP_ID) AS ROWNUM
FROM #EMP_DEMO2 E
)
SELECT * FROM CTE WHERE ROWNUM=1
METHOD 3:
By using Group by also we ca avoid this duplicates.
Hope this works fine for your case
you can use this query,
select * from (select ROW_NUMBER() over(partition by emp_id order by emp_id) AS Rownum, * from Emp_demo2 as e
)x
where x.Rownum = 1

have multiple columns need to display column type with other column in rows

input
enter image description here
output
enter image description here
select deptno, replace(title,'d','author),date from employee
for the particular departno I have multiple title and date so i want to display in row wise example
Your data does not match your desired results. Here is what I believe you want:
create table deleteme_tbl(department int, title varchar2(20), mydate date);
insert into deleteme_tbl values( 1,'One', date '2016-01-01');
insert into deleteme_tbl values( 1,'Two', date '2016-04-01');
insert into deleteme_tbl values( 1,'three', date '2016-02-02');
insert into deleteme_tbl values( 2,'five', date '2016-04-04');
insert into deleteme_tbl values( 2,'six', null);
insert into deleteme_tbl values( 2,'seven', null);
commit;
SELECT department
, LISTAGG (title, ',') WITHIN GROUP (ORDER BY title) titles
, LISTAGG (mydate, ',') WITHIN GROUP (ORDER BY mydate) mydates
FROM deleteme_tbl
GROUP BY department;
DEPARTMENT TITLES MYDATES
1 One,Two,three 01-JAN-16,02-FEB-16,01-APR-16
2 five,seven,six 04-APR-16

How to count records in SQL

Edit: Schema taken/extrapolated from comment below
create table #employees
(
Emp_ID int,
Name varchar(50),
Dept_ID int,
);
create table #departments
(
Dept_ID int,
Dept_Name varchar(50)
);
How do I count the number of employees from table employees that work in each department in table departments and include all departments that have no employees working in them.
Welcome to SO.
This problem is quite simple to solve. The steps would be as follows:
Join the Departments table to the Employees table on the Dept_ID column.
SELECT the Dept_ID and Count() and GROUP BY the Dept_ID field.
In order to return Departments without employees you need to LEFT JOIN this aggregation to the Departments table on the Dept_ID column.
In order to return the value of 0 for departments without employees, use the ISNULL() function.
Please see the below sample script using your schema. Note that this script is written in T-SQL, as you did not mention your server type.
create table #employees
(
Emp_ID int,
Name varchar(50),
Dept_ID int,
);
create table #departments
(
Dept_ID int,
Dept_Name varchar(50)
);
insert into #employees
select 1, 'Pim', 1
union all
select 2, 'Salma', 2;
insert into #departments
select 1, 'IT'
union all
select 2, 'Marketing'
union all
select 3, 'Design'
select
d1.Dept_Name
,isnull(d2.EmployeeCount, 0) as EmployeeCount
from
#departments d1
left join
(
select
d.Dept_ID
,count(e.Dept_ID) as EmployeeCount
from
#departments d
join
#employees e
on e.Dept_ID = d.Dept_ID
group by
d.Dept_ID
)
d2
on d2.Dept_ID = d1.Dept_ID
drop table #employees
drop table #departments
As you have supplied no data please try below and see if this works for you
Create the tables, I don't know if this is similar to your table structure
CREATE TABLE tbl_EMPLOYEES (Empl_Name nvarchar(20), Dept nvarchar(15))
CREATE TABLE tbl_DEPARTMENT (Dept nvarchar(15))
Populate these tables
INSERT INTO tbl_EMPLOYEES Values ('James', 'Finance')
INSERT INTO tbl_EMPLOYEES Values ('Tim', 'HR')
INSERT INTO tbl_EMPLOYEES Values ('Sally', 'Finance')
INSERT INTO tbl_EMPLOYEES Values ('Bob', 'Sales')
INSERT INTO tbl_EMPLOYEES Values ('Sam', 'HR')
INSERT INTO tbl_EMPLOYEES Values ('James', 'Finance')
INSERT INTO tbl_DEPARTMENT Values ('Finance')
INSERT INTO tbl_DEPARTMENT Values ('HR')
INSERT INTO tbl_DEPARTMENT Values ('Sales')
INSERT INTO tbl_DEPARTMENT Values ('IT')
This query will give you the number of people in each department
SELECT Dept, Count(Dept) AS Count
FROM
(
SELECT Dept
FROM tbl_EMPLOYEES
) AS Blah_Blah
GROUP BY Dept

Reference table from subquery in Oracle

I have simplified my tables but essentially I have a table of accounts that have a cycle_no and end date. This end date is always set to the first of the month but I need to get the real end date by looking in the calendar details table. The real end date is the next date for this cycle_no.
To create the simplified tables and enter a few rows of data:
CREATE TABLE DATA_OWNER.ACCOUNT
(
ACCNO NUMBER(4),
CYCLE_NO NUMBER(4),
ENDDATE DATE
);
CREATE TABLE DATA_OWNER.CALENDAR_DETAILS
(
CALENDAR_DT DATE,
BILL_CYCL_NO NUMBER(4)
);
INSERT INTO calendar_Details
VALUES
('18/DEC/2017',
17);
INSERT INTO calendar_Details
VALUES
('23/DEC/2017',
20);
INSERT INTO calendar_Details
VALUES
('18/JAN/2018',
17);
INSERT INTO calendar_Details
VALUES
('23/JAN/2018',
20);
INSERT INTO calendar_Details
VALUES
('20/FEB/2018',
17);
INSERT INTO calendar_Details
VALUES
('21/FEB/2018',
20);
INSERT INTO account
VALUES
(1, 17, '01/DEC/2107');
INSERT INTO account
VALUES
(2, 20, '01/DEC/2107');
If we run this query though we get "ACC". "ENDDATE": invalid identifier:
SELECT accno, cycle_no, enddate, actual_date
FROM account acc
JOIN
(
SELECT MIN(calendar_dt) actual_date
FROM calendar_details cal
WHERE calendar_dt > acc.enddate
)
ON acc.cycle_no = cal.bill_cycl_no;
Can anyone give us some pointers on the best way to achieve this please?
You cannot refer to outer table references in a subquery in the FROM. Just use a correlated subquery:
SELECT accno, cycle_no, enddate,
(SELECT MIN(cal.calendar_dt) as actual_date
FROM calendar_details cal
WHERE cal.calendar_dt > acc.enddate AND acc.cycle_no = cal.bill_cycl_no
) as actual_date
FROM account acc;