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 |
pras2007
Posting Yak Master
216 Posts |
Posted - 2007-10-20 : 01:14:49
|
Hello All,I have a sample of the before table (show below) where I want to insert “Open” value records based on the record already given… This will have to be derived. I know how to write it mathematically but don’t have a clue on how to create it in a query if it is even possible… Below is what is needed mathematically. Please advice. Thanks.- Everything beings from the start field. Open -540 + 630 = 90Break 630 + 15 = 645Open -645 + 720 = 75Lunch 720 + 60 = 780Open -780 + 930 = 150Break 930 + 15 = 945Open -945 + 960 = 15DCOM 960 + 15 = 975Open -975 + 990 = 15ADM 990 + 13 = 1003Open -1003 + 1005 = 2Blank 1005 + 1 = 1006Open -1006 + 1006 = 0ADM 1006 + 5 = 1055Open -1011 + 1012 = 1CMT 1012 + 38 = 1050To determine if an Open record is needed, Start field + Amount filed (540 + 510 = 1050) has to be equal to the last value, if not an Open record is need.Query that generated the before table:SELECT TABLE1.CREATED_DATE, TABLE1.ID, TABLE2.LENGTH, TABLE3.EVENT, TABLE1.START, TABLE2.INTERVAL, TABLE1.AMOUNTFROM TABLE1 INNER JOIN TABLE2 ON TABLE1.SCHED_ID = TABLE2.SCHED_ID INNER JOIN TABLE3 ON TABLE2.EXC_CODE = TABLE3.EXC_IDGROUP BY TABLE1.CREATED_DATE, TABLE1.ID, TABLE2.LENGTH, TABLE3.EVENT, TABLE1.START, TABLE2.START, TABLE1.AMOUNTORDER BY TABLE2.STARTBEFORE ID CREATED_DATE LENGTH EVENT START INTERVAL AMOUNT7623 10/18/2007 15 Break 540 630 5107623 10/18/2007 60 Lunch 540 720 5107623 10/18/2007 15 Break 540 930 5107623 10/18/2007 15 DCOM 540 960 5107623 10/18/2007 13 ADM 540 990 5107623 10/18/2007 1 Blank 540 1005 5107623 10/18/2007 5 ADM 540 1006 5107623 10/18/2007 1 Blank 540 1011 5107623 10/18/2007 38 CMT 540 1012 510AFTER ID CREATED_DATE LENGTH EVENT START INTERVAL AMOUNT7623 10/18/2007 90 Open 540 5107623 10/18/2007 15 Break 540 630 5107623 10/18/2007 75 Open 540 15 5107623 10/18/2007 60 Lunch 540 720 5107623 10/18/2007 150 Open 540 60 5107623 10/18/2007 15 Break 540 930 5107623 10/18/2007 15 Open 540 15 5107623 10/18/2007 15 DCOM 540 960 5107623 10/18/2007 15 Open 540 15 5107623 10/18/2007 13 ADM 540 990 5107623 10/18/2007 2 Open 540 13 5107623 10/18/2007 1 Blank 540 1005 5107623 10/18/2007 0 Open 540 1 5107623 10/18/2007 5 ADM 540 1006 5107623 10/18/2007 Open 540 5 5107623 10/18/2007 1 Blank 540 1012 5107623 10/18/2007 Open 540 38 5107623 10/18/2007 38 CMT 540 1012 510 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-20 : 08:18:04
|
what is table1 and table2 ? can you post the table DDL and sample data from the tables ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|