|
|
|
Forum 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?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:46 AM
Points: 6,693,
Visits: 11,705
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Forum 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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:46 AM
Points: 6,693,
Visits: 11,705
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Forum 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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:46 AM
Points: 6,693,
Visits: 11,705
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 4:43 PM
Points: 192,
Visits: 640
|
|
| 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.
|
|
|
|