Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to insert new record for a user having multiple records by performing lookup? Expand / Collapse
Author
Message
Posted Wednesday, February 20, 2013 7:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 21, 2013 6:46 AM
Points: 3, Visits: 7
Hi,

I have one table called Student. The source for loading the table is StudentHistory.

My Scenario is i have a single student having multiple records like single student mapped to several departments.
Select distinct student,Department from Student gives 10 records.
if i get 11th record from StudentHistory table it should check for student mapped to that department, if no add record to Student table.

I applied Left outer join using mergejoin task in SSIS for this scenario.
StudentHistory as left source and Student as right, when i am looking for the student & department, for matched rows also my Student table is returning null values.
so that all the 11 records are getting inserted into Student table based on condition.

What is the reason for retrieving all null values even for matched records?
Post #1422084
Posted Wednesday, February 20, 2013 8:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:23 AM
Points: 7,098, Visits: 12,605
I could take a guess at what you want and I might nail it but I might not. So in the spirit of getting you some tested code in an efficient way I would prefer you provide examples of the tables you are using in the form of CREATE TABLE statements, a set of sample data in the form of INSERT statements, the expected results based on that sample data and most importantly, what you have tried so far.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1422123
Posted Thursday, February 21, 2013 1:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 21, 2013 6:46 AM
Points: 3, Visits: 7
Hi, Thanks for the reply
here goes the example
Source Table
CREATE TABLE StudentHistory(
StudentHistoryId int IDENTITY(1,1) NOT NULL,
StudentId VARCHAR(6) NOT NULL,
DepartmentId VARCHAR(5) NOT NULL,
ProcessedMonth INT NOT NULL,
ProcessedYear INT NOT NULL,
ProcessedDate DATETIME NOT NULL,
InsertedDate DATETIME NOT NULL
CONSTRAINT PK_StudentHistory_StudentHistoryId PRIMARY KEY CLUSTERED
(
StudentHistoryId ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Considerations
1) StudentId and DepartmentId are foreign Keys
2) Every month for a year will have present active records
3) ProcessedDate is daily process time getdate
4) InsertedDate is the record first time inserted for that month in a year

DestinationTable
CREATE TABLE Student(
PrimarykeyId int IDENTITY(1,1) NOT NULL,
StudentId VARCHAR(6) NOT NULL,
DepartmentId VARCHAR(5) NOT NULL,
InsertedDate DATETIME NOT NULL,
LastUpdated DATETIME NOT NULL
CONSTRAINT PK_StudentHistory_PrimarykeyId PRIMARY KEY CLUSTERED
(
PrimarykeyId ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Consider like Student 1 is mapped to three departments in both the tables
for today history table is inserted with same student mapped to another department.
Student table lookup for student and department when left outer join is performed the matched three records are also returning null
Post #1422421
Posted Thursday, February 21, 2013 3:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:23 AM
Points: 7,098, Visits: 12,605
Thank you for the table definitions. This looks like homework, which is fine, I am happy to help, but I need for you to provide the other three things I asked for.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1422489
Posted Thursday, February 21, 2013 6:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 21, 2013 6:46 AM
Points: 3, Visits: 7
INSERT INTO StudentHistory(1,100,2,2013,GETDATE(),'2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(2,100,2,2013,GETDATE(),'2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(2,101,2,2013,GETDATE(),'2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(2,102,2,2013,GETDATE(),'2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(2,103,2,2013,GETDATE(),'2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(3,103,2,2013,GETDATE(),'2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(3,104,2,2013,GETDATE(),'2013-02-01 12:00:00 ')


INSERT INTO StudentHistory(1,100,'2013-02-01 12:00:00 ','2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(2,100,'2013-02-01 12:00:00 ','2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(2,101,'2013-02-01 12:00:00 ','2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(2,102,'2013-02-01 12:00:00 ','2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(2,103,'2013-02-01 12:00:00 ','2013-02-01 12:00:00 ')
INSERT INTO StudentHistory(3,103,'2013-02-01 12:00:00 ','2013-02-01 12:00:00 ')

Try to insert this first later build ssis to perform left outer join with StudentHistory as Left source and Student as right Source on conditions studentid and departmentid. I have used Merge join

Now INSERT INTO StudentHistory(2,104,2,2013,GETDATE(),GETDATE())

run the ssis my issue is i am doing lookup for a studentid 2 to a department while doing so i should get the below data

Source Destination
StudentId DepartmentId StudentId DepartmentId
2 100 2 100
2 101 2 101
2 102 2 102
2 103 2 103
2 104 NULL NULL


but i am receiveing


Source Destination
StudentId DepartmentId StudentId DepartmentId
2 100 NULL NULL
2 101 NULL NULL
2 102 NULL NULL
2 103 NULL NULL
2 104 NULL NULL


Could u please guide me on this
Post #1422526
Posted Thursday, February 21, 2013 6:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:23 AM
Points: 7,098, Visits: 12,605
Than you for the test data. There were some errors in it but I got it to work. Here it is for other onlookers:

INSERT  INTO dbo.StudentHistory
(StudentId, DepartmentId, ProcessedMonth, ProcessedYear, ProcessedDate, InsertedDate)
VALUES (1, 100, 2, 2013, GETDATE(), '2013-02-01 12:00:00 '),
(2, 100, 2, 2013, GETDATE(), '2013-02-01 12:00:00 '),
(2, 101, 2, 2013, GETDATE(), '2013-02-01 12:00:00 '),
(2, 102, 2, 2013, GETDATE(), '2013-02-01 12:00:00 '),
(2, 103, 2, 2013, GETDATE(), '2013-02-01 12:00:00 '),
(3, 103, 2, 2013, GETDATE(), '2013-02-01 12:00:00 '),
(3, 104, 2, 2013, GETDATE(), '2013-02-01 12:00:00 ');

INSERT INTO dbo.Student
(StudentId, DepartmentId, InsertedDate, LastUpdated)
VALUES (1, 100, '2013-02-01 12:00:00 ', '2013-02-01 12:00:00 '),
(2, 100, '2013-02-01 12:00:00 ', '2013-02-01 12:00:00 '),
(2, 101, '2013-02-01 12:00:00 ', '2013-02-01 12:00:00 '),
(2, 102, '2013-02-01 12:00:00 ', '2013-02-01 12:00:00 '),
(2, 103, '2013-02-01 12:00:00 ', '2013-02-01 12:00:00 '),
(3, 103, '2013-02-01 12:00:00 ', '2013-02-01 12:00:00 ');


I am wondering if you need to bother with any of the Join Transforms in SSIS. Are StudentHistory and Student tables on the same Database Instance, i.e. can they be joined together in a single SQL query like this to add missing rows to the Student table?

INSERT  INTO dbo.Student
(
StudentId,
DepartmentId,
InsertedDate,
LastUpdated
)
SELECT DISTINCT
sh.StudentId,
sh.DepartmentId,
GETDATE(),
GETDATE()
FROM dbo.StudentHistory sh
LEFT JOIN dbo.Student s ON sh.StudentId = s.StudentId
AND sh.DepartmentId = s.DepartmentId
WHERE s.StudentId IS NULL;



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1422535
Posted Thursday, May 16, 2013 3:32 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 3:05 PM
Points: 355, Visits: 870
So you are adding back records to the student table that you think should be there. Could it be the students table was archived somewhere else and the history table not? By reinserting the student you may be duplicating data relative to the student and (possible) student archive tables. Just a thought that occurred to me.
Post #1453799
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse