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 |
|
limericklad1974
Starting Member
39 Posts |
Posted - 2012-09-21 : 11:41:38
|
| Hi thereI need help creating a trigger please.I have a Person table with columns/rows as follows:ID Firstname Lastname 1 James Smith2 Rob James3 Tom RyanI have a Session table with columns/rows as follows:ID Date1 2012-09-20 13:30:002 2012-09-21 11:30:003 2012-09-21 15:00:00I have a Data table with the rowsID SessionID PersonID Secs1 1 1 572 1 2 333 1 3 394 2 1 455 2 2 566 2 3 467 3 1 468 3 2 439 3 3 57So basically, Person 1 did an activity for 57 secs in Session 1. Person 2 did it for 33 secs and Person 3 did it for 39 secs. They did a 2nd session and in Session 2, person 1 did it for 45 secs, person 2 for 56 secs and person 3 for 46 secs. They did a 3rd session and in Session 3, person 1 did it for 46 secs, person 2 for 43 secs and person 3 for 57 secs. Sessions 2 and 3 were on the same day, the 21st September.I have a webpage set up which adds data to the Data table.When data gets added to the Data table, I need a trigger to run, which calculates the total number of secs for that player for that day (i.e. any data for sessions that day in the data table) and then once you calculate this total (e.g. person 1 had 91 secs on 21st Sept), I need to convert it to Mins and Secs and store it in a new table called RZData.The RZData table has the following columns:ID Date PersonID Mins SecsI need to store the daily Mins and Secs total for each player in the RZData tableIs this possible!!! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-21 : 12:22:51
|
| sorry I cant see any way in your current data model to link the session to a day. Unless you've have that relationship setup its not possible to rollup (aggregate)the seconds for sessions upto daylevel.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-09-21 : 13:03:36
|
| Vishakh, The Date is on the Session table.limericklad1974, what version of SQL are you using? Also, why use a trigger? How is your data being added? Are you controlling access via stored procedures? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-21 : 13:22:31
|
Ha..Friday effect I guess  CREATE TRIGGER YourTriggerON DataFOR INSERT,UPDATEASBEGINDELETE aFROM INSERTED iINNER JOIN Person pON p.ID = i.PersonIDINNER JOIN Session sON s.ID = i.SessionIDINNER JOIN AggregateTable aON a.PersonName = p.Firstname + ' ' + p.LastnameAND a.[Date] = s.[Date]INSERT INTO AggregateTable (PersonName,[Date],TotalTime)SELECT p.Firstname + ' ' + p.Lastname,s.[Date],CONVERT(varchar(8),DATEADD(ss,SUM(d.Secs),0),108) AS TotalTimeFROM INSERTED dINNER JOIN Person pON p.ID = d.PersonIDINNER JOIN Session sON s.ID = d.SessionIDGROUP BY p.Firstname + ' ' + p.Lastname,s.[Date]END ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-09-21 : 14:03:26
|
I'm not sure if this does what you want, as I wasn't sure what the ID was for in the RZdata table, but maybe it'll get you started:CREATE TRIGGER DataAfterInsertUpdate ON Data AFTER INSERT, UPDATEASBEGINMERGE RZData AS TargetUSING ( SELECT CAST(Session.[Date] AS DATE) AS SessionDate, Inserted.PersonID, SUM(Inserted.Secs) AS Secs FROM Inserted INNER JOIN Session ON Inserted.SessionID = Session.ID GROUP BY CAST(Session.[Date] AS DATE), Inserted.PersonID ) AS Source ON Target.PersonID = Source.PersonID AND Target.[Date] = Source.SessionDateWHEN MATCHED THEN UPDATE SET Target.Mins = ((Target.Secs + Source.Secs) / 60), Target.Secs = ((Target.Secs + Source.Secs) % 60)WHEN NOT MATCHED THEN INSERT ( [Date], PersonID, Mins, Secs ) VALUES ( Source.SessionDate, Source.PersonID, Source.Secs / 60, Source.Secs % 60 );END |
 |
|
|
|
|
|
|
|