Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a vales like this.

TaskId	InternalId	(No column name)	UploadId	RequestId	CreatedDate	ProcessStatusMappingID
959	959	0	183	959636241330158000000	3/3/2017	197
959	959	0	183	959636241463837000000	3/3/2017	197
960	960	0	183	960636241330205000000	3/3/2017	197
960	960	0	183	960636241463912000000	3/3/2017	197
961	961	0	183	961636241330219000000	3/3/2017	197
983	983	0	194	983636244258262000000	3/6/2017	197
983	983	0	194	983636244258532000000	3/6/2017	197
984	984	0	194	984636244258309000000	3/6/2017	197
984	984	0	194	984636244258553000000	3/6/2017	197
985	985	0	194	985636244258327000000	3/6/2017	197



i require a query to return the value has

TaskId	InternalId	(No column name)	UploadId	RequestId	CreatedDate	ProcessStatusMappingID
959	959	0	183	959636241463837000000	3/3/2017	197
960	960	0	183	960636241463912000000	3/3/2017	197
961	961	0	183	961636241330219000000	3/3/2017	197
983	983	0	194	983636244258532000000	3/6/2017	197
984	984	0	194	984636244258553000000	3/6/2017	197
985	985	0	194	985636244258327000000	3/6/2017	197


thanks in advnace.

What I have tried:

tried with distinct but not working.
Posted
Updated 9-May-17 12:25pm
v2
Comments
Suvendu Shekhar Giri 9-May-17 3:33am    
Anything you have tried so far?
Share the tried query.

Try this,

SQL
SELECT *
FROM   TABLE T
       INNER JOIN 
       (
           SELECT TASKID, MAX(REQUESTID) RID
           FROM   TABLE T
       ) TMP ON T.TASKID = TMP.TASKID AND T.REQUESTID = TMP.RID
 
Share this answer
 
Comments
Maciej Los 9-May-17 16:41pm    
Good one, even if there's no comment!
WITH DEDUPE AS (
SELECT *
, ROW_NUMBER() OVER ( PARTITION BY TaskId ORDER BY TaskId ) AS OCCURENCE
FROM table_name
)
SELECT * FROM DEDUPE
WHERE
OCCURENCE = 1
 
Share this answer
 
v2
There's no duplicates. All rows are unique! All what you want to achieve, is to get date with the highest RequestId.

I'd suggest to use MAX() aggregate function with OVER() clause[^]:
SQL
SELECT DISTINCT TaskId,	InternalId,	NoColumnName,	UploadId,
	MAX(RequestId) OVER( PARTITION BY TaskId ORDER BY CreatedDate) AS MaxRequestId,	CreatedDate,	ProcessStatusMappingID
FROM YourTableName
 
Share this answer
 
v2
C#
with task as(
select  *, ROW_NUMBER() over (partition by TaskId order by TaskId) as rownum from tasks) select * from task  where rownum=1

Using CTE and concept of Row_Number we can achieve distinct records. task is name of CTE and rownum is Temporary column Name.
 
Share this answer
 
Comments
CHill60 10-May-17 9:14am    
This is exactly the same as Solution 2 - all you have done is change the alias names.
Member 13154494 10-May-17 11:07am    
Yes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900