| Author |
Topic |
|
xwarrior
Starting Member
7 Posts |
Posted - 2006-12-06 : 22:30:45
|
| i have 4 fields, TagID, x,y,Tdate. tag1, 123,523, datetag1, 123, 123, latest datetag2, 434,423, latest datetag2, 123,432, datetag3,342, 543,datetag3,532,342,latest datein the TagID field i have different names. i want to use a query statement to draw the x and Y of each different TagID by the latest TDate.this is my query statement Code:SELECT DISTINCT TagID,x,y from RTLOC where TDate =(SELECT max(TDate) from RTLOC)the result given back is only 1 TagID.expected result should be TagID , x , y , TDatetag1 ,123 ,123 ,latest datetag2 ,434 ,423 ,latest datetag3 ,532, 342 , latest datecan this be done? thanks for your help. |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-12-07 : 00:04:51
|
Something like this?SELECT TagID,x,y,max(TDate) as TDatefrom RTLOCGroup by TagID,x,y Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
xwarrior
Starting Member
7 Posts |
Posted - 2006-12-07 : 00:26:09
|
| it should be something like this SELECT DISTINCT TagID,x,y from RTLOC where TDate =(SELECT max(my.TDate) from RTLOC as myand TagID = my.TagID)but this is in MYSql format. anybody can change it to sql server 2005 for me? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-07 : 01:45:48
|
This should do itSELECT DISTINCT q.TagID, q.x, q.y, q.TDatefrom RTLOC AS qwhere q.TDate = (SELECT MAX(w.TDate) from RTLOC AS w) Peter LarssonHelsingborg, Sweden |
 |
|
|
xwarrior
Starting Member
7 Posts |
Posted - 2006-12-07 : 01:52:46
|
| the result only me 1 tag. i need the result to display the different tags' x and y based on their latest date. |
 |
|
|
xwarrior
Starting Member
7 Posts |
Posted - 2006-12-07 : 01:53:09
|
| the result only return me 1 tag. i need the result to display the different tags' x and y based on their latest date. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-07 : 01:59:47
|
Unless you give us some PROPER sample data for dates, all we can give is suggestions based on advanced GUESSES!Do the datetime column has time information too?Try thisSELECT DISTINCT q.TagID, q.x, q.y, q.TDatefrom RTLOC AS qwhere q.TDate >= (SELECT MAX(DATEADD(day, DATEDIFF(day, 0, w.TDate), 0)) from RTLOC AS w) Peter LarssonHelsingborg, Sweden |
 |
|
|
xwarrior
Starting Member
7 Posts |
Posted - 2006-12-07 : 02:09:31
|
| my TDate is in this format--> 11/28/2006 10:04:34 AM.distinct doesn't seem to be working |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-07 : 02:32:22
|
| [code]SELECT r.TagID, r.x, r.y, r.TDateFROM ( SELECT TagID, MAX(TDate) peso FROM RTLOC GROUP BY TagID ) dINNER JOIN RTLOC r ON r.TagID = d.TagID AND r.TDate = d.peso[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
xwarrior
Starting Member
7 Posts |
Posted - 2006-12-07 : 02:50:18
|
| there is a syntax errror( missing operator in query expressin MAX(TDate) peso |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-12-07 : 02:55:12
|
| Are you doing this from MS-Access?Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-07 : 02:58:57
|
Not if you are using SQL Server!SELECT r.TagID, r.x, r.y, r.TDateFROM ( SELECT TagID, MAX(TDate) AS peso FROM RTLOC GROUP BY TagID ) dINNER JOIN RTLOC r ON r.TagID = d.TagID AND r.TDate = d.peso Peter LarssonHelsingborg, Sweden |
 |
|
|
xwarrior
Starting Member
7 Posts |
Posted - 2006-12-07 : 03:19:22
|
thanks. it is solved now. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-07 : 03:38:01
|
| Which DBMS are you using?Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-12-07 : 05:07:35
|
| It is always preferrable to use AS to name alias nameMadhivananFailing to plan is Planning to fail |
 |
|
|
|