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
 General SQL Server Forums
 New to SQL Server Programming
 Deriving counts and maximum dates
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jj735301
Starting Member

3 Posts

Posted - 03/28/2012 :  16:22:36  Show Profile  Reply with Quote
Hello,

I appoligize for hijacking a forum topic I thought related to this one.
I appreciate your expertise and time too.
Can you please help me derive results from:

id|entry id
x|12
x|10
x|9
x|8
t|11
t|10
t|9

Query result will produce counts of id entries, id, latest entry, next to latest entry. This would be the actual result from above.
4|x|12|10
3|t|11|10

Hope that makes sense

Thank you

visakh16
Very Important crosS Applying yaK Herder

India
47145 Posts

Posted - 03/28/2012 :  16:29:46  Show Profile  Reply with Quote

SELECT COUNT(*) AS cnt,
id,
MAX(CASE WHEN Rn=1 THEN entryid END) AS Latest,
MAX(CASE WHEN Rn=2 THEN entryid END) AS NextLatest
FROM 
(SELECT *,ROW_NUMBER() OVER (PARTITION BY id ORDER BY entry_Id DESC) AS Rn
FROM table
)t
GROUP BY id


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jj735301
Starting Member

3 Posts

Posted - 03/29/2012 :  14:49:22  Show Profile  Reply with Quote
Ha! Awesome work! You guys are the best!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47145 Posts

Posted - 03/29/2012 :  14:58:57  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.05 seconds. Powered By: Snitz Forums 2000