sql server programming:
I need a trigger to calculate and insert into an Action table, five action due dates (each date is a separate record with a corresponding action item) based on two Milestone dates that are inserted into a Milestone table. These Milestone dates, A and B, are inserted as separate rows in the Milestone table. Milestone A will be the calculation start date and Milestone B, the calculation end date. If the Milestone A date exists, and the Milestone B date is inserted, I need five due dates inserted into the Action table based on percentages of time between A and B. (Action 1=55%, 2=30%,3 thru 5=5% ea. for a total of 100%) I'm at a loss as to where I should start. Any help is greatly appreciated! --
Could you please post your DDL? You'll have to have a way of relating Milestone A with Milestone B, since your Milestone table would presumably have many Milestones A and B in it. -- Tom ---------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS SQL Server MVP Toronto, ON Canada https://mvp.support.microsoft.com/profile/Tom.Moreau [quoted text, click to view] "FinnGirl" <FinnGirl@discussions.microsoft.com> wrote in message news:CC385D3C-7481-4F54-B857-39D63A5926F3@microsoft.com...
I need a trigger to calculate and insert into an Action table, five action due dates (each date is a separate record with a corresponding action item) based on two Milestone dates that are inserted into a Milestone table. These Milestone dates, A and B, are inserted as separate rows in the Milestone table. Milestone A will be the calculation start date and Milestone B, the calculation end date. If the Milestone A date exists, and the Milestone B date is inserted, I need five due dates inserted into the Action table based on percentages of time between A and B. (Action 1=55%, 2=30%,3 thru 5=5% ea. for a total of 100%) I'm at a loss as to where I should start. Any help is greatly appreciated! -- Finn Girl
[quoted text, click to view] >> A and B, are inserted as separate rows in the Milestone table. Milestone A will be the calculation start date and Milestone B, the calculation end date. <<
That makes no sense. The start and end dates are an atomic data element -- the duration is part of the same fact. You just have to model the fact with two columns in SQL [quoted text, click to view] >> I need five due dates inserted into the Action table based on percentages of time between A and B. (Action 1=55%, 2=30%,3 thru 5=5% each for a total of 100%) <<
Those are calculations so they should be in a VIEW and not a base table But you did not tell us how % is computed against time, the rounding errors allowed, etc.
I've got an example, maybe you could hack the bits you need out for your trigger? -- Create percentages table; just for the demo DECLARE @action_percentages TABLE( action_number INT PRIMARY KEY, action_percentage DECIMAL( 3, 2 ) NOT NULL CHECK ( action_percentage < 1 ) ) SET NOCOUNT ON INSERT INTO @action_percentages VALUES( 1, .55 ) INSERT INTO @action_percentages VALUES( 2, .30 ) INSERT INTO @action_percentages VALUES( 3, .05 ) INSERT INTO @action_percentages VALUES( 4, .05 ) INSERT INTO @action_percentages VALUES( 5, .05 ) SET NOCOUNT OFF -- Work out the difference between the two dates, then multiply that by the percentages DECLARE @start_date DATETIME, @end_date DATETIME DECLARE @diff INT -- Initialise the dates SELECT @start_date = '20070101', @end_date = '20071231' -- Work out the number of days between the two dates SET @diff = DATEDIFF( d, @start_date, @end_date ) -- Apply the percentages to the dates SELECT DATEADD( day, @diff * x.running_total, @start_date ) FROM ( -- Create running total of percentages SELECT a.action_number, SUM( b.action_percentage ) AS running_total FROM @action_percentages a CROSS JOIN @action_percentages b WHERE a.action_number >= b.action_number GROUP BY a.action_number ) x GO Hope that makes sense. Let me know how you get on. wBob Rate the post if it's right! [quoted text, click to view] "FinnGirl" wrote: > I need a trigger to calculate and insert into an Action table, five action > due dates (each date is a separate record with a corresponding action item) > based on two Milestone dates that are inserted into a Milestone table. These > Milestone dates, A and B, are inserted as separate rows in the Milestone > table. Milestone A will be the calculation start date and Milestone B, the > calculation end date. If the Milestone A date exists, and the Milestone B > date is inserted, I need five due dates inserted into the Action table based > on percentages of time between A and B. (Action 1=55%, 2=30%,3 thru 5=5% ea. > for a total of 100%) I'm at a loss as to where I should start. Any help is > greatly appreciated! > --
FinnGirl (FinnGirl@discussions.microsoft.com) writes: [quoted text, click to view] > I need a trigger to calculate and insert into an Action table, five > action due dates (each date is a separate record with a corresponding > action item) based on two Milestone dates that are inserted into a > Milestone table. These Milestone dates, A and B, are inserted as > separate rows in the Milestone table. Milestone A will be the > calculation start date and Milestone B, the calculation end date. If > the Milestone A date exists, and the Milestone B date is inserted, I > need five due dates inserted into the Action table based on percentages > of time between A and B. (Action 1=55%, 2=30%,3 thru 5=5% ea. for a > total of 100%) I'm at a loss as to where I should start. Any help is > greatly appreciated!
Since I don't know your tables, I will have to guess a bit. But this should give you the gist of it: CREATE TRIGGER milstone_tri ON milestones FOR INSERT, UPDATE AS INSERT actions (milestoneid, day1, day2, day3, day4, day5) SELECT a.milestoneid, dateadd(DAY, 0.55*datediff(DAY, a.date, b.date), a.date), dateadd(DAY, 0.85*datediff(DAY, a.date, b.date), a.date), dateadd(DAY, 0.90*datediff(DAY, a.date, b.date), a.date), dateadd(DAY, 0.95*datediff(DAY, a.date, b.date), a.date), b.date FROM milestones a JOIN milestones b ON a.milestoneid = b.milestoneid WHERE a.date IS NOT NULL AND b.date IS NOT NULL AND EXISTS (SELECT * FROM inserted i WHERE a.miltestoneid = i.milestoneid) You probably have to consider how rounding should strike here. -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at
Correct. The records are related through the "GeneralMasterID" column. Thanks. Table - GeneralActions Column Data Type GeneralActionID int (PK) GeneralMasterID int (FK) Action varchar(30) DueDate datetime Table - GeneralMilestones Column Data Type GeneralMilestoneID int (PK) GeneralMasterID int (FK) Milestone varchar(20) MilestoneDate datetime Table - GeneralMasters Column DataType GeneralMasterID int (PK) .... -- Finn Girl [quoted text, click to view] "Tom Moreau" wrote: > Could you please post your DDL? You'll have to have a way of relating > Milestone A with Milestone B, since your Milestone table would presumably > have many Milestones A and B in it. > > -- > Tom > > ---------------------------------------------------- > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS > SQL Server MVP > Toronto, ON Canada > https://mvp.support.microsoft.com/profile/Tom.Moreau > > > "FinnGirl" <FinnGirl@discussions.microsoft.com> wrote in message > news:CC385D3C-7481-4F54-B857-39D63A5926F3@microsoft.com... > I need a trigger to calculate and insert into an Action table, five action > due dates (each date is a separate record with a corresponding action item) > based on two Milestone dates that are inserted into a Milestone table. > These > Milestone dates, A and B, are inserted as separate rows in the Milestone > table. Milestone A will be the calculation start date and Milestone B, the > calculation end date. If the Milestone A date exists, and the Milestone B > date is inserted, I need five due dates inserted into the Action table based > on percentages of time between A and B. (Action 1=55%, 2=30%,3 thru 5=5% > ea. > for a total of 100%) I'm at a loss as to where I should start. Any help is > greatly appreciated! > -- > Finn Girl >
So it would appear that the following might be a solution: create trigger tri_GeneralMilestones on GeneralMilestones after insert as if @@ROWCOUNT = 0 return insert GeneralActions select i.GeneralMilestoneID , i.GeneralMasterID , 'Action ' + str (x.Id) , dateadd (dd, pct * datediff (dd, m.MilestoneDate, i.MilestoneDate) / 100.0, m.MilestoneDate) from inserted i join GeneralMilestones m on m.GeneralMasterID = i.GeneralMasterID cross join ( select 1, 55 union all select 2, 30 union all select 3, 5 union all select 4, 5 union all select 5, 5 ) as x (Id, pct) where i.Milestone = 'Milestone B' and m.Milestone = 'Milestone A' go That said, I think the percentages of time need to be rethought out. I think you gave us only the deltas. Therefore, I believe the following is the correct approach: create trigger tri_GeneralMilestones on GeneralMilestones after insert as if @@ROWCOUNT = 0 return insert GeneralActions select i.GeneralMilestoneID , i.GeneralMasterID , 'Action ' + str (x.Id) , dateadd (dd, pct * datediff (dd, m.MilestoneDate, i.MilestoneDate) / 100.0, m.MilestoneDate) from inserted i join GeneralMilestones m on m.GeneralMasterID = i.GeneralMasterID cross join ( select 1, 55 union all select 2, 85 union all select 3, 90 union all select 4, 95 union all select 5, 100 ) as x (Id, pct) where i.Milestone = 'Milestone B' and m.Milestone = 'Milestone A' go -- Tom ---------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS SQL Server MVP Toronto, ON Canada https://mvp.support.microsoft.com/profile/Tom.Moreau [quoted text, click to view] "FinnGirl" <FinnGirl@discussions.microsoft.com> wrote in message news:30C00FE7-A73B-4B75-B3A2-C1564BBD7E09@microsoft.com...
Correct. The records are related through the "GeneralMasterID" column. Thanks. Table - GeneralActions Column Data Type GeneralActionID int (PK) GeneralMasterID int (FK) Action varchar(30) DueDate datetime Table - GeneralMilestones Column Data Type GeneralMilestoneID int (PK) GeneralMasterID int (FK) Milestone varchar(20) MilestoneDate datetime Table - GeneralMasters Column DataType GeneralMasterID int (PK) .... -- Finn Girl [quoted text, click to view] "Tom Moreau" wrote: > Could you please post your DDL? You'll have to have a way of relating > Milestone A with Milestone B, since your Milestone table would presumably > have many Milestones A and B in it. > > -- > Tom > > ---------------------------------------------------- > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS > SQL Server MVP > Toronto, ON Canada > https://mvp.support.microsoft.com/profile/Tom.Moreau > > > "FinnGirl" <FinnGirl@discussions.microsoft.com> wrote in message > news:CC385D3C-7481-4F54-B857-39D63A5926F3@microsoft.com... > I need a trigger to calculate and insert into an Action table, five action > due dates (each date is a separate record with a corresponding action > item) > based on two Milestone dates that are inserted into a Milestone table. > These > Milestone dates, A and B, are inserted as separate rows in the Milestone > table. Milestone A will be the calculation start date and Milestone B, > the > calculation end date. If the Milestone A date exists, and the Milestone B > date is inserted, I need five due dates inserted into the Action table > based > on percentages of time between A and B. (Action 1=55%, 2=30%,3 thru 5=5% > ea. > for a total of 100%) I'm at a loss as to where I should start. Any help > is > greatly appreciated! > -- > Finn Girl > >
Don't see what you're looking for? Try a search.
|