Query for inserting value to the newly created column - sql

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
);

Related

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

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.

T-SQL: convert columns to rows and insert/update another table

Looking for non fancy, easily debugable for junior developer solution...
In SQL Server 2008 R2, I have to update data from #data table to #tests table in desired format. I am not sure how I would archive result using T-SQL query?
NOTE: temp tables have only 3 columns for sample purpose only but real table have more than 50 columns for each set.
Here is what my tables look like:
IF OBJECT_ID('tempdb..#tests') IS NOT NULL
DROP TABLE #tests
GO
CREATE TABLE #tests
(
id int,
FirstName varchar(100),
LastName varchar(100),
UniueNumber varchar(100)
)
IF OBJECT_ID('tempdb..#data') IS NOT NULL
DROP TABLE #data
GO
CREATE TABLE #data
(
id int,
FirstName1 varchar(100),
LastName1 varchar(100),
UniueNumber1 varchar(100),
FirstName2 varchar(100),
LastName2 varchar(100),
UniueNumber2 varchar(100),
FirstName3 varchar(100),
LastName3 varchar(100),
UniueNumber3 varchar(100),
FirstName4 varchar(100),
LastName4 varchar(100),
UniueNumber4 varchar(100),
FirstName5 varchar(100),
LastName5 varchar(100),
UniueNumber5 varchar(100),
FirstName6 varchar(100),
LastName6 varchar(100),
UniueNumber6 varchar(100),
FirstName7 varchar(100),
LastName7 varchar(100),
UniueNumber7 varchar(100)
)
INSERT INTO #data
VALUES (111, 'Tom', 'M', '12345', 'Sam', 'M', '65432', 'Chris', 'PATT', '54656', 'Sean', 'Meyer', '865554', 'Mike', 'Max', '999999', 'Tee', 'itc', '656546444', 'Mickey', 'Mul', '65443231')
INSERT INTO #data
VALUES (222, 'Kurr', 'P', '22222', 'Yammy', 'G', '33333', 'Saras', 'pi', '55555', 'Man', 'Shey', '666666', 'Max', 'Dopit', '66666678', '', '', '', '', '', '')
INSERT INTO #data
VALUES (333, 'Mia', 'K', '625344', 'Tee', 'TE', '777766', 'david', 'mot', '4444444', 'Jeff', 'August', '5666666', 'Mylee', 'Max', '0000000', '', '', '', 'Amy', 'Marr', '55543444')
SELECT *
FROM #data
I want to insert/update data into #tests table from #data table.
Insert data into #tests table if id and UniqueNumber combination does not exists from #data table. If combination exists then update data into #tests table from #data table
This is desired output into #tests table
Here is an option that will dynamically UNPIVOT your data without using Dynamic SQL
To be clear: UNPIVOT would be more performant, but you don't have to enumerate the 50 columns.
This is assuming your columns end with a NUMERIC i.e. FirstName##
Example
Select ID
,FirstName
,LastName
,UniueNumber -- You could use SSN = UniueNumber
From (
SELECT A.ID
,Grp
,Col = replace([Key],Grp,'')
,Value
FROM #data A
Cross Apply (
Select [Key]
,Value
,Grp = substring([Key],patindex('%[0-9]%',[Key]),25)
From OpenJson( (Select A.* For JSON Path,Without_Array_Wrapper ) )
) B
) src
Pivot ( max(Value) for Col in ([FirstName],[LastName],[UniueNumber]) ) pvt
Order By ID,Grp
Results
UPDATE XML Version
Select ID
,FirstName
,LastName
,UniueNumber
From (
SELECT A.ID
,Grp = substring(Item,patindex('%[0-9]%',Item),50)
,Col = replace(Item,substring(Item,patindex('%[0-9]%',Item),50),'')
,Value
FROM #data A
Cross Apply ( values (convert(xml,(Select A.* for XML RAW)))) B(XData)
Cross Apply (
Select Item = xAttr.value('local-name(.)', 'varchar(100)')
,Value = xAttr.value('.','varchar(max)')
From B.XData.nodes('//#*') xNode(xAttr)
) C
Where Item not in ('ID')
) src
Pivot ( max(Value) for Col in (FirstName,LastName,UniueNumber) ) pvt
Order By ID,Grp
One way is to query each group of columns separately and UNION the results
SELECT
id int,
FirstName1 as FirstName,
LastName1 as LastName,
UniueNumber1 AS SSN
FROM #data
UNION
SELECT
id int,
FirstName2 as FirstName,
LastName2 as LastName,
UniueNumber2 AS SSN
FROM #data
UNION
...
There's not a way to cleanly "loop through" the 7 groups of columns - you'll spend more time building a loop to create the query dynamically than just copying and pasting the query 6 times and changing the number.
Of course, it's best to avoid the type of structure you have in #data now if at all possible.

