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 2008 Forums
 Transact-SQL (2008)
 Group by without aggregate
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kwikwisi
Constraint Violating Yak Guru

281 Posts

Posted - 04/29/2013 :  01:27:17  Show Profile  Reply with Quote
Query with order by datetime retrieve below records. If only first record for the same Id(first column xxx) is needed, what could be the query ? I don't think group by will work as other columns are still required and values may be different.
xxx1 15/01/2013 col1 col2 col3...
xxx1 13/01/2013 col1 col2 col3...
xxx2 13/01/2013 col1 col2 col3...

Required Result:
xxx1 15/01/2013 col1 col2 col3...
xxx2 13/01/2013 col1 col2 col3...

Thanks

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 04/29/2013 :  01:33:52  Show Profile  Reply with Quote

SELECT required columns here
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col1) AS Seq,*
FROM (your Current query)
)t
WHERE Seq=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 04/29/2013 01:34:11
Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

281 Posts

Posted - 04/29/2013 :  01:46:32  Show Profile  Reply with Quote
Hi,

Thanks for your help.

Order by clause throws error, actually this is a sub-query 'The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.'

Sorry if I'm asking stupid question, I'm weak in SQL.

quote:
Originally posted by visakh16


SELECT required columns here
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col1) AS Seq,*
FROM (your Current query)
)t
WHERE Seq=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

281 Posts

Posted - 04/29/2013 :  01:49:06  Show Profile  Reply with Quote
It works when order by is moved to PARTITION clause.

Thanks again

quote:
Originally posted by visakh16


SELECT required columns here
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col1) AS Seq,*
FROM (your Current query)
)t
WHERE Seq=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 04/29/2013 :  02:28:26  Show Profile  Reply with Quote
order has to be inside partition by construct. if you've any additional fields to order by put it in outside query after WHERE Seq=1

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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