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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 complex trigger!!!

Author  Topic 

limericklad1974
Starting Member

39 Posts

Posted - 2012-09-21 : 11:41:38
Hi there
I need help creating a trigger please.
I have a Person table with columns/rows as follows:
ID Firstname Lastname
1 James Smith
2 Rob James
3 Tom Ryan

I have a Session table with columns/rows as follows:
ID Date
1 2012-09-20 13:30:00
2 2012-09-21 11:30:00
3 2012-09-21 15:00:00

I have a Data table with the rows
ID SessionID PersonID Secs
1 1 1 57
2 1 2 33
3 1 3 39
4 2 1 45
5 2 2 56
6 2 3 46
7 3 1 46
8 3 2 43
9 3 3 57

So 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
Secs

I need to store the daily Mins and Secs total for each player in the RZData table

Is 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-21 : 13:22:31
Ha..Friday effect I guess


CREATE TRIGGER YourTrigger
ON Data
FOR INSERT,UPDATE
AS
BEGIN

DELETE a
FROM INSERTED i
INNER JOIN Person p
ON p.ID = i.PersonID
INNER JOIN Session s
ON s.ID = i.SessionID
INNER JOIN AggregateTable a
ON a.PersonName = p.Firstname + ' ' + p.Lastname
AND 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 TotalTime
FROM INSERTED d
INNER JOIN Person p
ON p.ID = d.PersonID
INNER JOIN Session s
ON s.ID = d.SessionID
GROUP BY p.Firstname + ' ' + p.Lastname,
s.[Date]

END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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, UPDATE
AS
BEGIN

MERGE
RZData AS Target
USING
(
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.SessionDate
WHEN 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
Go to Top of Page
   

- Advertisement -