Delete data based on a column and latest date - sql

In the below Table, i need to get the query for table to Get the Delete query to remove the Unique Employee based on the older date of joining.
The result should have unique EmpNo, where the Doj is latest.
Below is the table data query
DECLARE #TableTest TABLE (
[Sid] [int] NULL,
[EmpNo] [int] NULL,
[EmpName] [nvarchar](50) NULL,
[DoJ] [int] NULL
)
INSERT INTO #TableTest VALUES
(101,'Suresh',20160517)
,(102,'Ravi',20160312)
,(101,'Ramu',20161022)
,(103,'Kumar',20160308)
,(104,'Gopi',20160411)
,(104,'Sridhar',20160620)
,(104,'Suresh',20161012)
Expected result

If you want to delete the rows, you can do:
with todelete as (
select tt.*,
row_number() over (partition by EmpNo order by Sid desc) as seqnum
from #tabletest
)
delete from todelete
where seqnum <> 1;

Related

SQL get recently modified post for each user

This is my table:
CREATE TABLE [dbo].[posts]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[user_id] [int] NOT NULL,
[date_posted] [datetime] NOT NULL,
[date_modified] [datetime] NOT NULL,
[content] [text] NOT NULL,
CONSTRAINT [PK_posts] PRIMARY KEY CLUSTERED ( [id] ASC )
)
My company needs a single query that will get the post id of the most recently modified post for each user. Can anyone please help me? Thanks,
The rank() function should do the trick:
SELECT user_id, id AS most_recent_post_id
FROM (SELECT user_id,
id,
RANK() OVER (PARTITION BY user_id ORDER BY date_posted DESC) AS rk
FROM [posts]) p
WHERE rk = 1

Simple max() function and inner join T-SQL

I have simple table which puzzles me:
CREATE TABLE [dbo].[T3]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[S_Id] [int] NULL,
[P_Id] [int] NULL,
[level] [int] NULL,
[Path] [nvarchar](255) NULL
) ON [PRIMARY]
In the table is data
id S_Id P_Id level Path
------------------------------------
1 218252 218231 1 218231
2 218271 218252 1 218252
3 218271 218252 2 218231-218252
EDIT:
I try to get the
ID, S_ID, P_ID, level, Path
on maximum length of column Path.
It should return id 3.
If I try to get max len from path like this:
select
b.id, a.p_id, a.s_id,
max(len(a.path)) as Path,
a.path
from
t3 a, t3 b
where
b.id = a.id
group by
a.p_id , a.s_id, b.id , a.path
order by
1
I get all the data, not just row with id 3, why ?
If you only want the max path record... Correct me if I'm wrong.
;WITH tmp AS (select TOP 1 id from #TaskTask3 ORDER BY LEN(path) DESC)
select t.*
from #TaskTask3 t
inner join tmp on tmp.id = t.id
Updates
;WITH tmp AS (select id, row_number() over (partition by S_Id, P_Id order by len(path) DESC) as rn from #TaskTask3)
select t.*
from #TaskTask3 t
inner join tmp on tmp.id = t.id
WHERE tmp.rn = 1
I tried to keep it simple....There are other methods (mentioned already) but I think you need to start slow... :)
declare #maxLen int
select #maxLen = max(len(path))
from t3
select * from t3
where len (path) = #maxLen
To put weePee's answer in a simple way,you can use the following query:
select id,s_id,p_id,level from t3 where len(path)= (select max(len(path)) from t3)
This is what I used to create and insert into table t3:
CREATE TABLE [dbo].[T3]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[S_Id] [int] NULL,
[P_Id] [int] NULL,
[level] [int] NULL,
[Path] [nvarchar](255) NULL
) ON [PRIMARY]
insert into t3 (s_id,p_id,level,path) values (218252,218231,1,'218231'),(218271,218252,1,'218252'),(218271,218252,2,'218231-218252')

Update a column in table with information from another table

