Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 First row in group function

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2011-02-18 : 16:39:59
In Access, I used First function as below to make data from 1) to 2) as below query:
SELECT ID, DATEFROM,
First(duration) AS duration
FROM TABLE1
GROUP BY MEMBID, DATEFROM

How to code in SQL to make result as 2)?

1)

ID--------DATEFROM--------DURATION

06000031--2009-07-22------13
06000031--2009-07-22------20
06000031--2010-07-22------4
06000051--2010-08-22------50
06000051--2010-08-22------5
06000051--2010-08-22------80

2)

ID--------DATEFROM--------DURATION

06000031--2009-07-22------13
06000051--2010-08-22------50

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-02-18 : 17:03:57
There's no FIRST() function in SQL Server. Row order does not matter in a true relational database. Unless you have another column you can order by that preserves the row order, you'll have to use MIN() or MAX().
Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2011-02-18 : 17:21:23
I knew there is no first function in SQL.
MIN or MAX will make the different result.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-02-18 : 17:34:25
So will FIRST(), if the table is ever reindexed. That's why physical storage order is meaningless, you can't rely on it to be consistent.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-21 : 03:42:24
Why dont you have a column with unique contraint (or an identity column) and can try these methods?
http://beyondrelational.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -