| Author |
Topic |
|
daman
Yak Posting Veteran
72 Posts |
Posted - 2007-12-17 : 21:13:27
|
| I want to get one row record (the latest record of an ID) and the ID is from an ID list.declare @temp table (id int)insert into @tempselect 1330 union allselect 1336 union allselect 1367 Now i want to runselect top 1 value from Table A, @temp T where A.id = T.idThis will get me 1 value back. I think I need to use A.id in T.id but it will give me syntax errorI want to get one row for each Id in the @temp table |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-12-17 : 21:22:02
|
I am not sure why you need the temp table..You can just pull the records using an aggregate query...what is the structure of the source table (Table A?) Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
daman
Yak Posting Veteran
72 Posts |
Posted - 2007-12-17 : 21:33:57
|
| thanks for the quick reply.Table A structure isId Value TimeAn id may have different values and I want to get the latest value (max time)Since table A has many id and i only want to get a subset of that id, one per id.Thanks and looking forward to your help |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-18 : 00:24:15
|
| SELECT t.Value FROM(SELECT ROW_NUMBER() OVER (PARTITION BY t1.ID ORDER BY t1.Time DESC) AS 'RowNo',t1.Value FROM TableA t1INNER JOIN @temp t2ON t1.Id=t2.Id) tWHERE t.RowNo=1 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-18 : 03:29:09
|
select top 1 a.value from TableA as ainner join @temp as T on A.id = T.idorder by a.value desc E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-18 : 03:38:42
|
quote: Originally posted by Peso select top 1 a.value from TableA as ainner join @temp as T on A.id = T.idorder by a.value desc E 12°55'05.25"N 56°04'39.16"
orselect max(a.value) as value from TableA as ainner join @temp as T on A.id = T.idMadhivananFailing to plan is Planning to fail |
 |
|
|
daman
Yak Posting Veteran
72 Posts |
Posted - 2007-12-18 : 09:57:06
|
quote: Originally posted by visakh16 SELECT t.Value FROM(SELECT ROW_NUMBER() OVER (PARTITION BY t1.ID ORDER BY t1.Time DESC) AS 'RowNo',t1.Value FROM TableA t1INNER JOIN @temp t2ON t1.Id=t2.Id) tWHERE t.RowNo=1
This works great. Thanks a million.I just found out that I need to get this working on both SQL 2005 and 2003.Do you know the equivalent of this in 2003 since it has no ROW_NUMBER |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-18 : 10:01:04
|
| select A.id,max(a.value) as value from TableA as ainner join @temp as T on A.id = T.idGroup by A.idorCan you post some sample data with expected result?MadhivananFailing to plan is Planning to fail |
 |
|
|
daman
Yak Posting Veteran
72 Posts |
Posted - 2007-12-18 : 10:13:32
|
| Madhivanan,Your is very close to what I want...I want to use the Max() to filter out but I don't want to select itselect A.id,max(a.Time) as latest, a.valuefrom Table A as ainner join @temp as T on A.id = T.idGroup by A.id, A.valueHere is the sample result1861 1900-01-01 13:57:00 512131 1900-01-01 11:36:00 54.252456 1900-01-01 11:36:00 54.252650 1900-01-01 13:31:00 551420 1900-01-01 14:14:00 561367 1900-01-01 14:15:00 572812 1900-01-01 14:14:00 59.5I like to display only the id and the value column only...THANKS |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2007-12-18 : 10:18:10
|
So just do:select latest, valuefrom (select A.id,max(a.Time) as latest, a.valuefrom Table A as ainner join @temp as T on A.id = T.idGroup by A.id, A.value) a |
 |
