当对客户信息进行注释时,可能会引用发票。我正试图找到一份将发票和相关注释结合起来的报告。
我挣扎了一段时间,因为这两列使用了两种不同的数据类型(Varchar和Text(。我最终决定使用强制转换来更改数据类型,并将它们导入到临时表中。现在我正在使用相同的数据类型VarChar,我认为它应该更容易。这是我用来创建临时表的语句。
select c.clientid, c.searchname, cast(nom.ref as varchar(150)) as Ref,
n.CreatedDate, s.name as 'Note Creator', nt.NoteTypeName, n.RecordID,
cast(n.note as varchar(150)) as note
into #ARNotes
from tblnotes n
join tblclient c on c.clientid=n.RecordID
join tblstaff s on s.staffid=n.CreatedByID
join tblNoteTypes nt on nt.NoteTypeID=n.NoteTypeID
join tblNominal nom on nom.ClientID=c.ClientID
where n.Note is not null
所有的笔记都显示在我的临时表中,有些正确。。仍然是原始数据需要神奇的where X like Y子句。
因此,我的新临时表#ARNotes中的一个数据样本看起来像这样:
Invoice Number Notes
1234 1234 Here is an example
A111 Another Example A111
B222 Note Example B222
9876 Note Example 9876
5432 No bill referenced
CCCC No bill referenced
我正在使用一个类似于下面的select语句进行查询。。我实际的最终报表会更有逻辑性,这样它只会提取我们逾期的发票。
select *
from #ARNotes
where note like ('%'+ref+'%')
order by CreatedDate desc
唯一的问题是,只有在发票只有数字的情况下才能提取。如果发票编号是字母数字,则不会显示。因此,如果我的表使用的是我的样本数据,它只会把这些结果拉到下面。因此,它将省略A111和B222的相关注释。
Invoice Number Notes
1234 1234 Here is an example
9876 Note Example 9876
我错过了什么来真正拉动所有的价值观?或者这可能吗?
经过进一步审查,我发现带有发票编号的数据有尾随空格。那些只有整数的是从我们的旧系统中导入的,并且没有在新系统中分配新的参考号/发票号。我将临时表中的导入调整为
select c.clientid, c.searchname, **rTRIM(CAST(nom.ref AS varchar(150))) AS Ref,**,
n.CreatedDate, s.name as 'Note Creator', nt.NoteTypeName, n.RecordID,
cast(n.note as varchar(150)) as note
into #ARNotes
from tblnotes n
join tblclient c on c.clientid=n.RecordID
join tblstaff s on s.staffid=n.CreatedByID
join tblNoteTypes nt on nt.NoteTypeID=n.NoteTypeID
join tblNominal nom on nom.ClientID=c.ClientID
where n.Note is not null