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
 how to use left join to insert missing values

Author  Topic 

insanepaul
Posting Yak Master

178 Posts

Posted - 2012-05-25 : 12:34:15
We have a job that transfers credit card data from one database to another but found that not all money was being transfered.

I've used the left join and can see 35 missing amounts but I don't know how to insert the missing values into the right hand table easily. I could do it manually but would take a long time. What would I need to do? Here is the join:

	
SELECT g.giftid, g.amount as [CORWEB GIFT],eg.amount as [Radiothon GIFT], g.date as [CORWEB Date], eg.date as [Radiothon Date]
FROM LCRVWDB2.CORWeb_SC.dbo.CORWeb_Gift G
left JOIN dbo.ExternalGift EG ON EG.DATE = G.DATE
WHERE g.Date between '05/22/2012' AND '05/27/2012'
AND g.GiftStatusID = 19

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-25 : 12:37:44
[code]
INSERT Table1 (Column1, Column2, ...)
SELECT g.giftid, g.amount as [CORWEB GIFT],eg.amount as [Radiothon GIFT], g.date as [CORWEB Date], eg.date as [Radiothon Date]
FROM LCRVWDB2.CORWeb_SC.dbo.CORWeb_Gift G
left JOIN dbo.ExternalGift EG ON EG.DATE = G.DATE
WHERE g.Date between '05/22/2012' AND '05/27/2012'
AND g.GiftStatusID = 19
[/code]

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2012-05-25 : 12:40:52
Thanks for the quick reply. I see then that i have to get just the missing rows. When i do a left join I get all the rows but how do i get just the missing rows in a select command?

quote:
Originally posted by tkizer


INSERT Table1 (Column1, Column2, ...)
SELECT g.giftid, g.amount as [CORWEB GIFT],eg.amount as [Radiothon GIFT], g.date as [CORWEB Date], eg.date as [Radiothon Date]
FROM LCRVWDB2.CORWeb_SC.dbo.CORWeb_Gift G
left JOIN dbo.ExternalGift EG ON EG.DATE = G.DATE
WHERE g.Date between '05/22/2012' AND '05/27/2012'
AND g.GiftStatusID = 19


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2012-05-25 : 12:55:59
Do I use IF EXISTS?
Also I don't know how to hardcode the 'CC' as in:
INSERT INTO	ExternalGift (Amount,GiftType,Date,GiftAid,StationID)VALUES(@dAmount,'CC',@dDate,@bGiftAid,@iStationID)

quote:
Originally posted by tkizer


INSERT Table1 (Column1, Column2, ...)
SELECT g.giftid, g.amount as [CORWEB GIFT],eg.amount as [Radiothon GIFT], g.date as [CORWEB Date], eg.date as [Radiothon Date]
FROM LCRVWDB2.CORWeb_SC.dbo.CORWeb_Gift G
left JOIN dbo.ExternalGift EG ON EG.DATE = G.DATE
WHERE g.Date between '05/22/2012' AND '05/27/2012'
AND g.GiftStatusID = 19


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-25 : 13:00:04
[code]
INSERT Table1 (Column1, Column2, ...)
SELECT g.giftid, g.amount as [CORWEB GIFT],eg.amount as [Radiothon GIFT], g.date as [CORWEB Date], eg.date as [Radiothon Date], 'CC'
FROM LCRVWDB2.CORWeb_SC.dbo.CORWeb_Gift G
left JOIN dbo.ExternalGift EG ON EG.DATE = G.DATE
WHERE g.Date between '05/22/2012' AND '05/27/2012'
AND g.GiftStatusID = 19
AND EG.DATE IS NULL
[/code]

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2012-05-25 : 13:18:33
Great thanks, that worked.
quote:
Originally posted by tkizer


INSERT Table1 (Column1, Column2, ...)
SELECT g.giftid, g.amount as [CORWEB GIFT],eg.amount as [Radiothon GIFT], g.date as [CORWEB Date], eg.date as [Radiothon Date], 'CC'
FROM LCRVWDB2.CORWeb_SC.dbo.CORWeb_Gift G
left JOIN dbo.ExternalGift EG ON EG.DATE = G.DATE
WHERE g.Date between '05/22/2012' AND '05/27/2012'
AND g.GiftStatusID = 19
AND EG.DATE IS NULL


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-25 : 13:32:00
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -