我有两个表要用新表替换。旧表:
CREATE TABLE [dbo].[OldContacts](
[OldContactID] [int] IDENTITY(1,1) NOT NULL,
[ClientID] [int] NULL,
[BusinessID] [int] NULL,
[Title] [varchar](20) NULL,
[FirstName] [varchar](100) NULL,
[Surname] [varchar](100) NULL,
[JobTitle] [varchar](100) NULL,
[EmailAddress] [varchar](100) NULL,
[Telephone] [varchar](20) NULL,
[Mobile] [varchar](20) NULL,
[DecisionMaker] [int] NULL,
[PrimaryContact] [int] NULL,
[ContactDisabled] [int] NULL,
[ZoomInfoPersonId] [int] NULL,
[CreationDate] [datetime] NULL,
[BusinessListRecordID] [int] NULL)
CREATE TABLE [dbo].[Transaction](
[TransactionId] [int] IDENTITY(1,1) NOT NULL,
[CreditTransactionId] [int] NOT NULL,
[PersonId] [int] NOT NULL,
[CompanyId] [int] NOT NULL,
[CompanyName] [nvarchar](150) NOT NULL,
[ContactName] [nvarchar](150) NOT NULL,
[ContactEmail] [nvarchar](150) NULL,
[ContactPhone] [nvarchar](50) NULL,
[ContactJobTitle] [nvarchar](150) NULL,
[EmailBought] [bit] NULL,
[AddressLine1] [varchar](100) NULL,
[AddressLine2] [varchar](100) NULL,
[AddressLine3] [varchar](100) NULL,
[County] [varchar](100) NULL,
[Town] [varchar](100) NULL,
[Country] [varchar](100) NULL,
[Telephone] [varchar](100) NULL,
[Website] [varchar](500) NULL,
[Industry] [varchar](100) NULL,
[BusinessID] [int] NULL)
新的:
CREATE TABLE [dbo].[ClientContacts](
[ClientContactID] [int] IDENTITY(1,1) NOT NULL,
[ClientUserID] [int] NULL,
[PersonID] [int] NULL,
[BusinessID] [int] NULL,
[CompanyID] [int] NULL,
[CompanyName] [varchar](50) NULL,
[ContactName] [varchar](100) NULL,
[ContactFirstName] [varchar](50) NULL,
[ContactSurname] [varchar](50) NULL,
[ContactEmail] [varchar](50) NULL,
[ContactPhone] [varchar](50) NULL,
[ContactMobile] [varchar](50) NULL,
[ContactJobTitle] [varchar](50) NULL,
[EmailBought] [bit] NULL,
[AddressLine1] [varchar](50) NULL,
[AddressLine2] [varchar](50) NULL,
[AddressLine3] [varchar](50) NULL,
[County] [varchar](50) NULL,
[Town] [varchar](50) NULL,
[Country] [varchar](50) NULL,
[CompanyTelephone] [varchar](50) NULL,
[CompanyWebsite] [varchar](50) NULL,
[Industry] [varchar](50) NULL,
[DecisionMaker] [bit] NULL,
[PrimaryContact] [bit] NULL,
[ContactDisabled] [bit] NULL,
[CreationDate] [datetime] NULL,
[BusinessListRecordID] [int] NULL,
[IsPurchased] [bit] NULL
)
旧的逻辑是,你在Transaction
创建了一条记录,然后它会向OldContacts
添加一条记录,创建我们现在试图摆脱的冗余。
但是,您OldContacts
上的记录可能不是来自Transaction
。
现在,我需要将数据迁移到新表格,获取所有Transaction
条记录以及OldContacts
不与Transaction
重复的INSERT INTO dbo.ClientContacts
(
ClientUserID, PersonID, BusinessID, CompanyID, CompanyName, ContactName,
ContactFirstName, ContactSurname, ContactEmail, ContactPhone, ContactMobile,
ContactJobTitle, EmailBought, AddressLine1, AddressLine2, AddressLine3,
County, Town, Country, CompanyTelephone, CompanyWebsite, Industry,
DecisionMaker, PrimaryContact, ContactDisabled, CreationDate,
BusinessListRecordID, IsPurchased
)
SELECT
CT.ClientUserId,
LC.ZIPersonId,
LC.BusinessID,
T.CompanyId,
T.CompanyName,
CONCAT(LC.FirstName, ' ', LC.Surname),
LC.FirstName,
LC.Surname,
LC.EmailAddress,
LC.Telephone AS ContactPhone,
LC.Mobile AS ContactMobile,
LC.JobTitle,
T.EmailBought,
T.AddressLine1,
T.AddressLine2,
T.AddressLine3,
T.County,
T.Town,
T.Country,
T.Telephone,
T.Website,
T.Industry,
LC.DecisionMaker,
LC.PrimaryContact,
LC.ContactDisabled,
LC.CreationDate,
LC.BusinessListRecordID,
CASE LC.ZIPersonId WHEN 0 THEN 0 ELSE 1 END
FROM OldContacts LC
LEFT OUTER JOIN dbo.[Transaction] T ON LC.ZIPersonId = T.PersonId
LEFT OUTER JOIN dbo.CreditTransaction CT ON T.CreditTransactionId = CT.CreditTransactionId
条记录。这是我到目前为止所得到的:
INSERT INTO dbo.OldContacts (ClientID, BusinessID, Title, FirstName, Surname, JobTitle, EmailAddress, Telephone, Mobile, DecisionMaker, PrimaryContact, ContactDisabled, ZIPPersonId, CreationDate, BusinessListRecordID)
VALUES
(13832, 10, 'Mr', 'John55', 'Smith55', 'Admin', 'john55@smith.com', '123456', '123456', 0, 0, 0, 55, GETDATE(), NULL)
INSERT INTO dbo.OldContacts (ClientID, BusinessID, Title, FirstName, Surname, JobTitle, EmailAddress, Telephone, Mobile, DecisionMaker, PrimaryContact, ContactDisabled, ZIPPersonId, CreationDate, BusinessListRecordID)
VALUES
(13832, 10, 'Mr', 'John66', 'Smith66', 'Admin', 'john66@smith.com', '123456', '123456', 0, 0, 0, 66, GETDATE(), NULL)
INSERT INTO dbo.OldContacts (ClientID, BusinessID, Title, FirstName, Surname, JobTitle, EmailAddress, Telephone, Mobile, DecisionMaker, PrimaryContact, ContactDisabled, ZIPPersonId, CreationDate, BusinessListRecordID)
VALUES
(13832, 10, 'Mr', 'John77', 'Smith77', 'Admin', 'john77@smith.com', '123456', '123456', 0, 0, 0, 77, GETDATE(), NULL)
-- ******************************************************************************
INSERT INTO dbo.[Transaction] (CreditTransactionId, PersonId, CompanyId, CompanyName, ContactName, ContactEmail, ContactJobTitle, EmailBought, AddressLine1, AddressLine2, AddressLine3, County, Town, Country, Telephone, Website, Industry, BusinessID)
VALUES
(7965, 77, 123, 'TestCompany', 'John77 Smith77', 'john77@smith77.com', 'Admin', 1, 'AL1','AL2', 'AL3', 'testcounty', 'testtown', 'testcountry', '321456', 'www.test.com', 'someIndustry', 10)
但是,这只能得到一条记录,其中ZIPersonId匹配。如何从两个表中获取记录而不会出现重复项?
以下是一些测试记录:
OldContacts
修改
我用LEFT OUTER JOINS替换了两个连接,现在我已经拥有了所有记录。但是,我没有得到一些列:没有来自import java.io.File;
import SimpleOpenNI.*;
import java.util.*;
SimpleOpenNI kinect;
void setup()
{
size(640, 480);
kinect = new SimpleOpenNI(this);
kinect.enableDepth();
}
int precedente = millis();
void draw()
{
kinect.update();
PImage depthImage = kinect.depthImage();
image(depthImage, 0, 0);
int[] depthValues = kinect.depthMap();
StringBuilder sb = new StringBuilder();
Deque<Integer> row = new LinkedList<Integer>();
int kinectheight = 770; // kinect distance from the baselevel [mm]
int scaleFactor = 1;
int pixelsPerRow = 640;
int pixelsToSkip = 40;
int rowNum = 0;
for (int i = 0; i < depthValues.length; i++) {
if (i > 0 && i == (rowNum + 1) * pixelsPerRow) {
fillStringBuilder(sb, row);
rowNum++;
sb.append("\n");
row = new LinkedList<Integer>();
}
if (i >= (rowNum * pixelsPerRow) + pixelsToSkip) {
row.addFirst((kinectheight - depthValues[i]) * scaleFactor);
}
}
fillStringBuilder(sb, row);
String kinectDEM = sb.toString();
final String[] txt= new String[1]; //creates a string array of 2 elements
int savingtimestep = 15000; // time step in millisec between each saving
if (millis() > precedente + savingtimestep) {
txt[0] = "ncols 600\nnrows 480\nxllcorner 0\nyllcorner 0\ncellsize 91.6667\nNODATA_value 10\n" +kinectDEM;
saveStrings("kinectDEM0.tmp", txt);
precedente = millis();
// delete the old .txt file, from kinectDEM1 to kinectDEMtrash
File f = new File (sketchPath("kinectDEM1.txt"));
boolean success = f.delete();
// rename the old .txt file, from kinectDEM0 to kinectDEM1
File oldName1 = new File(sketchPath("kinectDEM0.txt"));
File newName1 = new File(sketchPath("kinectDEM1.txt"));
oldName1.renameTo(newName1);
// rename kinectDEM0.tmp file to kinectDEM0.txt
File oldName2 = new File(sketchPath("kinectDEM0.tmp"));
File newName2 = new File(sketchPath("kinectDEM0.txt"));
oldName2.renameTo(newName2);
}
}
void fillStringBuilder(StringBuilder sb, Deque<Integer> row) {
boolean emptyRow = false;
while (!emptyRow) {
Integer val = row.pollFirst();
if (val == null) {
emptyRow = true;
} else {
sb.append(val);
val = row.peekFirst();
if (val != null) {
sb.append(" ");
}
}
}
}
表的联系人的ClientUserId,CompanyId,CompanyName和isPurchased字段总是一个,当它不应该时,如果ZIPersonId不是,则它应该是1 0
答案 0 :(得分:1)
似乎你必须用LEFT JOIN替换INNER JOIN:
INSERT INTO dbo.ClientContacts
(
ClientUserID, PersonID, BusinessID, CompanyID, CompanyName, ContactName,
ContactFirstName, ContactSurname, ContactEmail, ContactPhone, ContactMobile,
ContactJobTitle, EmailBought, AddressLine1, AddressLine2, AddressLine3,
County, Town, Country, CompanyTelephone, CompanyWebsite, Industry,
DecisionMaker, PrimaryContact, ContactDisabled, CreationDate,
BusinessListRecordID, IsPurchased
)
SELECT
CT.ClientUserId,
LC.ZIPersonId,
LC.BusinessID,
T.CompanyId,
T.CompanyName,
CONCAT(LC.FirstName, ' ', LC.Surname),
LC.FirstName,
LC.Surname,
LC.EmailAddress,
LC.Telephone AS ContactPhone,
LC.Mobile AS ContactMobile,
LC.JobTitle,
T.EmailBought,
T.AddressLine1,
T.AddressLine2,
T.AddressLine3,
T.County,
T.Town,
T.Country,
T.Telephone,
T.Website,
T.Industry,
LC.DecisionMaker,
LC.PrimaryContact,
LC.ContactDisabled,
LC.CreationDate,
LC.BusinessListRecordID,
CASE LC.ZIPersonId WHEN 0 THEN 0 ELSE 1 END
FROM OldContacts LC
LEFT JOIN dbo.[Transaction] T ON LC.ZIPersonId = T.PersonId
LEFT JOIN dbo.CreditTransaction CT ON T.CreditTransactionId = CT.CreditTransactionId
即使没有Transactions或CreditTransaction,也会选择OldContacts。
回答你的新问题:
CASE LC.ZIPersonId WHEN 0 THEN 0 ELSE 1 END
应该是
CASE T.PersonId WHEN 0 THEN 0 ELSE 1 END
答案 1 :(得分:0)
首先尝试联合,然后在UNION of
中取消选择所有重复项 OLDCONTACTS
和
TRANSACTION.
很难复制你的案子,我会尝试这样的事情:
select the-cols-you-need from
(
select a.the-cols-you-need, a.distinct(foreign-key) from
(
select * from oldcontacts
union all
select * from transaction
) as a
)
inner join CreditTransaction CT on a.transactionId = CT.transactionId