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.
| Author |
Topic |
|
rogersql
Starting Member
3 Posts |
Posted - 2005-10-27 : 10:41:37
|
| This sounds so simple, but its driving me madSystem Microsft SQL Server 2000I need to find out how to select the highest value fields against multiples held in another field. I hope to end up with a list held in a view with unique values of case_id by the highest table_id or latest date.From this table table_id case_id event_id eventdate4930 5001 7 2005-10-27 00:00:004920 5001 7 2005-10-27 00:00:003950 5001 7 2005-10-20 00:00:001735 5001 7 2005-10-03 00:00:00362 5001 7 2005-09-19 00:00:00306 5001 7 2005-09-15 00:00:001913 5002 7 2005-10-04 00:00:001922 5003 7 2005-10-04 00:00:003737 5004 7 2005-10-19 00:00:001927 5004 7 2005-10-04 00:00:002207 5005 7 2005-10-05 00:00:001933 5006 7 2005-10-04 00:00:001939 5007 7 2005-10-04 00:00:001944 5008 7 2005-10-04 00:00:001949 5009 7 2005-10-04 00:00:00to this viewtable_id case_id event_id eventdate4930 5001 7 2005-10-27 00:00:001913 5002 7 2005-10-04 00:00:001922 5003 7 2005-10-04 00:00:003737 5004 7 2005-10-19 00:00:002207 5005 7 2005-10-05 00:00:001933 5006 7 2005-10-04 00:00:001939 5007 7 2005-10-04 00:00:001944 5008 7 2005-10-04 00:00:001949 5009 7 2005-10-04 00:00:00This morning have tried many forms of select queries using Group Order Max and sub_queriesAny help would be gratefully appreciated.Thanks Roger |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-10-27 : 10:52:00
|
| [code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(table_id int, case_id int, event_id int, eventdate datetime)GOINSERT INTO myTable99(table_id, case_id, event_id, eventdate)SELECT 4930, 5001, 7, '2005-10-27 00:00:00' UNION ALLSELECT 4920, 5001, 7, '2005-10-27 00:00:00' UNION ALLSELECT 3950, 5001, 7, '2005-10-20 00:00:00' UNION ALLSELECT 1735, 5001, 7, '2005-10-03 00:00:00' UNION ALLSELECT 362 , 5001, 7, '2005-09-19 00:00:00' UNION ALLSELECT 306 , 5001, 7, '2005-09-15 00:00:00' UNION ALLSELECT 1913, 5002, 7, '2005-10-04 00:00:00' UNION ALLSELECT 1922, 5003, 7, '2005-10-04 00:00:00' UNION ALLSELECT 3737, 5004, 7, '2005-10-19 00:00:00' UNION ALLSELECT 1927, 5004, 7, '2005-10-04 00:00:00' UNION ALLSELECT 2207, 5005, 7, '2005-10-05 00:00:00' UNION ALLSELECT 1933, 5006, 7, '2005-10-04 00:00:00' UNION ALLSELECT 1939, 5007, 7, '2005-10-04 00:00:00' UNION ALLSELECT 1944, 5008, 7, '2005-10-04 00:00:00' UNION ALLSELECT 1949, 5009, 7, '2005-10-04 00:00:00'GO SELECT * FROM myTable99 o WHERE eventdate = (SELECT MAX(eventdate) FROM myTable99 i WHERE i.case_id = o.case_id)ORDER BY case_idGOSET NOCOUNT OFFDROP TABLE myTable99GO[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
rogersql
Starting Member
3 Posts |
Posted - 2005-10-27 : 12:21:51
|
| Hi Thanks for your swift reply. It is very similar to what I was trying to do and works to some degree. However I still have a problem in that it is producing duplicate rows where the eventdate is the same. Give that we have no time stamp in eventdate is there anyway of incorporating table_id as this incremented for every new entry and therefore is an indication of age. I need the last one entered so that I have unique rows based on case_id.Thanks again for your time.Roger |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-10-27 : 12:25:04
|
| Unless you have an IDENTITY column or a date added column, how would you even know which one was the "last entered"You can't, is the short answer.The order of data in a database is meaningless....Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
rogersql
Starting Member
3 Posts |
Posted - 2005-10-27 : 12:29:34
|
| Hitable_id in my example identity column with seeding set to 1Roger |
 |
|
|
|
|
|
|
|