SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL quer
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

shailesh18
Starting Member

India
8 Posts

Posted - 10/01/2011 :  04:26:07  Show Profile  Reply with Quote
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

5155 Posts

Posted - 10/01/2011 :  08:07:00  Show Profile  Reply with Quote
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;
Go to Top of Page

paultech
Yak Posting Veteran

Egypt
79 Posts

Posted - 10/02/2011 :  07:28:41  Show Profile  Reply with Quote
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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/02/2011 :  07:43:32  Show Profile  Reply with Quote
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/

Go to Top of Page

vmvadivel
Yak Posting Veteran

India
69 Posts

Posted - 10/02/2011 :  12:16:44  Show Profile  Visit vmvadivel's Homepage  Reply with Quote
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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/02/2011 :  13:41:54  Show Profile  Reply with Quote
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/

Go to Top of Page

vmvadivel
Yak Posting Veteran

India
69 Posts

Posted - 10/02/2011 :  21:52:03  Show Profile  Visit vmvadivel's Homepage  Reply with Quote
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
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17642 Posts

Posted - 10/02/2011 :  22:24:05  Show Profile  Reply with Quote
OP = Original Poster


KH
Time is always against us

Go to Top of Page

vmvadivel
Yak Posting Veteran

India
69 Posts

Posted - 10/03/2011 :  03:09:02  Show Profile  Visit vmvadivel's Homepage  Reply with Quote
Thanks Khtan .. I learned something today :)

Best Regards
Vadivel

http://vadivel.blogspot.com
Go to Top of Page

shailesh18
Starting Member

India
8 Posts

Posted - 10/03/2011 :  04:04:02  Show Profile  Reply with Quote
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;


Go to Top of Page

shailesh18
Starting Member

India
8 Posts

Posted - 10/03/2011 :  04:04:32  Show Profile  Reply with Quote
Thanks everybody for all your efforts
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000