我有临时客户表
create table #tmpCustomer
(
ID int identity(1,1),
CustomerID nvarchar(128),
CustomerName nvarchar(50),
FirstName nvarchar(50),
LastName nvarchar(50),
DateCreated nvarchar(50),
CreatedBy int,
YearBuilt nvarchar(50),
IsActive bit,
CustTypeID nvarchar(128),
CustomerTypeID int,
CompanyID int,
Line1 nvarchar(50) not null,
Line2 nvarchar(50) null,
Line3 nvarchar(50) null,
City nvarchar(50) not null,
ZipCode nvarchar(15),
StateID int not null,
NewCounty nvarchar(20),
SubDivisionID int null
)
我从原始客户表填充并使用此表来填充临时表
declare @separator char(1);
set @separator = ',';
insert into #tmpCustomer
(CustomerID, CustomerName, FirstName, LastName, DateCreated, CreatedBy, YearBuilt, IsActive, CustomerTypeID, CompanyID, Line1, Line2, Line3, City, ZipCode, StateID, CountyID, NewCounty, SubDivisionID)
select
c.CustomerID,
c.CustomerName,
LastName = Case
When CHARINDEX(@separator, c.CustomerName, 1) - 1 <= 0 Then c.CustomerName
Else SUBSTRING(c.CustomerName,1,CHARINDEX(@separator, c.CustomerName, 1) - 1)
End,
FirstName = Case
When CHARINDEX(@separator, c.CustomerName, 1) - 1 <= 0 Then NULL
Else SUBSTRING(c.CustomerName,CHARINDEX(@separator, c.CustomerName, 1) + 1, Len(c.CustomerName) - (CHARINDEX(@separator, c.CustomerName, 1) ))
End,
GETDATE(),
1,
case when c.YearBuilt is NULL then 'N/A' else c.YearBuilt end,
c.EnabledInd,
case when CustomerTypeID = '4F0B6446-441D-46B8-81CB-B0E8A94624A7' then 1 else 2 end,
1,
c.Address1,
c.Address2,
null,
c.City,
c.ZipCode,
11,
null,
Case when c.County <> '' then County ELSE 'N/A' end as "County",
null
from [Customer] c
where c.CompanyID = '21DE6731-5E6C-11D5-AF81-00D0B74725F6'
这总计有33165条记录,这正是它应该是的。
接下来是我有一个单独的表来保存地址
create table #tmpAddress
(
ID int identity(1,1),
Line1 nvarchar(50) not null,
Line2 nvarchar(50) null,
Line3 nvarchar(50) null,
City nvarchar(50) not null,
ZipCode nvarchar(15),
StateID int not null,
CountyID int null,
SubDivisionID int null
)
如果我运行这个
insert into #tmpAddress
(Line1, Line2, Line3, City, ZipCode, StateID, CountyID,SubDivisionID)
select cr.Line1, cr.Line2,cr.Line3, cr.City, cr.ZipCode, 11, 0, null from #tmpCustomer cr
然后我在33165
获得正确的地址我遇到的问题是郡。
我有一张县的表,叫做县,问题似乎是当我加入县表以获取其ID时。这里返回的记录多于应有的记录,它返回34546条记录,比另一条记录多出1000条记录。
insert into #tmpAddress
(Line1, Line2, Line3, City, ZipCode, StateID, ct.CountyID,SubDivisionID)
select cr.Line1, cr.Line2,'', cr.City, cr.ZipCode, 11, ct.CountyID, null from #tmpCustomer cr
inner join Exo.dbo.County ct on ct.County = cr.NewCounty
我不知道出了什么问题,也许有人可以向我指出,所以我可以获得tmpAddress表的33165条记录
答案 0 :(得分:1)
正如unfinishedmonkey所提到的,看起来Exo.dbo.County.County
中有重复值,您可以查看以下查询:
SELECT c.County, COUNT(*)
FROM Exo.dbo.County c
GROUP BY c.County
HAVING COUNT(*) > 1
ORDER BY c.County
如果这返回任何记录,那么Exo.dbo.County
中的重复记录在County
字段中具有相同的值,这反过来导致您在结果集中获得多行的单行来自#tmpCustomer
。
您可以通过多种方式解决此问题,首先是从Exo.dbo.County
删除记录,以便该表中的所有County
值都是唯一的,或者通过修改SELECT
子句来解决:< / p>
SELECT DISTINCT cr.Line1, cr.Line2,'', cr.City, cr.ZipCode, 11, ct.CountyID, NULL
FROM #tmpCustomer cr
INNER JOIN Exo.dbo.County ct ON ct.County = cr.NewCounty
如果此查询仍然返回的记录多于SELECT Line1, Line2, cr.City, cr.ZipCode FROM #tmpCustomer
的记录,我不确定问题是什么。如果它返回的记录较少,那么您必须在#tmpCustomer中有一些记录NewCounty
,而Exo.dbo.County.County
中的值不会出现,所以您可能需要考虑{{1}如果是这样的话。