How can I return the top N most recently inserted records from a table? [duplicate]

I have created table like below:
create table EmployeeDetails
(
id int,
name varchar(50),
designation varchar(50),
ReportingTo int
)
And inserted rows like this:
insert into EmployeeDetails values(1, 'A', 'Developer', 3)
insert into EmployeeDetails values(5, 'E', 'CEO', 5)
insert into EmployeeDetails values(2, 'B', 'Developer', 3)
insert into EmployeeDetails values(3, 'C', 'Manager', 4)
insert into EmployeeDetails values(4, 'D', 'S.Manager', 5)
My question is: how can I identify the last inserted row in the table?
Used queries:
select IDENT_CURRENT('EmployeeDetails')
Select SCOPE_IDENTITY()
But I still didn't find the answer.
Replies are always welcome
You have a fundamental misunderstanding of tables in relational databases. They represent unordered sets. So, there is no "last row" in a table. The information on the ordering of rows has to be included when you create the table and/or load data into it.
In SQL Server, the simplest method is an identity column. That is why many tables are defined as:
create table EmployeeDetails (
id int identity primary key,
name varchar(50),
designation varchar(50),
ReportingTo int
);
You can also add default insertion time columns:
create table EmployeeDetails (
id int identity primary key,
name varchar(50),
designation varchar(50),
ReportingTo int,
CreatedAt datetime default gettime()
);
However, you can have multiple rows with the same datetime value.
You also can use rowversion like below,if you add a column
CREATE TABLE MyTest (myKey int
,myValue int, RV rowversion);
GO
INSERT INTO MyTest (myKey, myValue) VALUES (1, 0);
GO
INSERT INTO MyTest (myKey, myValue) VALUES (2, 0);
select * from mytest order by rv desc
Note:
This always give the last row inserted/Updated
References:
rowversion
Add fields of type datetime with name CreateDate and UpdateDate in your table, when you insert record in your table set their value for getdate()
After that you can run queries:
Select top 10 * from YourTable Order By CreateDate DESC
Or for last updated
Select top 10 * from YourTable Order By UpdateDate DESC
Please find my answer as below. Hope this may help you.
Add one more column to store record creation date/time as below.
create table EmployeeDetails
(
id int,
name varchar(50),
designation varchar(50),
ReportingTo int,
CreatedOn datetime
)
After table creation and inserting records write/execute query (here inner query is used) as below
select
*
from EmployeeDetails
where CreatedOn = (select max(CreatedOn) from EmployeeDetails )

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

UNION and MIN/MAX with Grouping and Odd Date Request

