How to populate given SQL records to all UserID - sql

I have a table in this form:
id | firstname | lastname | userid
---+-----------+------------------------
1 | john | smith | 545868-5434-343435-35353
2 | adam | finger | 545868-5434-343435-35353
3 | teri | marti | 545868-5434-343435-35353
4 | pei | port | 545868-5434-343435-35353
In the DB i have many userid i need to populate the very same firstname and lastname to all userid found in the Database
Here is my SQl Query
SELECT
cID, c.firstname,c.lastname,
[s].UserID,c.OwnerID
FROM
Customer INNER JOIN [s] ON c.OwnerID = [s].UserID AND c.AssignedtoID =
[s].UserID AND c.CreatedByUserID = [s].UserID
AssignedtoID is the same as UserID

is this helpful for you.?
Create table #tmpCustomer (id int, firstname VARCHAR(50),lastname VARCHAR(50),userid VARCHAR(100))
INSERT INTO #tmpCustomer
SELECT 1, 'john','smith','545868-5434-343435-35353'
union
SELECT 2,'adam','finger','545868-5434-343435-35353'
union
SELECT 3,'teri','marti','545868-5434-343435-35353'
union
SELECT 4, 'pei','port','545868-5434-343435-35353'
union
SELECT 5, 'abc','xyz','545868-5434-343435-35354'
union
SELECT 6, 'mno','ert','545868-5434-343435-35354'
--select * from #tmpCustomer
;with cte1 AS(Select row_number()over(partition by userid order by id) rn,* from #tmpCustomer ),
cte2 AS (select * from cte1 where rn=1 )
update t
set t.firstname=c.firstname
from #tmpCustomer t
JOIN cte2 c on t.userid=c.userid
select * from #tmpCustomer
drop table #tmpCustomer

i don't know if i good understand your question, try solution posted below
DECLARE #cust as table (firstname varchar(20),lastname Varchar(20))
Insert #cust
values
('Suzan','Smith')
declare #id as table (id int identity,anything varchar(20),row_inserted datetime2 default (cast(sysdatetime() as datetime2)))
INSERT #id
(anything,row_inserted)
SELECT 'x' ,'20180305'
union all
select 'y','20180305'
union all
select 'z','20180305'
select s.id,c.firstname,
c.lastname
from #id as s
cross join #cust as c

Related

How to get child of master entry from sql table

I have a table for ledgers. It consists of MasterID as unique ID and a parentID for joining.
Model of my table
As in the image my parent ledger is 'A'. 'B' is direct child of 'A'. 'C' is direct child of 'B' and 'D' is direct child of 'C'.
I want a select query to select all childs of 'A'. i.e, result will be B,C,D.
I am beginner in SQL.
I tried some while loop for this, but only direct child was accessible. I am not able make a logic for the requirement.
Thank you in advance.
That'as a typical hierarchical query. Consider the solution that makes use of a recursive cte:
with cte as (
select masterID rootID, masterID, name, parentid
from mytable
where parentID is null
union all
select c.rootID, t.masterID, t.name, t.parentID
from mytable t
inner join cte c on c.masterID = t.parentID
)
select * from cte
As commented by The Impaler, you can change starting condition where parentID is null to the id of another node if needed.
With your sample data, this yields:
rootID | masterID | name | parentid
-----: | -------: | :--- | -------:
1 | 1 | A | null
1 | 2 | B | 1
1 | 3 | C | 2
1 | 4 | D | 3
Note that I kept track of the id of the root object, so it is easier to understand what is going on if there are several roots in your data.
You can also use the root to generate a flat list of children:
with cte as (
select masterID rootID, masterID, name, parentid
from mytable
where parentID is null
union all
select c.rootID, t.masterID, t.name, t.parentID
from mytable t
inner join cte c on c.masterID = t.parentID
)
select rootID, string_agg(masterID, ',') childrenID from cte group by rootID
rootID | childrenID
-----: | :---------
1 | 1,2,3,4
Demo on DB Fiddle
Here is an option that uses HierarchyID
The range keys (R1 / R2) in the final select are optional.
Example
Declare #YourTable table (MasterID int ,name varchar(50), ParentID int);
Insert Into #YourTable values
( 1, 'A', null),
( 2, 'B', 1),
( 3, 'C', 2),
( 4, 'D', 3)
Declare #Top int = 2 --<< Sets top of Hier Try NULL for entire hier
Declare #Nest varchar(25) = '|-----' --<< Optional: Added for readability
;with cteP as (
Select MasterID
,ParentID
,Name
,HierID = convert(hierarchyid,concat('/',MasterID,'/'))
From #YourTable
Where IsNull(#Top,-1) = case when #Top is null then isnull(ParentID ,-1) else MasterID end
Union All
Select MasterID = r.MasterID
,ParentID = r.ParentID
,Name = r.Name
,HierID = convert(hierarchyid,concat(p.HierID.ToString(),r.MasterID,'/'))
From #YourTable r
Join cteP p on r.ParentID = p.MasterID)
Select R1 = Row_Number() over (Order By HierID)
,R2 = (Select count(*) From cteP where HierID.ToString() like A.HierID.ToString()+'%')
,Lvl = HierID.GetLevel()
,MasterID
,ParentID
,Name = Replicate(#Nest,HierID.GetLevel()-1) + Name
,HierID
,HierID_String = HierID.ToString()
From cteP A
Order By A.HierID
Returns

SQL Query group by in case statement

I Have three tables like bellow
**tRole**
+--------+----------+-----------+
| RoleID | RoleCode | RoleTitle |
+--------+----------+-----------+
| 1 | Role1 | RT1 |
| 2 | Role2 | RT2 |
| 3 | Role3 | RT3 |
+--------+----------+-----------+
**tEmployee**
+-------+-------+
| EmpID | Name |
+-------+-------+
| 1 | Emp 1 |
| 2 | Emp 2 |
| 3 | Emp 3 |
+-------+-------+
**tEmployeeRole**
+-------+--------+
| EmpID | RoleID |
+-------+--------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 3 | 3 |
+-------+--------+
I want a output like below ,If a role mapped with only one employee then employee name will show other wise Multiple will show.
+--------+----------+-----------+----------+
| RoleID | RoleCode | RoleTitle | Employee |
+--------+----------+-----------+----------+
| 1 | Role1 | RT1 | Multiple |
| 2 | Role2 | RT2 | Multiple |
| 3 | Role3 | RT3 | Emp 3 |
+--------+----------+-----------+----------+
I write bellow query but when I group by emp.First_Name then the result is wrong
select cr.RoleCode,cr.RoleID,
case
when count(ear.RoleID)=1 then emp.First_Name
else 'M' end as 'AssignedTO'
from tRole as cr
left outer join tEmployeeRole as ear on cr.RoleID=ear.RoleID
left outer join tEmployee as emp on ear.EmployeeID=emp.EmployeeID
group by cr.RoleCode,crRoleID,emp.First_Name
Hello You can use this query for your solution :
you need to count with partition and use distinct data
DECLARE #tRole TABLE (
RoleID INT
,RoleCode VARCHAR(50)
,RoleTitle VARCHAR(50)
)
DECLARE #tEmployee TABLE (
EmpID INT
,EmpName VARCHAR(50)
)
DECLARE #tEmployeeRole TABLE ( EmpID INT, RoleID INT )
INSERT #tRole ( RoleID, RoleCode, RoleTitle )
SELECT 1, 'Role1', 'RT1'
UNION
SELECT 2, 'Role2', 'RT2'
UNION
SELECT 3, 'Role3', 'RT3'
INSERT #tEmployee ( EmpID, EmpName )
SELECT 1, 'Epm 1'
UNION
SELECT 2, 'Epm 2'
UNION
SELECT 3, 'Epm 3'
INSERT #tEmployeeRole ( EmpID, RoleID )
SELECT 1, 1
UNION
SELECT 1, 2
UNION
SELECT 2, 1
UNION
SELECT 2, 2
UNION
SELECT 3, 3
SELECT DISTINCT tRole.RoleID
, RoleCode
, RoleTitle
, CASE WHEN COUNT(tEmployeeRole.EmpID) OVER ( PARTITION BY tEmployee.EmpID ) = 1 THEN tEmployee.EmpName ELSE 'Multiple'END [Employee]
FROM #tEmployee tEmployee
LEFT OUTER JOIN #tEmployeeRole tEmployeeRole ON tEmployeeRole.EmpID = tEmployee.EmpID
LEFT OUTER JOIN #tRole tRole ON tRole.RoleID = tEmployeeRole.RoleID
You can Modify the answer from #Pratik to add a col that lists the employees
;with CTE as(
SELECT
DISTINCT tRole.RoleID
RoleCode
, RoleTitle
, CASE WHEN COUNT(tEmployeeRole.EmpID) OVER ( PARTITION BY tEmployee.EmpID ) = 1 THEN tEmployee.EmpName ELSE 'Multiple'END [Employee]
FROM #tEmployee tEmployee
LEFT OUTER JOIN #tEmployeeRole tEmployeeRole ON tEmployeeRole.EmpID = tEmployee.EmpID
LEFT OUTER JOIN #tRole tRole ON tRole.RoleID = tEmployeeRole.RoleID
)
select *
,stuff( (select ','+EmpName from #tEmployee IE inner join #tEmployeeRole IER on IE.EmpID = IER.EmpID where IER.RoleID = CTE.rolecode for xml PATH('') ),1,1,'') AS EMList
from CTE
This query might help you out . Make a try
Your Tables Data looks like
create TABLE #tRole (RoleID INT ,RoleCode VARCHAR(50) ,RoleTitle VARCHAR(50) )
create TABLE #tEmployee (EmpID INT ,EmpName VARCHAR(50) )
create TABLE #tEmployeeRole( EmpID INT, RoleID INT )
INSERT #tRole ( RoleID, RoleCode, RoleTitle )
SELECT 1, 'Role1', 'RT1'
UNION
SELECT 2, 'Role2', 'RT2'
UNION
SELECT 3, 'Role3', 'RT3'
INSERT #tEmployee ( EmpID, EmpName )
SELECT 1, 'Epm 1'
UNION
SELECT 2, 'Epm 2'
UNION
SELECT 3, 'Epm 3'
INSERT #tEmployeeRole ( EmpID, RoleID )
SELECT 1, 1
UNION
SELECT 1, 2
UNION
SELECT 2, 1
UNION
SELECT 2, 2
UNION
SELECT 3, 3
Desired Query
;with cte as
(
select tr.roleid,tr.rolecode,tr.roletitle,te.empname
,COUNT(ter.EmpID) OVER ( PARTITION BY ter.EmpID ) as emp_count
from #tEmployee te
inner join #tEmployeeRole tER on tER.empid=te.empid
inner join #tRole tr on tr.roleid=ter.roleid
)
select distinct RoleID,RoleCode,RoleTitle
,case when emp_count>1 then 'Multiple' else empname end as Employee
from cte

Delete duplicate rows from a table and referenced table microsoft sql server

I have table Person :
PersonId | FirstName | LastName |
1 | 'John' | 'Doe' |
2 | 'Mike' | 'Test' |
3 | 'John' | 'Doe' |
4 | 'Mike' | 'Test' |
5 | 'John' | 'Doe' |
6 | 'John' | 'Doe' |
Table Customer :
CustomerId | PersonId |
1001 | 1 |
1002 | 2 |
1003 | 3 |
1004 | 4 |
1005 | 5 |
1006 | 6 |
I want to delete Customer 1003,1004,1005,1006 because their Persons are duplicate, but PersonId is not same.
This should check FirstName and LastName in Person table and delete the duplicates in Customer table , Then delete duplicates in Person table. ( 3,4,5,6 )
Sorry if similar questions has been asked before but I couldn't do this.
Check This.
We delete first from Customer table. First we find duplicate records by using Row_number() and deleting personid which have to rank more than 1.
Below Query show duplicate records :
select ROW_NUMBER () over ( partition by firstname,lastname order by
PersonId ) RID, PersonId,FirstName,LastName
from #Person
After finding Duplicates we delete it from customer table then Person.
delete from Customer where PersonId in
(
select distinct PersonId P from
( select ROW_NUMBER () over ( partition by firstname,lastname order by PersonId ) RID, PersonId,FirstName,LastName from #Person )a
where RID>1
)
delete from Person where PersonId in
(
select distinct PersonId P from
( select ROW_NUMBER () over ( partition by firstname,lastname order by PersonId ) RID, PersonId,FirstName,LastName from #Person )a
where RID>1
)
Use this query to view the duplicates:
with duplicatecte(personid,rownum)As
(
select personid ,
row_Number() over(partition by FirstName+LastName order by personid)
from #person
)
select b.personid,customerid from duplicatecte a
inner join #customer b on a.personid=b.personid where rownum>1
Modify this query to delete as below
with duplicatecte(personid,rownum)As
(
select personid ,
row_Number() over(partition by FirstName+LastName order by personid)
from #person
)
delete b
from duplicatecte a
inner join #customer b on a.personid=b.personid where rownum>1
Begin Tran
CREATE TABLE #Person(PersonId INT,FirstName NVARCHAR(50),LastName NVARCHAR(50))
CREATE TABLE #Customer (CustomerId INT,PersonId INT)
INSERT INTO #Person
SELECT 1,'John','Doe' UNION ALL
SELECT 2 ,'Mike','Test' UNION ALL
SELECT 3 ,'John','Doe' UNION ALL
SELECT 4 ,'Mike','Test' UNION ALL
SELECT 5 ,'John','Doe' UNION ALL
SELECT 6 ,'John','Doe'
INSERT INTO #Customer
SELECT 1001, 1 UNION ALL
SELECT 1002 ,2 UNION ALL
SELECT 1003 ,3 UNION ALL
SELECT 1004 ,4 UNION ALL
SELECT 1005,5 UNION ALL
SELECT 1006,6
GO
WITH CTE (PersonId, DuplicateCount)
AS
(
SELECT FirstName,
ROW_NUMBER() OVER(PARTITION BY FirstName,LastName ORDER BY FirstName,PersonId) AS DuplicateCount
FROM #Person
)
--Select * from CTE WHERE DuplicateCount>1
DELETE FROM CTE WHERE DuplicateCount >1
DELETE FROM #Customer WHERE PersonId NOT IN(SELECT PersonId FROM #Person)
Select * from #Person
SELECT * from #Customer
ROLLBACK TRAN
declare #tbl table
(pid int
)
;with cte
as
(
select t1.*,row_number() over (partition by firstname,lastname order by personid) as rownum
from
person t1
)
delete
from
cte
output deleted.personid into #tbl where rownum>1
delete from customer where personid in (select personid from #tbl)
This will work for you:
Declare #person As table
(
PersonId int,
FirstName varchar(25),
LastName varchar(25)
)
Declare #customer As table
(
CustomerId int,
PersonId int
)
Insert Into #person (PersonId,FirstName,LastName) values(1,'John','Doe')
Insert Into #person (PersonId,FirstName,LastName) values(2,'Mike','Test')
Insert Into #person (PersonId,FirstName,LastName) values(3,'John','Doe')
Insert Into #person (PersonId,FirstName,LastName) values(4,'Mike','Test')
Insert Into #person (PersonId,FirstName,LastName) values(5,'John','Doe')
Insert Into #person (PersonId,FirstName,LastName) values(6,'John','Doe')
Insert Into #customer(CustomerId,PersonId) values(1001,1)
Insert Into #customer(CustomerId,PersonId) values(1002,2)
Insert Into #customer(CustomerId,PersonId) values(1003,3)
Insert Into #customer(CustomerId,PersonId) values(1004,4)
Insert Into #customer(CustomerId,PersonId) values(1005,5)
Insert Into #customer(CustomerId,PersonId) values(1006,6)
select p.PersonId into #temp from #person p right join
(Select PersonId,FirstName,LastName, ROW_NUMBER() over (partition by FirstName,LastName Order by PersonId) rownumber
from #person ) a
on p.PersonId=a.PersonId where a.rownumber>1
delete from #customer where PersonId in (select PersonId from #temp)
delete from #person where PersonId in (select PersonId from #temp)
select *from #customer
select *from #person

SQL: Merging rows with comma separated values in columns

We have a table like this:
Group | User | Team
-------------------
Grp1 | U1 | T1,T2
Grp1 | U2 | T1,T2,T3
Grp1 | U3 | T4
Grp2 | U4 | T2,T4
Grp2 | U5 | T5
I want to create a view which has the data like this:
Group | Teams
-------------
Grp1 | T1,T2,T3,T4
Grp2 | T2,T4,T5
Can somebody please help me? I tried doing few trail and errors and finally I am at a state where I am not even sure where to start from now
Here is my approach.
I am using cursor but you can do it in while loop also.
The only issue is that teams in new table are not distinct but I think you can play with string commands and it will sort the issue.
CREATE TABLE #tbl1 ([GROUP] varchar(20), [USER] VARCHAR(20), team VARCHAR(20))
INSERT INTO #tbl1 ( [GROUP], [USER],team )
VALUES ( 'Grp1', 'U1', 'T1,T2'), ( 'Grp1', 'U2', 'T3,T4'), ( 'Grp1', 'U3', 'T4'),
( 'Grp2', 'U1', 'T1,T2'), ( 'Grp2', 'U2', 'T3,T4')
CREATE TABLE #tbl2 ([GROUP] varchar(20), team VARCHAR(20))
DECLARE #ListOfteams VARCHAR(max)
DECLARE #grp VARCHAR(20)
DECLARE Curs CURSOR FAST_FORWARD
FOR
SELECT DISTINCT [GROUP]
FROM #tbl1 AS T
OPEN Curs
FETCH NEXT FROM Curs INTO #grp
WHILE ##FETCH_STATUS = 0
BEGIN
SET #ListOfteams= ''
SELECT #ListOfteams= #ListOfteams+ [Team] + ',' FROM #tbl1 AS CL WHERE CL.[GROUP] = #grp ORDER BY [CL].[USER]
INSERT INTO #tbl2 ( [GROUP],team )
SELECT DISTINCT #grp,
SUBSTRING(#ListOfteams, 1, LEN(#ListOfteams)-1)
FROM #tbl1 AS T
WHERE T.[GROUP] = #grp;
FETCH NEXT FROM Curs INTO #grp
END
CLOSE Curs
DEALLOCATE Curs;
SELECT *
FROM #tbl2 AS T
Here is ow it works: http://rextester.com/BYK57259
+-------+----------------+
| GROUP | team |
+-------+----------------+
| Grp1 | T1,T2,T3,T4,T4 |
| Grp2 | T1,T2,T3,T4 |
+-------+----------------+
Here is how am doing this, first convert the comma seperated team names to individual rows based on their groups and then take the unique team grp and team names. Now apply the listagg function for the unique team names to achieve the result.
with t as
(
select 'Grp1' grp,'U1' user1,'T1,T2' team from dual
union all
select 'Grp1','U2','T1,T2,T3' from dual
union all
select 'Grp1','U3','T4' from dual
union all
select 'Grp2','U4','T2,T4' from dual
union all
select 'Grp2','U5','T5' from dual
),
g as
(
select distinct grp, regexp_substr(team,'[^,]+',1,level) team1
from t
connect by level <= LENGTH(REGEXP_REPLACE(team, '[^,]+')) + 1
--group by grp
)
select grp,listagg(team1,',') within group (order by team1) from g
group by grp;
THis is the output I got
+----+-------------------------------------------+
|GRP ||Teams
+----+-------------------------------------------+
|Grp1|T1,T2,T3,T4 |
|Grp2|T2,T4,T5 |
+----+-------------------------------------------+

How do you order a group of records then insert their order placement too?

I have a table of logs that contain a ID and TIMESTAMP. I want to ORDER BY ID and then TIMESTAMP.
For example, this is what the result set would look like:
12345 05:40
12345 05:50
12345 06:22
12345 07:55
12345 08:33
Once that's done, I want to INSERT a order value in a third column that signifies it's placement in the group from earliest to latest.
So, you would have something like this:
12345 05:40 1 <---First entry
12345 05:50 2
12345 06:22 3
12345 07:55 4
12345 08:33 5 <---Last entry
How can I do that in a SQL statement? I can select the data and ORDER BY ID, TIMESTAMP. But, I can't seem to INSERT a order value based on the groupings. :(
Try this update not an insert:
Fiddle demo here:
;with cte as(
select id, yourdate, row_number() over(order by id,yourdate) rn
from yourTable
)
Update ut Set thirdCol = rn
From yourTable ut join cte on ut.Id = cte.id and ut.yourdate = cte.yourdate
NOTE: if you need to get the thirdColumn updated per id basis, please partition your rownumber by using row_number() over (partition by id, order by order by id,yourdate)
Results:
| ID | YOURDATE | THIRDCOL |
|-------|----------|----------|
| 12345 | 05:40 | 1 |
| 12345 | 05:50 | 2 |
| 12345 | 06:22 | 3 |
| 12345 | 07:55 | 4 |
| 12345 | 08:33 | 5 |
Using a derived table and an update.
IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
drop table #TableOne
end
CREATE TABLE #TableOne
(
SomeColumnA int ,
LetterOfAlphabet varchar(12) ,
PositionOrdinal int not null default 0
)
INSERT INTO #TableOne ( SomeColumnA , LetterOfAlphabet )
select 123 , 'x'
union all select 123 , 'b'
union all select 123 , 'z'
union all select 123 , 't'
union all select 123 , 'c'
union all select 123 , 'd'
union all select 123 , 'e'
union all select 123 , 'a'
Select 'pre' as SpaceTimeContinium , * from #TableOne order by LetterOfAlphabet
Update
#TableOne
Set PositionOrdinal = derived1.rowid
From
( select SomeColumnA , LetterOfAlphabet , rowid = row_number() over (order by LetterOfAlphabet asc) from #TableOne innerT1 )
as derived1
join #TableOne t1
on t1.LetterOfAlphabet = derived1.LetterOfAlphabet and t1.SomeColumnA = derived1.SomeColumnA
Select 'post' as SpaceTimeContinium, * from #TableOne order by LetterOfAlphabet
IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
drop table #TableOne
end
To get the order you desire without doing an insert and an update, you can set your clustered index to handle it for you. The example below creates a clustered primary key.
To do this you must remove any clustered index that you already have on the table because you can only have one clustered index per table.
CREATE TABLE dbo.Table_1
(
ID int NOT NULL,
DTStamp datetime NOT NULL
)
ALTER TABLE dbo.Table_1 ADD CONSTRAINT
PK_Table_1 PRIMARY KEY CLUSTERED
(
ID,
DTStamp
)
Insert some random data to test with...
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,getdate());
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,dateadd(mi,1,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,dateadd(mi,2,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,dateadd(mi,3,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,dateadd(mi,4,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12340,dateadd(mi,5,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12340,dateadd(mi,6,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12340,dateadd(mi,7,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12340,dateadd(mi,8,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,1,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,2,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,3,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,4,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,5,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,6,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,7,getdate()));
INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,8,getdate()));
Now query your table and check out the order...
SELECT [ID] ,[DTStamp] FROM [Table_1]
If you need the order to display in a query, you can add the row number with an over clause.
SELECT [ID] ,[DTStamp],row_number() over (partition by [ID] order by [ID] ,[DTStamp]) as SortOdr FROM [Table_1]