| Author |
Topic  |
|
|
shailesh18
Starting Member
India
8 Posts |
Posted - 10/01/2011 : 04:26:07
|
Hi
I have an table of 500,000 records. the data in table is like FE
------------------------------------
ID CID Cdt_DT (dd-MM-yy)
-------------------------------------
1 1 01-01-11
1 2 05-03-11
2 1 08-01-11
2 5 18-06-11
--------------------------------------
I want only unique values of ID column from this table with date sorted ascending or descending. how can i achieve this. Here is sample output
------------------------------------
ID CID Cdt_DT (dd-MM-yy)
-------------------------------------
1 2 05-03-11
2 5 18-06-11
-------------------------------------
Thanks in advance |
Edited by - shailesh18 on 10/01/2011 04:32:41
|
|
|
sunitabeck
Flowing Fount of Yak Knowledge
5152 Posts |
Posted - 10/01/2011 : 08:07:00
|
Using row_number function would perhaps be the simplest:;WITH cte AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY cdt_DT DESC) RN
FROM
FE
)
SELECT ID, CID, Cdt_DT FROM cte WHERE RN = 1; |
 |
|
|
paultech
Yak Posting Veteran
Egypt
79 Posts |
Posted - 10/02/2011 : 07:28:41
|
hi ,
for my understanding , you want unique values of colum ID and CID
you can try :
SELECT DISTINCT ID, CID FROM tablename
|
Edited by - paultech on 12/14/2011 16:48:47 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47188 Posts |
Posted - 10/02/2011 : 07:43:32
|
quote: Originally posted by paultech
hi ,
for my understanding , you want unique values of colum ID and CID
you can try :
SELECT DISTINCT ID, CID FROM tablename
good luck,
paul Tech
Can you please test it before you post something? how do you think above suggestion will give posted output for OP?
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
vmvadivel
Yak Posting Veteran
India
69 Posts |
Posted - 10/02/2011 : 12:16:44
|
I just tweaked Paul's response.
SELECT DISTINCT ID AS [ID], MAX(cid) AS [CID], CONVERT(VARCHAR(10), MAX(cdt_dt), 105) AS [Cdt_DT (dd-MM-yy)] FROM FE GROUP BY ID GO
Best Regards Vadivel
http://vadivel.blogspot.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47188 Posts |
Posted - 10/02/2011 : 13:41:54
|
quote: Originally posted by vmvadivel
I just tweaked Paul's response.
SELECT DISTINCT ID AS [ID], MAX(cid) AS [CID], CONVERT(VARCHAR(10), MAX(cdt_dt), 105) AS [Cdt_DT (dd-MM-yy)] FROM FE GROUP BY ID GO
Best Regards Vadivel
http://vadivel.blogspot.com
this will work for sample data posted But OP should see if date values go on increasing trend as per CID value else this wont work.
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
vmvadivel
Yak Posting Veteran
India
69 Posts |
Posted - 10/02/2011 : 21:52:03
|
Yep I just tweaked his response to fit the sample data provided. As his query in current form wouldn't show up anything. BTW what does OP mean?
Best Regards Vadivel
http://vadivel.blogspot.com |
 |
|
|
khtan
In (Som, Ni, Yak)
Singapore
16746 Posts |
Posted - 10/02/2011 : 22:24:05
|
OP = Original Poster
KH Time is always against us
|
 |
|
|
vmvadivel
Yak Posting Veteran
India
69 Posts |
|
|
shailesh18
Starting Member
India
8 Posts |
Posted - 10/03/2011 : 04:04:02
|
Hi sunita thats the perfect solution.
quote: Originally posted by sunitabeck
Using row_number function would perhaps be the simplest:;WITH cte AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY cdt_DT DESC) RN
FROM
FE
)
SELECT ID, CID, Cdt_DT FROM cte WHERE RN = 1;
|
 |
|
|
shailesh18
Starting Member
India
8 Posts |
Posted - 10/03/2011 : 04:04:32
|
Thanks everybody for all your efforts
|
 |
|
| |
Topic  |
|