Wednesday, 15 June 2011

sql server - SQL to insert into table variable from another table with aggregate function? -


My source table has a table with several thousands of records, the primary key is the column [data_serial_num].

  Select the L000 [Data_serial_num], [Foreign_serial_num], [Msg_id], [Data_value], [timestamp], [A], [B] [I thought I [[Snyukt_siriyl_anyuem]] of'll create a table variable to maintain a unique compound key with [Msg_id] which [data_value] and reflect recent times I of [timestamp].  
  announced @filtered_data table (foreign_serial_num bigint NOT NULL, Msg_id not clear, data_value bigint bigInt, timestamp datetimeoffset (7) primary key (vehicle_serial_num, msg_id))  

table to insert variables I thought I'd do something like:

  enter filtered_data foreign_serial_num, msg_id, data_value, max (timestamp) select timestamp by 'External_serial_num From external_data group, msg_id  

does not work in conjunction with [data_value] being Should be grouped by section, but it does not return unique records.

This does work is down but I do not know how to get [data_value] columns and data in the table variable.

  filtered_data table announced (foreign_serial_num Bigint blank No Msg_id empty no timestamp datetimeoffset (7) primary key (Vehicle_serial_num, Msg_id) BigInt) Filtered_data selection foreign_serial_num, putting Msg_id, External_data group foreign_serial_num Maximum timestamp 'timestamp', msg_id selection * filtered_data   

sample from expected product:

  foreign_serial_num | Msg_id | Data_value | Timestamp 20 | 1 | 0 | 2015-01-14 16: 02: 47.0000000 -08: 00 20 | 2 | 45 2015-01-14 16: 02: 47.0000000 -08: 00 20 | 3 | 10 | 2014-11-10 15:41 41: 36.0000000 -08: 00 155 | 1 | 0 | 2015-02-27 10: 43: 09.0000000 -08: 00 155 | 2 | 33 | 2015-02-27 10: 43: 09.0000000 -08: 00 155 | 3 | 0 | My question is, what is a good way to get [table_value] from the table [external_data] in my table? Keeping the extreme primary key [foreign_sire_name] and [msg_id] with the latest timings from [timestamp]?  

  * (external_data to RN as foreign_serial_num, msg_id, data_value, timestamp, ROW_NUMBER () (oreign_serial_num, ordered a selection of over msg_id ordered by division timestamp desc) X), where ordered.rn = 1  

No comments:

Post a Comment