在转换为DTSX之前,在转换为Excel之前修改查询结果的最佳方法
我有一个执行各种任务的DTSX,其中之一是从查询中检索数据并将其转换为保存在Excel文件中 有些行有一个带有重复数据的列,我应该做的是对上述列进行更改
我认为,如果我已经有一个带有相同名称的帐户添加后缀_2的帐户,那么在当前行上,假设上游我无法阻止这种情况,那么最好的方法是什么? (我想在检索数据的查询中,可以做任何事情)
更新 我尝试了此查询,但结果没有改变
WITH CTE as
(
select
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account,
ROW_NUMBER()
OVER( PARTITION By Account Order By LastName,FirstName) as RN
from
MyTable mt (nolock)
inner join
People pe (nolock)
on
(ltrim(rtrim(mt.Profile))+' '+ltrim(rtrim(mt.House)))=SUBSTRING(ltrim(rtrim(pe.FirstName)),1,11)
where
flag_new= 1 and
pe.Code='A' and
SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1)<>'-' and
ISNUMERIC(SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1))=1
union
select
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account,
ROW_NUMBER()
OVER( PARTITION By Account Order By LastName,FirstName) as RN
from
MyTable mt (nolock)
inner join
People pe (nolock)
on
(ltrim(rtrim(mt.Profile))+' '+ltrim(rtrim(mt.House)))=SUBSTRING(ltrim(rtrim(pe.FirstName)),1,11)
where
flag_new= 1 and
pe.Code='A' and
SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1)<>'-' and
ISNUMERIC(SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1))=0
union
select
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account,
ROW_NUMBER()
OVER( PARTITION By Account Order By LastName,FirstName) as RN
from
MyTable mt (nolock)
inner join
People pe (nolock)
on
(ltrim(rtrim(mt.Profile))+' '+ltrim(rtrim(mt.House)))=SUBSTRING(ltrim(rtrim(pe.FirstName)),1,10)
where
flag_new= 1 and
pe.Code='A' and
SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1)='-'
union
select
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account,
ROW_NUMBER()
OVER( PARTITION By Account Order By LastName,FirstName) as RN
from
MyTable mt (nolock)
inner join
People pe (nolock)
on
Code='A'
where
flag_new= 1 and
ltrim(rtrim(mt.Profile))= 'OFFICE 1' and
pe.Type in ('30','31') and
((pe.end_validation_date is null) or (pe.end_validation_date>getdate())))
SELECT
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account+
Case
when RN = 1
then ''
else '_' + cast(RN+1 as varchar(20))
end as Account
FROM CTE;
I have a dtsx that performs various tasks, one of them is to retrieve data from a query and convert it for saving in an excel file
Some rows have a column with duplicate data, what I should do is make a change to the aforementioned column as in the example
Before(the column in question is in yellow)
After(the desired result is the two gray cells)
I would think that to the current line if I already have an Account with the same name add the suffix _2, assuming that upstream I cannot prevent this situation, what is the best way to go? (I imagine that in the query that retrieves the data, can do anything)
UPDATE
I tried this query but the result didn't change
WITH CTE as
(
select
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account,
ROW_NUMBER()
OVER( PARTITION By Account Order By LastName,FirstName) as RN
from
MyTable mt (nolock)
inner join
People pe (nolock)
on
(ltrim(rtrim(mt.Profile))+' '+ltrim(rtrim(mt.House)))=SUBSTRING(ltrim(rtrim(pe.FirstName)),1,11)
where
flag_new= 1 and
pe.Code='A' and
SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1)<>'-' and
ISNUMERIC(SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1))=1
union
select
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account,
ROW_NUMBER()
OVER( PARTITION By Account Order By LastName,FirstName) as RN
from
MyTable mt (nolock)
inner join
People pe (nolock)
on
(ltrim(rtrim(mt.Profile))+' '+ltrim(rtrim(mt.House)))=SUBSTRING(ltrim(rtrim(pe.FirstName)),1,11)
where
flag_new= 1 and
pe.Code='A' and
SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1)<>'-' and
ISNUMERIC(SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1))=0
union
select
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account,
ROW_NUMBER()
OVER( PARTITION By Account Order By LastName,FirstName) as RN
from
MyTable mt (nolock)
inner join
People pe (nolock)
on
(ltrim(rtrim(mt.Profile))+' '+ltrim(rtrim(mt.House)))=SUBSTRING(ltrim(rtrim(pe.FirstName)),1,10)
where
flag_new= 1 and
pe.Code='A' and
SUBSTRING(ltrim(rtrim(pe.FirstName)),11,1)='-'
union
select
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account,
ROW_NUMBER()
OVER( PARTITION By Account Order By LastName,FirstName) as RN
from
MyTable mt (nolock)
inner join
People pe (nolock)
on
Code='A'
where
flag_new= 1 and
ltrim(rtrim(mt.Profile))= 'OFFICE 1' and
pe.Type in ('30','31') and
((pe.end_validation_date is null) or (pe.end_validation_date>getdate())))
SELECT
pe.Code,
pe.Name
Fiscal_Code,
LastName,
FirstName,
Account+
Case
when RN = 1
then ''
else '_' + cast(RN+1 as varchar(20))
end as Account
FROM CTE;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许可以通过使用变量对象类型和脚本组件来完成
一个非常糟糕的解决方案,即将数据与字符串变量进行比较,并在变量不存在时将其添加到该变量中,并将所需的前缀添加到该变量(如果存在) 。
您可以使用表达任务来完成。
该解决方案可能适合少量记录,但是对于大量记录,这是一个非常糟糕的解决方案
Maybe it can be done by using Variable object type and script component
A very bad solution is to compare the data with a string variable and add it to the variable if it doesn't exist, and add the desired prefix to the variable if it exists.
You can do it using Expression Task.
This solution may be suitable for a small number of records, but for a large number of records, it is a very bad solution