﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / Business Intelligence  / How to insert new record for a user having multiple records by performing lookup? / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 10:30:11 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: How to insert new record for a user having multiple records by performing lookup?</title><link>http://www.sqlservercentral.com/Forums/Topic1422084-147-1.aspx</link><description>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.</description><pubDate>Thu, 16 May 2013 15:32:00 GMT</pubDate><dc:creator>mmartin1</dc:creator></item><item><title>RE: How to insert new record for a user having multiple records by performing lookup?</title><link>http://www.sqlservercentral.com/Forums/Topic1422084-147-1.aspx</link><description>Than you for the test data. There were some errors in it but I got it to work. Here it is for other onlookers:[code="sql"]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 ');[/code]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?[code="sql"]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;[/code]</description><pubDate>Thu, 21 Feb 2013 06:26:39 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: How to insert new record for a user having multiple records by performing lookup?</title><link>http://www.sqlservercentral.com/Forums/Topic1422084-147-1.aspx</link><description>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 dataSource			                                                      Destination	StudentId	DepartmentId		                   StudentId	DepartmentId2	             100		                                      2	1002	             101		                                      2	1012	             102		                                      2	1022	             103		                                      2	1032	             104		                                      NULL	NULLbut i am receiveingSource			                                                      Destination	StudentId	DepartmentId		                   StudentId	DepartmentId2	             100		                                      NULL	NULL2	             101		                                      NULL	NULL2	             102		                                      NULL	NULL2	             103		                                      NULL	NULL2	             104		                                      NULL	NULLCould u please guide me on this</description><pubDate>Thu, 21 Feb 2013 06:12:13 GMT</pubDate><dc:creator>deepthipriya.ravi</dc:creator></item><item><title>RE: How to insert new record for a user having multiple records by performing lookup?</title><link>http://www.sqlservercentral.com/Forums/Topic1422084-147-1.aspx</link><description>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.</description><pubDate>Thu, 21 Feb 2013 03:57:31 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: How to insert new record for a user having multiple records by performing lookup?</title><link>http://www.sqlservercentral.com/Forums/Topic1422084-147-1.aspx</link><description>Hi, Thanks for the replyhere goes the exampleSource TableCREATE 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]Considerations1) StudentId and DepartmentId are foreign Keys2) Every month for a year will have present active records 3) ProcessedDate is daily process time getdate4) InsertedDate is the record first time inserted for that month in a yearDestinationTableCREATE 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 tablesfor 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</description><pubDate>Thu, 21 Feb 2013 01:10:14 GMT</pubDate><dc:creator>deepthipriya.ravi</dc:creator></item><item><title>RE: How to insert new record for a user having multiple records by performing lookup?</title><link>http://www.sqlservercentral.com/Forums/Topic1422084-147-1.aspx</link><description>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.</description><pubDate>Wed, 20 Feb 2013 08:40:07 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>How to insert new record for a user having multiple records by performing lookup?</title><link>http://www.sqlservercentral.com/Forums/Topic1422084-147-1.aspx</link><description>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 &amp; 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?</description><pubDate>Wed, 20 Feb 2013 07:33:40 GMT</pubDate><dc:creator>deepthipriya.ravi</dc:creator></item></channel></rss>