| Author |
Topic |
|
krissyM2
Starting Member
4 Posts |
Posted - 2009-10-07 : 10:58:21
|
| Hi,I'm completly new on writing stored procedures so i need some help. I need to select the data from a table. Let's give you an example:Name Group DateKris 1 1-7-2009Eric 2 10-21-2009Lisa 1 9-20-2009Emy 2 8-25-2009I want to pick out one of each group, i.e. the one with the latest date!! Appreciate som help asap!Thanks/Kris |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-10-07 : 11:05:13
|
HiCREATE TABLE #TEMP (NAME VARCHAR(10), GR_ID INT , DATE DATETIME)INSERT INTO #TEMP SELECT 'Kris', 1, '1-7-2009' UNION ALLSELECT 'Eric' ,2, '10-21-2009' UNION ALLSELECT 'Lisa' ,1, '9-20-2009' UNION ALLSELECT 'Emy' ,2 ,'8-25-2009'SELECT MAX(DATE) FROM #TEMP GROUP BY GR_IDDROP TABLE #TEMP -------------------------R... |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-10-07 : 11:06:09
|
how about this?select * from(select * , row_number() over(partition by [Group] order by [Date] desc) as seqfrom <table> ) twhere t.seq = 1 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-10-07 : 11:11:06
|
| [code]SET NOCOUNT ONGOCREATE TABLE #t99 ([Name] varchar(10), [Group] int, [Date] datetime)GOINSERT INTO #t99 ([Name], [Group], [Date])SELECT 'Kris', 1, '1-7-2009' UNION ALLSELECT 'Eric', 2, '10-21-2009' UNION ALLSELECT 'Lisa', 1, '9-20-2009' UNION ALLSELECT 'Emy', 2, '8-25-2009'GOSELECT * FROM #t99 o WHERE EXISTS ( SELECT [Group] FROM #t99 i WHERE o.[Group] = i.[Group] GROUP BY i.[Group] HAVING MAX(i.[Date]) = o.[Date])GODROP TABLE #t99GOSET NOCOUNT ONGO[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
krissyM2
Starting Member
4 Posts |
Posted - 2009-10-07 : 11:32:58
|
| Hi,Thanks all for quick response. But I don't understand - the table is about 1000 records, how could I list them all when creating the temorary table??Or what am i missunderstanding?/kris |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-10-07 : 11:33:56
|
It's just sample data...Just use my query and put the table name you are using in place of #t99 and run itFor example...the output of my query isName Group Date---------- ----------- -----------------------Eric 2 2009-10-21 00:00:00.000Lisa 1 2009-09-20 00:00:00.000 Would that be correct?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-10-07 : 11:35:30
|
HIU can use your table instead of temporary table.....For exampleSELECT * FROM <Your_table> o WHERE EXISTS ( SELECT [Group] FROM <your_table> i WHERE o.[Group] = i.[Group] GROUP BY i.[Group] HAVING MAX(i.[Date]) = o.[Date]) -------------------------R... |
 |
|
|
krissyM2
Starting Member
4 Posts |
Posted - 2009-10-08 : 03:20:10
|
| It worked perfectly!!! I should have understood that you created a table as sample data :-) Well, well. Thanks!! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-08 : 04:06:19
|
| http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
|