|
|
daman
Yak Posting Veteran
72 Posts |
Posted - 2007-12-18 : 10:43:39
|
| guys,Only the one using ROW_NUMBER return one per Id.The other suggestions return multiple row per id.1330 63 1900-01-01 08:11:001330 64 1900-01-01 09:29:001330 64.5 1900-01-01 11:18:001330 65 1900-01-01 09:31:001330 67 1900-01-01 11:23:001336 55.25 1900-01-01 08:15:001336 56.25 1900-01-01 08:10:001336 56.5 1900-01-01 09:47:001336 57 1900-01-01 16:02:001336 57.5 1900-01-01 10:41:001336 58 1900-01-01 08:36:00 |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2007-12-18 : 11:01:08
|
using the data from above:declare @temp table (id int, value decimal(15,2), Time datetime)insert into @tempselect 1330, 63, '1900-01-01 08:11:00'union select 1330, 64, '1900-01-01 09:29:00'union select 1330, 64.5, '1900-01-01 11:18:00'union select 1330, 65, '1900-01-01 09:31:00'union select 1330, 67, '1900-01-01 11:23:00'union select 1336, 55.25, '1900-01-01 08:15:00'union select 1336, 56.25, '1900-01-01 08:10:00'union select 1336, 56.5, '1900-01-01 09:47:00'union select 1336, 57, '1900-01-01 16:02:00'union select 1336, 57.5, '1900-01-01 10:41:00'union select 1336, 58, '1900-01-01 08:36:00'select a.latest, b.valuefrom (select id,max(Time) as latestfrom @temp as aGroup by id ) ainner join @temp bon a.id = b.idand a.latest = b.time |
 |
|
|
daman
Yak Posting Veteran
72 Posts |
Posted - 2007-12-18 : 11:27:55
|
| RICKDDDDDDWe are almost there...there is an instance where an ID has exactly the 2 same MAX(Time)...this result in duplicate.1367 51 1900-01-01 16:00:00.0001367 52 1900-01-01 16:00:00.000 |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2007-12-18 : 11:30:53
|
| How would you know which value you wanted in this situation? |
 |
|
|
daman
Yak Posting Veteran
72 Posts |
Posted - 2007-12-18 : 15:33:19
|
quote: Originally posted by RickD How would you know which value you wanted in this situation?
RickD,Assume we have another column say like TimeId. If an id have 2 of the same time, then we select the one with larger TimeId.Thanks |
 |
|
|
daman
Yak Posting Veteran
72 Posts |
Posted - 2007-12-18 : 16:56:07
|
| HERE is the table. I hope someone can help. The number I like to get out is1330 71.25 14942818 1900-01-01 11:23:001460 86 15020856 1900-01-01 12:55:00From the data belowID Value TimeID Time1330 71.25 14942818 1900-01-01 11:23:001330 68.75 14942692 1900-01-01 11:18:001330 71.25 14942536 1900-01-01 11:14:001330 70 14940174 1900-01-01 09:31:001330 68.75 14940044 1900-01-01 09:29:001330 67.5 14937387 1900-01-01 08:11:001330 66.5 15085262 1900-01-01 08:11:001330 67.5 14937127 1900-01-01 08:10:001460 85.5 15020088 1900-01-01 12:55:001460 86 15020856 1900-01-01 12:55:001460 81 15015558 1900-01-01 11:32:001460 85.5 15014212 1900-01-01 09:55:001460 81 15013658 1900-01-01 09:41:001460 81 15013363 1900-01-01 09:39:00 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-18 : 23:57:35
|
SELECT r.ID,r.ValueFROM TableA rINNER JOIN ( SELECT t.ID,MAX(t.TimeID) AS 'MaxTimeID' FROM TableA t INNER JOIN ( SELECT ID,MAX(Time) AS 'MaxTime' FROM TableA GROUP BY ID )tmp ON tmp.ID=t.ID AND tmp.MaxTime=t.Time GROUP BY t.ID)tmaxON tmax.ID=r.IDAND tmax.MaxTimeID=r.TimeID |
 |
|
|
daman
Yak Posting Veteran
72 Posts |
Posted - 2007-12-20 : 08:45:51
|
| That's it, Visakh16Now, a variation of this.Instead of the Time column, we have DateTime column which include date and time.2007-12-12 11:23:002007-12-12 13:33:20Now, how would we go about select one per ID for a specific date, say 2007-12-12...if there is no value for that specific date, then find the last date prior to 2007-12-12And for a certain date, an id has many values, we choose the one with the max(TimeID)You guys have been phenomenal. |
 |
|
|
|