This is a new version of my question, since it seems to be confusing. Sorry. I figured it out. See the code if you're interested. Notes to solve are in there. Thanks for your help!
I got it to work this far, but the OriginaionL (L is for Little and B is for Big) is not correct. It's taking the correct date but not Origination.
CREATE TABLE MyTable
(
LoadTagID INT,
EnteredDateTime datetime,
JobNumber VARCHAR(50),
Origination VARCHAR(50)
)
INSERT INTO MyTable VALUES
(1, '2015-02-09 00:00:00.00', 11111, 'Here')
,(2, '2015-02-09 00:00:00.00', 22222, 'There')
,(3, '2016-03-09 00:00:00.00', 11111, 'Outside')
,(4, '2016-08-09 00:00:00.00', 12578, 'Anywhere')
,(252, '2017-06-29 00:00:00.00', 12345, 'Here')
,(253, '2017-08-01 00:00:00.00', 99999, 'There')
,(254, '2017-08-04 00:00:00.00', 12345, 'Outside')
,(255, '2017-08-09 00:00:00.00', 12345, 'Anywhere')
,(256, '2017-08-10 00:00:00.00', 99999, 'Anywhere')
,(257, '2017-08-10 00:00:00.00', 123456, 'Anywhere')
,(258, '2017-08-11 00:00:00.00', 123456, 'Over Yonder')
,(259, '2017-08-13 00:00:00.00', 99999, 'Under The Bridge')
--Select * From MyTable
CREATE TABLE #LTTB1 --MAX
(
LoadTagID varchar(50),
JobNumber varchar(50),
EnteredDateTime varchar(50),
Origination varchar(50)
)
CREATE TABLE #LTTB2 --MIN
(
LoadTagID varchar(50),
JobNumber varchar(50),
EnteredDateTime varchar(50),
Origination varchar(50)
)
CREATE TABLE #LTTB3
(
LoadTagIDL varchar(50),
JobNumberL varchar(50),
EnteredDateTimeL
varchar(50),
OriginationL varchar(50)
, LoadTagID varchar(50),
JobNumber varchar(50),
EnteredDateTime varchar(50),
Origination varchar(50)
)
INSERT INTO #LTTB1
SELECT
MAX(LoadTagID) AS LoadTagID,
JobNumber,
MAX(EnteredDateTime) AS EnteredDateTime,
MAX(Origination) AS Origination
FROM MyTable
WHERE CONVERT (Date, EnteredDateTime) >= CONVERT (Date, GETDATE()-10) --Gets the last 10 days.
GROUP BY JobNumber ORDER BY JobNumber
INSERT INTO #LTTB2
SELECT MIN(LoadTagID) AS LoadTagIDL,
JobNumber AS JobNumberL,
MIN(EnteredDateTime) AS EnteredDateTimeL,
MAX(Origination) AS OriginationL --MAX! This needed to be max!! Why?
FROM MyTable
Where CONVERT (Date, EnteredDateTime) >= CONVERT (Date, GETDATE()-60) --Goes further back in case one is a long.
GROUP BY JobNumber ORDER BY JobNumber
INSERT INTO #LTTB3
SELECT L.LoadTagID AS LoadTagIDL
, L.JobNumber AS JobNumberL
, L.EnteredDateTime AS EnteredDateTimeL
, L.Origination AS OriginationL
, B.LoadTagID, B.JobNumber, B.EnteredDateTime, B.Origination
FROM #LTTB1 B --MAX
INNER JOIN #LTTB2 L ON B.JobNumber = L.JobNumber
Select * From #LTTB3
So for JobNumber 12345 6/29 is correct, but it should be "Here" and not "Anywhere:
For 99999 everything is correct but for 8/1 it should be "There" and not Anywhere. That seems to be the middle value in the set. I'm so confused.
Does anyone know why it's grabbing that value? Thank you.
SELECT *
FROM mytable
WHERE LoadTagID=(SELECT MIN(LoadTagID)
FROM mytable)
OR LoadTagID=(SELECT MAX(LoadTagID)
FROM mytable);
query according to the output you want
CREATE TABLE MyTable
(
LoadTagID INT,
Date Date,
Job INT,
Origination VARCHAR(20)
)
INSERT INTO MyTable
VALUES(
252, '6/29/17', 12345, 'Here')
,(253, '8/1/17', 99999, 'There')
,(254, '8/4/17', 12345, 'Outside')
,(255, '8/8/17', 12345, 'Anywhere')
--SELECT * FROM MyTable
SELECT * INTO #Table1
FROM MyTable
WHERE LoadTagID IN (SELECT MIN(LoadTagID)
FROM MyTable)
SELECT * INTO #Table2
FROM MyTable
WHERE LoadTagID IN (SELECT MAX(LoadTagID)
FROM MyTable)
SELECT * INTO #T3
FROM ( SELECT * FROM #Table1 T1
UNION ALL
SELECT * FROM #Table2 T2
) A
SELECT #T3.Date,
#T3.Job,
#T3.LoadTagID,
#T3.Origination
FROM #T3
LEFT JOIN #Table1 T1
ON T1.Job=#T3.Job
WHERE T1.Job IS NOT NULL
INSERT INTO #LTTB1
SELECT
MAX(LoadTagID) AS LoadTagID,
JobNumber,
MAX(EnteredDateTime) AS EnteredDateTime,
MAX(Origination) AS Origination
FROM MyTable
WHERE CONVERT (Date, EnteredDateTime) >= CONVERT (Date, GETDATE()-10) --Gets the last 10 days.
GROUP BY JobNumber ORDER BY JobNumber
INSERT INTO #LTTB2
select LoadTagID
,JobNumber
,EnteredDateTime
,Origination from (
select *, ROW_NUMBER() Over(partition by jobnumber order by EnteredDateTime) l
from MyTable
Where CONVERT (Date, EnteredDateTime) >= CONVERT (Date, GETDATE()-60)
)lk
where lk.l=1
INSERT INTO #LTTB3
SELECT L.LoadTagID AS LoadTagIDL
, L.JobNumber AS JobNumberL
, L.EnteredDateTime AS EnteredDateTimeL
, L.Origination AS OriginationL
, B.LoadTagID, B.JobNumber, B.EnteredDateTime, B.Origination
FROM #LTTB1 B --MAX
INNER JOIN #LTTB2 L ON B.JobNumber = L.JobNumber
Select * From MyTable
Select * From #LTTB3
--I hope, your prob has been solved now..