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 |
|
Devnull49437
Starting Member
3 Posts |
Posted - 2011-04-21 : 11:09:49
|
| Hey all I have a bunch of History Tables in my database the tables contain an exact replica of the data table with 3 extra columns action timestamps user. I want to write some stored procedures to Display the data in a certain way [old data] [ new data] so I would send in a id of the item I want history for and lets say there are 3 rows for that item I can join the rows based off the time stamp and return two rows. Original Insert | Updated First timeUpdated First time | Updated second time. But I cannot figure out how to get the date time to work correctly. how can I look at my current records date and say give me the next on in seq. any help would be much appreciated. BrianThis is close except I get 2 rows for the first record becuase there are two timestamps greater. select tbl1.*, tbl2.*from table1 as tbl1left join table2 as tbl2 on tbl1.col_c = tbl2.col_cwhere tbl1.timestamp < tbl2.timestamp and tbl1.col2_c = 117 order by tbl1.timestamp asc; |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2011-04-21 : 11:41:23
|
| SELECT * FROM (SELECT col1,col2,col3, timestamp_column FROM table1 -- Get Orignal Row from Table1WHERE col2_c = 117UNION ALLSELECT col1,col2,col3, timestamp_column FROM table2 -- Get Rows from history tableWHERE col2_c = 117 AND timestamp_column >= (SELECT timestamp_column FROM table1 WHERE col2_c = 117))ORDER BY timestamp_column--------------------------http://connectsql.blogspot.com/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-04-21 : 12:34:17
|
| If I understand the issue (big IF) then you need to write your query differently so that you aren't joining to multiple rows or, at least, not returing multiple rows. But, without knowing your data or anything it's pretty hard to offer a reasonable suggestion. Here is a link that will help you organize your question and prepare your DDL, DML and expected output:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
Devnull49437
Starting Member
3 Posts |
Posted - 2011-04-21 : 13:56:21
|
quote: Originally posted by lionofdezert SELECT * FROM (SELECT col1,col2,col3, timestamp_column FROM table1 -- Get Orignal Row from Table1WHERE col2_c = 117UNION ALLSELECT col1,col2,col3, timestamp_column FROM table2 -- Get Rows from history tableWHERE col2_c = 117 AND timestamp_column >= (SELECT timestamp_column FROM table1 WHERE col2_c = 117))ORDER BY timestamp_column--------------------------http://connectsql.blogspot.com/
You seem to be on the right track here but I can't quite get it to work. here is what I have working so far. SELECT ptpj.* FROM part_types_per_job_h as ptpj-- Get Orignal Row from Table1WHERE ptpj.part_type_per_job_c = 117UNIONSELECT ijptpj.* FROM part_types_per_job_h as ijptpj -- Get Rows from history tableWHERE ijptpj.part_type_per_job_c = 117 AND ijptpj.action_timestamp >=(SELECT distinct action_timestamp FROM part_types_per_job_h WHERE part_type_per_job_c = 117 and part_type_per_job_h_c = ijptpj.part_type_per_job_h_c)ORDER BY ptpj.action_timestampThe outer select * from ( ) produces an error and so does the order by WHen I run this query this is what I get. Row 124 117 21 101 12 2011-03-15 2011-05-10 1 NULL 0 2 12 2011-03-21 11:09:58.000Row 217 117 21 101 12 2011-03-15 2011-05-10 1 Needed verification on bottle fill heights to get 0 2 4 2011-03-30 08:13:03.000Row 322 117 21 101 12 2011-03-15 2011-05-10 1 Finished it two days ago, but it won't go away. 0 2 4 2011-03-31 15:45:55.000What I am looking for is this. ROW 124 117 21 101 12 2011-03-15 2011-05-10 1 NULL 0 2 12 2011-03-21 11:09:58.000 17 117 21 101 12 2011-03-15 2011-05-10 1 Needed verification on bottle fill heights to get 0 2 4 2011-03-30 08:13:03.000Row 217 117 21 101 12 2011-03-15 2011-05-10 1 Needed verification on bottle fill heights to get 0 2 4 2011-03-30 08:13:03.000 22 117 21 101 12 2011-03-15 2011-05-10 1 Finished it two days ago, but it won't go away. 0 2 4 2011-03-31 15:45:55.000 |
 |
|
|
Devnull49437
Starting Member
3 Posts |
Posted - 2011-04-25 : 09:00:37
|
| Okay Now I have it where I am getting the correct data the correct way however I am getting an extra row that I cannot seem to get rid of here is what I havepart_type_per_job_h_c part_type_per_job_c job_c part_type_c responsible_user_c release_date due_date is_finished notes is_deleted action_code_c action_user_c action_timestamp part_type_per_job_h_c part_type_per_job_c job_c part_type_c responsible_user_c release_date due_date is_finished notes is_deleted action_code_c action_user_c action_timestamp24 117 21 101 12 2011-03-15 2011-05-10 1 NULL 0 2 12 2011-03-21 11:09:58.000 17 117 21 101 12 2011-03-15 2011-05-10 1 Needed verification on bottle fill heights to get 0 2 4 2011-03-30 08:13:03.00017 117 21 101 12 2011-03-15 2011-05-10 1 Needed verification on bottle fill heights to get 0 2 4 2011-03-30 08:13:03.000 22 117 21 101 12 2011-03-15 2011-05-10 1 Finished it two days ago, but it won't go away. 0 2 4 2011-03-31 15:45:55.00024 117 21 101 12 2011-03-15 2011-05-10 1 NULL 0 2 12 2011-03-21 11:09:58.000 22 117 21 101 12 2011-03-15 2011-05-10 1 Finished it two days ago, but it won't go away. 0 2 4 2011-03-31 15:45:55.000This row here is not correct and not needed. 24 117 21 101 12 2011-03-15 2011-05-10 1 NULL 0 2 12 2011-03-21 11:09:58.000 22 117 21 101 12 2011-03-15 2011-05-10 1 Finished it two days ago, but it won't go away. 0 2 4 2011-03-31 15:45:55.000however I cannot do a select top(2) or anything like that because It may very depending on what I am running the history for. Any Ideas?? Here is the SPAlter PROCEDURE [dbo].[Select_old_and_Current_Ptpj_h] @part_type_per_job_c int = 117ASSELECT distinct ptpj.*, ijptpj.* FROM part_types_per_job_h as ptpj-- Get Orignal Row from History Table left join part_types_per_job_h as ijptpj -- Get Rows from history tableon ijptpj.part_type_per_job_c = @part_type_per_job_c AND ijptpj.action_timestamp >=(SELECT distinct action_timestamp FROM part_types_per_job_h WHERE part_type_per_job_c = @part_type_per_job_c and part_type_per_job_h_c = ijptpj.part_type_per_job_h_c and action_timestamp > ptpj.action_timestamp)WHERE ptpj.part_type_per_job_c = @part_type_per_job_c and ptpj.part_type_per_job_c = ijptpj.part_type_per_job_c |
 |
|
|
|
|
|
|
|