Im already have this:
USE [AdventureWorks2012];
--- somewhere create table
IF OBJECT_ID('[dbo].[PersonPhone]','U') IS NOT NULL
DROP TABLE [dbo].[PersonPhone]
CREATE TABLE [dbo].[PersonPhone](
[BusinessEntityID] [int] NOT NULL,
[PhoneNumber] nvarchar(25) NOT NULL,
[PhoneNumberTypeID] [int] NOT NULL,
[ModifiedDate] [datetime] NOT NULL)
--- and append new column
ALTER Table [dbo].[PersonPhone]
ADD [StartDate] date NULL
--- after this, i want copy dates in this column (at another table, we increase dates by one)
UPDATE [dbo].[PersonPhone]
SET [StartDate] = [NewTable].[NewDate]
FROM (
SELECT DATEADD(day, 1, [HumanResources].[EmployeeDepartmentHistory].[StartDate]) AS [NewDate],
row_number() over(order by (select 1)) AS [RN]
FROM [HumanResources].[EmployeeDepartmentHistory]
) [NewTable]
How to improve query to copy the values ​​from [NewTable] to [dbo].[PersonPhone].[StartDate] row?
At your Update:
UPDATE [dbo].[PersonPhone]
SET [StartDate] = DATEADD(day, 1, [HumanResources].[EmployeeDepartmentHistory].[StartDate])
FROM [HumanResources].[EmployeeDepartmentHistory]
GO
SELECT row_number() over(order by (select 1)) AS [RN] FROM [HumanResources].[EmployeeDepartmentHistory]

Range rows from SQL Server

I want to create paging using T-SQL. How can I select 10 rows from 11th row to 20th row?
I know I can do this with C# etc. But my question is about SQL Server.
Here is the table:
CREATE TABLE EarlyAccess(
[EarlyAccessUserId] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[Lastname] [nvarchar](50) NOT NULL
)
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY earlyAccessUserId) rn
FROM earlyAccess
) q
WHERE rn BETWEEN 11 AND 20
ORDER BY
earlyAccessUserId
There is not such thing as n'th record in SQL unless you explicitly specify ordering.

Subquery Performance - Non Unique Column in Where Clause

I have two tables
Table Jobs
[ID] [int] IDENTITY(1,1) NOT NULL,
[title] [varchar](150) NULL,
[description] [text] NULL
Table JobSkills
[id] [int] IDENTITY(1,1) NOT NULL,
[jobId] [int] NULL,
[skill] [varchar](150) NULL
Shown above partial list of columns.
For table JobSkills I have indexed jobId column, column skill is full text indexed.
I have a stored procedure to get the list of jobs. sort of like this.
Select totalItems
,Id,title
from
(
Select Row_Number() over(Order By
CASE WHEN #sortBy Is Not Null AND #sortBy='relevance'
THEN
SkillMatchRank
END DESC
,CASE WHEN #sortBy Is Not Null AND #sortBy='date' THEN CreateDate END DESC
) As rowNumber
,COUNT(*) OVER() as totalItems
,ID,createDate,title
from Jobs J
OUTER APPLY dbo.GetJobSkillMatchRank(J.ID,#searchKey) As SkillMatchRank
Where
--where conditions here
) tempData
where
rowNumber>=CASE WHEN #startIndex>0 AND #endIndex>0 THEN #startIndex ELSE rowNumber END
AND rowNumber<=CASE WHEN #startIndex>0 AND #endIndex>0 THEN #endIndex ELSE rowNumber END
I have created a inline table valued function to get the skill matching rank.
CREATE FUNCTION [dbo].[GetJobSkillMatchRank]
(
#jobId int,
#searchKey varchar(150)
)
RETURNS TABLE
AS
RETURN
(
select SUM(ISNULL(JS2.[Rank],0)) as rank
from FREETEXTTABLE(JobSkills,skill,#searchKey) JS2
Where JS2.[Key] in (Select ID from JobSkills Where jobId=#jobId)
)
GO
Problem
Query runs super slow, more then a minute.
My observation
For the table valued function if I set jobId=1 (I do have a job with id=1) then it performs super fast as desired.
I understand that jobId is not unique column on JobSkills table.
In this case how could I improve the performance???
UDFs are great in certain cases, but the execution plans don't get cached like sprocs do. If you try using another derived table instead of a function, the query might perform better.